VBA: how to create new txt file name?

joeu2004

Banned user
Joined
Mar 2, 2014
Messages
3,080
Office Version
  1. 2010
Platform
  1. Windows
The code below demonstrates how I wanted to create a new txt file name. But apparently, GetOpenFilename works only for existing files.

(That is, when I enter a non-existing file name, I get the error "File not found".)

How can I get an API like GetOpenFilename, but for entering non-existing (or existing) file names?

Rich (BB code):
Option Explicit

Sub testit()
Dim fdOut As Integer, path As String

On Error Resume Next
path = Application.GetOpenFilename(Title:="Open new OUTPUT file")
If path = "" Or path = "False" Then Exit Sub

MsgBox path

On Error GoTo 0
fdOut = FreeFile
Open path For Output Access Write As #fdOut 

MsgBox "open okay"

' [... write to file ...]

Close #fdOut 

End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
.
Will this work ?

Code:
Option Explicit


Sub ExportRangetoFile()


Dim wb As Workbook
Dim saveFile As String
Dim WorkRng As Range


On Error Resume Next


Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wb = Application.Workbooks.Add


saveFile = Application.GetSaveAsFilename(fileFilter:="Text Files (*.txt), *.txt")
wb.SaveAs Filename:=saveFile, FileFormat:=xlText, CreateBackup:=False
wb.Close


Application.DisplayAlerts = True
Application.ScreenUpdating = True


End Sub
 
Upvote 0
Solution
Yes! Specifically, just this line:

path = Application.GetSaveAsFilename("", Title:="Open new OUTPUT file")

I thought, too, that what I wanted was "like" the Save As interface. But I must have made a mistake when I went looking for GetSaveAsFilename.

Thanks so much.
 
Upvote 0

Forum statistics

Threads
1,215,475
Messages
6,125,028
Members
449,205
Latest member
Eggy66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top