Change hardcoded file path to user prompted in VBA?
Right now, I have a VBA macro for Word which parses a document for a cert开发者_Python百科ain font and outputs all font of the selected type to a text file.
The hard coded line which I open the text file is something like this:
Open "C:\Documents and Settings\Output.txt" For Output As #n
Can I change this so the user is prompted to enter the file path at this point in the macro? Something like:
Open (PROMPTS USER FOR FILE PATH HERE) For Output As #n
Sorry if this seems trivial. I am new to VBA coding.
Two ways:
Simple
Dim path As String
path = InputBox("Enter a file path", "Title Here")
Open path For Output As #1
Close #1
With File Chooser
Dim path As String
With Application.FileDialog(msoFileDialogOpen)
.Show
If .SelectedItems.Count = 1 Then
path = .SelectedItems(1)
End If
End With
If path <> "" Then
Open path For Output As #n
End If
You're looking for the InputBox
function.
Open InputBox("Enter a file path", "Title", "default path") For Output As #n
精彩评论