Selection.InsertFile FileName:=“ C:\ Users \ FileName.Docx”行代码适用于Word VBA,但不适用于Excel VBA

时间:2018-12-21 10:15:58

标签: excel vba ms-word

我正在尝试使用书签在现有Word文件模板中插入多个Word文件。
下面在现有单词文件中搜索特定的书签,然后在现有单词文件中插入另一个单词文件的内容。
除了行Selection.InsertFile FileName:=BookmarkValue之外,其余代码工作正常。
此行代码在Word VBA中可用,但在Excel VBA中不起作用。

能帮我弄清楚我哪里错了吗?
为了方便起见,我在下面提供了简化的代码版本。

Sub CreateWordDocuments3()
    Dim LastRow As Long
    Dim DocLoc, BookmarkName, BookmarkValue, FileName As String

    Dim WordDoc, WordApp As Object
    Dim WordContent As Word.Range

    With Sheet3

        'Open Word Template
        On Error Resume Next                     'If Word is already running
        Set WordApp = GetObject("Word.Application")
        If Err.Number <> 0 Then
            'Launch a new instance of Word
            Err.Clear
            'On Error GoTo Error_Handler
            Set WordApp = CreateObject("Word.Application")
            WordApp.Visible = True               'Make the application visible to the user
        End If

        DocLoc = "C:\Users\DKC\Desktop\Basetemplate.docx"

        Set WordDoc = WordApp.Documents.Open(FileName:=DocLoc, ReadOnly:=False) 'Open Template          

        BookmarkName = .Cells(4, 2).Value        'BookmarkName is IndustryAnlysis in the word file (DocLoc)
        BookmarkValue = .Cells(4, 3).Value       'BookmarkValue is link to the word file to be inserted = "C:\Users\DKC\Desktop\Industry Analysis.docx"
        WordDoc.Bookmarks(BookmarkName).Select
        Selection.InsertFile FileName:=BookmarkValue ' Line code working in word VBA but not in excel VBA

        FileName = ThisWorkbook.Path & "\" & "NewDoc" & ".docx"
        WordDoc.SaveAs FileName

        WordApp.Quit
    End With
End Sub

1 个答案:

答案 0 :(得分:0)

借助这篇帖子How to use Words selection methods through Excel VBA弄清楚了它-或更具体地说,是@TimWilliams在第二个答案底部的评论。

Selection的问题在于它将引用Excel。我试图将其链接到Word文档,但这是应用程序的属性:

  

每个文档窗口窗格只能有一个“选择”对象,并且   整个应用程序中只有一个Selection对象可以处于活动状态。

     

Selection object

我也将CreateWord拉出了一个单独的功能。

Public Sub CreateWordDoc()

    Dim oWrd As Object, oWrdDoc As Object
    Dim BkMkName As String, BkMkValue As String

    Const docloc As String = "C:\Users\DKC\Desktop\Basetemplate.docx"

    Set oWrd = CreateWord
    Set oWrdDoc = oWrd.documents.Open(docloc, False)

    With Sheet3
        BkMkName = .Cells(4, 2)
        BkMkValue = .Cells(4, 3)
    End With

    oWrdDoc.Bookmarks(BkMkName).Select
    oWrd.Selection.InsertFile Filename:=BkMkValue

End Sub

Public Function CreateWord(Optional bVisible As Boolean = True) As Object

    Dim oTempWD As Object

    On Error Resume Next
    Set oTempWD = GetObject(, "Word.Application")

    If Err.Number <> 0 Then
        Err.Clear
        On Error GoTo ERROR_HANDLER
        Set oTempWD = CreateObject("Word.Application")
    End If

    oTempWD.Visible = bVisible
    Set CreateWord = oTempWD

    On Error GoTo 0
    Exit Function

ERROR_HANDLER:
        MsgBox "Error " & Err.Number & vbCr & _
                " (" & Err.Description & ") in procedure CreateWord."
            Err.Clear

End Function  

编辑-
因此,如果您不想使用我发布的代码,则可以通过更改

Selection.InsertFile FileName:=BookmarkValue  

WordApp.Selection.InsertFile FileName:=BookmarkValue