[英]Open Word Document From Dynamic Directory VB.Net
我有一個已為其開發用戶指南的程序。 我已將此用戶指南放在項目目錄中。 我創建了一個MenuStrip項,通過它可以在用戶計算機上的Word中打開用戶指南。 我可以使用以下代碼成功做到這一點:
Try
userGuide = MSWord.Documents.Open("C:Users\administrator\Documents\VisualStudio2010\Project3\UserGuide.doc")
MSWord.WindowState = Word.WdWindowState.wdWindowStateNormal
MSWord.Visible = True
Catch ex As Exception
MsgBox("An error has prevented the document from opening. The document may not be available." & vbCrLf & vbCrLf & _
"Please try one of the following options:" & vbCrLf & _
"- Check to see if the document is already open" & vbCrLf & _
"- Restart the program")
End Try
問題是,用於打開文件的路徑在用戶計算機上將不存在。 這是一個獨立的系統,因此無法創建用於放置文檔的文件共享,因此無法對公共路徑進行編碼。
有沒有辦法編碼動態路徑? 也許像這樣:
userGuide = MSWord.Documents.Open("%windir%\UserGuide.doc")
謝謝!
如果文檔將相對於應用程序可執行文件的安裝路徑存儲,則從exe路徑開始:
Dim path As String
path = System.IO.Path.GetDirectoryName( _
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
Dim docPath as String;
docPath = Path.Combine(path,"UserGuide.doc");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.