gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:34:20 25 4
gpt4 key购买 nike

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

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

Open "C:\Documents and Settings\Output.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

关于vba - 将硬编码文件路径更改为 VBA 中提示的用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7152660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com