![](/img/trans.png)
[英]Import a word from a specific word document into a specific excel document
[英]How to make sure from Excel that a specific Word document is open or not?
我希望我的excel宏通过在放置在模板word文档中的书签之后插入电子表格数据来创建报告。
但是我发现, 如果模板字文档已经打开,则宏将崩溃 ,因此,模板文档将被锁定为只读,并且无法再由宏访问。
有没有办法防止宏崩溃,即使模板Word文档已经打开 ?
下面是我的代码
Set wdApp = CreateObject("Word.Application") 'Create an instance of word
Set wdDoc = wdApp.Documents.Open(ThisWorkbook.Path & "\Templates\Template_Confirmation.docx") 'Create a new confirmation note
以下是评论中建议内容的演变:
一种测试文件是否打开并在测试过程中直接设置文件的功能 。
如何使用它 :
Sub test()
Dim WdDoc As Word.Document
Set WdDoc = Is_Doc_Open("test.docx", "D:\Test\")
MsgBox WdDoc.Content
WdDoc.Close
Set WdDoc = Nothing
End Sub
和功能:
Public Function Is_Doc_Open(FileToOpen As String, FolderPath As String) As Word.Document
'Will open the doc if it isn't already open and set an object to that doc
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
On Error Resume Next
'Set wrdApp = GetObject(, "Word.Application")
If wrdApp Is Nothing Then
Set wrdApp = CreateObject("Word.Application")
Set wrdDoc = wrdApp.Documents.Open(FolderPath & FileToOpen)
Else
On Error GoTo NotOpen
Set wrdDoc = wrdApp.Documents(FileToOpen)
GoTo OpenAlready
NotOpen:
Set wrdDoc = wrdApp.Documents.Open(FolderPath & FileToOpen)
End If
OpenAlready:
On Error GoTo 0
Set Is_Doc_Open = wrdDoc
Set wrdApp = Nothing
Set wrdDoc = Nothing
End Function
唯一的缺点是您没有Word应用程序的引用...
欢迎任何建议/发展!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.