2011-08-22 88 views
1

現在,我有一個Word的VBA宏,用於解析特定字體的文檔,並將選定類型的所有字體輸出到文本文件。將硬編碼文件路徑更改爲在VBA中提示的用戶?

硬編碼線,我打開文本文件是這樣的:

Open "C:\Documents and Settings\Output.txt" For Output As #n 

我可以改變這種做法,系統會提示用戶在宏觀這一點上輸入文件路徑?例如:

Open (PROMPTS USER FOR FILE PATH HERE) For Output As #n 

對不起,如果這似乎微不足道。我是VBA編碼的新手。

回答

6

兩種方式:

簡單

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 
+0

感謝您使用Application.FileDialog提示 - 參考用戶可以看到這篇文章:[使用Microsoft Access中的文件對話框](http://support.microsoft.com/kb/824272)(support.microsoft.com ) –

2

您正在尋找InputBox功能。

Open InputBox("Enter a file path", "Title", "default path") For Output As #n 
+0

有一個在VBA – transistor1

+0

@transistor沒有提示功能:固定;謝謝。我太習慣Javascript了。 – SLaks