在同一脚本中使用此工作簿和 Word 应用程序时出现 Excel VBA 438 错误

问题描述 投票:0回答:1

我正在测试一个更大项目的方法,并且该脚本遇到了 438 错误。如果我先抓取工作表,错误来自调用 word.application,如果我先抓取 word doc 内容,则当我抓取工作表时会抛出 438 错误。我找不到任何可行的解决方案,尽管我确实尝试了早期绑定,并且已经开始首先复制文档,以便它始终在新的 Word 文档中工作,以避免其他冲突。这里还有什么问题?

Option Explicit

Sub Merge()
    Dim WordDoc As Object, N As Variant, i As Integer, j As Integer
    'i = Range("C2").Value  'pulls length of list from an excel function located in cell C2 =COUNTIF(B4:B5005,"*")
    Dim wordApp As Word.Application
    Dim source As String
    Dim destination As String
    Dim xlobj As Object
    Set xlobj = CreateObject("Scripting.FileSystemObject")
    source = "C:\...\MERGE TEMPLATE - CONSTRUCTION LOAN AGREEMENT.docx"
    destination = "C:\...\WORKING - CONSTRUCTION LOAN AGREEMENT.docx"
    xlobj.CopyFile source, destination, True
    Set xlobj = Nothing
    Set wordApp = CreateObject(Class:="Word.Application")
    wordApp.Options.SaveInterval = 0
    Set WordDoc = wordApp.Documents.Open("C:\...\WORKING - CONSTRUCTION LOAN AGREEMENT.docx")
    WordDoc.Visible = True
    Dim dataws As Worksheet
    Set dataws = ThisWorkbook.Worksheets("Merge Fields")
    N = dataws.Range("a1:b1").Value 'test row
    Print (N)
   'For j = 1 To i
    With wordApp
        With WordDoc.Content.Find
            .Text = N(1, 1)
            .Replacement.Text = N(1, 2)
            .Wrap = wdFindContinue
            .MatchWholeWord = True
            .Execute Replace:=wdReplaceAll
      End With
    End With
    'Next j
    wordApp.ActiveDocument.Save
    wordApp.ActiveDocument.Close
    wordApp.Quit

    Set wordApp = Nothing
    Set WordDoc = Nothing
End Sub
excel vba runtime-error
1个回答
0
投票
WordDoc.Visible = True

应该是

wordApp.Visble = True

Document
没有
Visible
属性。

© www.soinside.com 2019 - 2024. All rights reserved.