【问题标题】:Word macro error while copying contents from one document to another将内容从一个文档复制到另一个文档时出现 Word 宏错误
【发布时间】:2021-09-02 12:56:23
【问题描述】:

我正在尝试将 doc1 的内容(格式化的文本和图像)复制到 doc2 中,其中 doc2 包含表格,并且我想将 doc1 的内容复制到 doc2 的第 18 个单元格中。

我从下面的参考中得到了代码,并相应地对其进行了修改。工作代码如下:

参考链接:How do I copy the contents of one word document to the end of another using vba?

Sub copyContents()
    Dim wordWasRunning As Boolean
    wordWasRunning = IsMSWordRunning()

    Dim mswApp As Word.Application
    Set mswApp = AttachToMSWordApplication()

    Dim doc1 As Word.Document
    Dim doc2 As Word.Document
    Set doc1 = mswApp.Documents.Open("C:\Temp\SingleDoc.docx")
    Set doc2 = mswApp.Documents.Open("C:\Temp\MergedDoc.docx")
    
    Dim destination As Word.Range
    Dim source As Word.Range
    
    Set source = doc1.Content
    Set destination = doc2.Content
    
    destination.Tables(1).Range.Cells(18).Range.FormattedText = source
    
    doc2.Close SaveChanges:=True
    doc1.Close

    If Not wordWasRunning Then
        mswApp.Quit
    End If
End Sub

当我使用不同的窗口并尝试将内容从 doc1 复制到 doc2(并且我从 doc3 运行宏)时,上面的代码正在工作。

问题: 我试图修改代码,以便可以从 doc2 本身运行宏。但是修改后的代码(如下所述)导致错误。

修改后的代码:

Sub copyContents()
    Dim wordWasRunning As Boolean
    wordWasRunning = IsMSWordRunning()
    Dim mswApp As Word.Application
    Set mswApp = AttachToMSWordApplication()
    
    Dim doc1 As Word.Document
    Set doc1 = mswApp.Documents.Open("C:\Temp\SingleDoc.docx")
    
    Dim source As Word.Range
    Set source = doc1.Content
    
    ActiveDocument.Tables(1).Range.Cells(18).Range.FormattedText = source
    
End Sub

错误:

错误 5941:请求的集合成员不存在

*导致错误的行:ActiveDocument.Tables(1).Range.Cells(18).Range.FormattedText = source 我试图将上面的行更改为以下试验(只是为了使其与工作代码一样接近)但我得到了同样的错误(错误 5941)

试验一:

ActiveDocument.Content.Tables(1).Range.Cells(18).Range.FormattedText = source

试验 2:

ActiveDocument.Range.Tables(1).Range.Cells(18).Range.FormattedText = source

谁能帮我解决这个问题?

谢谢, 维奈

【问题讨论】:

  • 嗨@PeterT,你能帮我解决这个问题吗..

标签: vba ms-word


【解决方案1】:

首先,您的代码不是为了在 Word 中运行而编写的。如果您在 Word 中运行它,那么前四行代码是完全没有必要的,因为 Word 显然正在运行。

Dim wordWasRunning As Boolean
wordWasRunning = IsMSWordRunning()
Dim mswApp As Word.Application
Set mswApp = AttachToMSWordApplication()

其次,Set doc1 = Documents.Open("C:\Temp\SingleDoc.docx") 使doc1 成为活动文档,您只需查看屏幕即可看到。这是ActiveDocument 需要小心使用的原因之一。如果您从目标文档运行代码,则可以改用ThisDocument

Sub copyContents()
   
    Dim doc1 As Word.Document
    Set doc1 = Documents.Open("C:\Temp\SingleDoc.docx")
    
    Dim source As Word.Range
    Set source = doc1.Content
    
    ThisDocument.Tables(1).Range.Cells(18).Range.FormattedText = source.FormattedText
    doc1.Close
    
End Sub

或者,您可以使用变量来显式引用目标文档。

Sub copyContents()
   
    Dim docTarget As Word.Document
    Set docTarget = ActiveDocument
    Dim docSource As Word.Document
    Set docSource = Documents.Open("C:\Temp\SingleDoc.docx")
    
    docTarget.Tables(1).Range.Cells(18).Range.FormattedText = docSource.Content.FormattedText
    docSource.Close
    
End Sub

【讨论】:

  • 感谢您的帮助和及时回复。第二个代码对我有用。
猜你喜欢
  • 2014-09-22
  • 1970-01-01
  • 1970-01-01
  • 2020-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多