现在,我有一个Word的VBA宏,它为特定字体解析文档并将所选类型的所有字体输出到文本文件中。
我打开文本文件的硬编码行是这样的:Open "C:Documents and SettingsOutput.txt" For Output As #n
我是否可以更改这一点,以便提示用户在宏中的此时输入文件路径?比如:
Open (PROMPTS USER FOR FILE PATH HERE) For Output As #n
很抱歉,如果这看起来微不足道。我是新的VBA编码。
两种方式:
简单Dim path As String
path = InputBox("Enter a file path", "Title Here")
Open path For Output As #1
Close #1
使用文件选择器
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
您正在寻找InputBox
函数。
Open InputBox("Enter a file path", "Title", "default path") For Output As #n