2012-05-27 38 views
0

我有一个程序,我开发了一个用户指南。我已将此用户指南放在项目目录中。我创建了一个MenuStrip项目,通过它在用户的机器上在Word中打开用户指南。我能成功用下面的代码来做到这一点:从动态目录打开Word文档VB.Net

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") 

谢谢!

+0

我很好奇。为什么你决定向用户提供一个名为UserGuide的文档?如果他们没有安装Word,会发生什么? – Steve

+0

@Steve如果他们没有安装Word,他们可以下载OpenOffice,它会打开.doc和.docx。或者他们可以意识到这是2012年,MS Office并不昂贵。 :D – Phoenix

+0

当然,还有其他的可能性,但这样用户可以编辑文档。这是一个可以接受的选项吗? (通常我从doc打印PDF)。 – Steve

回答

1

如果该文件将相对于应用程序的可执行文件的安装路径进行存储,然后用path of the 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"); 
+0

这完全奏效。谢谢!! – Phoenix