2016-11-15 58 views
0

请有人帮我解释这个语法。 我想将现有的文档分配给一个我可以稍后打开和激活的变量。运行代码时可能会打开几个文档(包括包含vba项目的文档),所以我需要确保我正在激活正确的文档。 下不工作,因为我需要的文件名,而不是线下决赛的文件路径,但我不知道如何从第2行的路径提取此...将现有文档分配给vba中的变量

Dim NewQuote1 as string Set NewQuote1 ="C:\Library\doc1.docx" Documents.Open fileName:=NewQuote1 ... Documents(NewQuote1).Activate

+0

您可以使用FileSystemObject的得到你要寻找的文件名属性。在https://blogs.technet.microsoft.com/heyscriptingguy/2006/05/30/how-can-i-extract-just-the-file-name-from-the-full上显示如何使用它的几个很好的示例-path-to-the-file/ – dbmitch

回答

1

我我没有在我的Word VBA,但我很确定它会非常类似于Excel。

所以:

Sub Test() 

    Dim wrdDoc As Document 
    Dim NewQuote1 As String 

    NewQuote1 = "C:\Library\doc1.docx" 

    Set wrdDoc = Documents.Open(NewQuote1) 

    'You can now reference the document using wrdDoc. 
    wrdDoc.Activate 

    MsgBox wrdDoc.Name 

End Sub 
+0

这非常有帮助。谢谢 –

+0

刚才在你的问题中注意到你谈论的是包含VBA项目的文档 - 这个文档总是可以使用'ThisDocument'来引用,所以'ThisDocument.Activate','Msgbox ThisDocument.Name'等等。 –