将硬编码文件路径更改为VBA中提示的用户?

Sha*_*leh 3 vba user-input

现在,我有一个用于Word的VBA宏,它解析某个字体的文档,并将所选类型的所有字体输出到文本文件.

我打开文本文件的硬编码行是这样的:

Open "C:\Documents and Settings\Output.txt" For Output As #n
Run Code Online (Sandbox Code Playgroud)

我可以更改此设置,以便提示用户在宏中此时输入文件路径吗?就像是:

Open (PROMPTS USER FOR FILE PATH HERE) For Output As #n
Run Code Online (Sandbox Code Playgroud)

对不起,如果这看起来微不足道.我是VBA编码的新手.

Ban*_*joe 8

两种方式:

简单

Dim path As String

path = InputBox("Enter a file path", "Title Here")
Open path For Output As #1
Close #1
Run Code Online (Sandbox Code Playgroud)

使用文件选择器

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
Run Code Online (Sandbox Code Playgroud)