替换Word中的书签中的文本而不删除书签

时间:2021-09-10 16:51:41

I'm working on an Excel File that calls a Word Doc Template and updates/replaces all text within specific enclosing bookmarks without deleting the actual bookmark. Retaining the Bookmark brackets in Word is very important because there are cross references that rely on these bookmarks staying intact or they won't work. I've combed a few different posts here and some other forums but after about 4 hours of trial and error I cannot seem to get it working.

我正在处理一个Excel文件,该文件调用Word Doc模板并更新/替换特定封闭书签中的所有文本,而不删除实际书签。在Word中保留书签括号非常重要,因为有些交叉引用依赖于这些书签保持不变或无法正常工作。我在这里和其他一些论坛上梳了几个不同的帖子,但经过大约4个小时的试验和错误,我似乎无法让它运作起来。

The most recent error I get when I run the macro is "Error No: 13" Here's the code - Can anyone help clue me in on what I'm goofing up on please? Utilizing the following advice gets me most of the way there, but when I set the BMRange to the Bookmark, I get the Error 13 - http://word.mvps.org/faqs/macrosvba/InsertingTextAtBookmark.htm

我运行宏时遇到的最新错误是“错误号码:13”这是代码 - 任何人都可以帮我找到我正在搞砸的内容吗?利用以下建议可以获得我的大部分建议,但是当我将BMRange设置为书签时,我得到了错误13 - http://word.mvps.org/faqs/macrosvba/InsertingTextAtBookmark.htm

ub BCMerge()
Dim pappWord As Object
Dim docWord As Object
Dim wb As Excel.Workbook
Dim xlName As Excel.Name
Dim TodayDate As String
Dim Path As String
Dim BMRange As Range

  Set wb = ActiveWorkbook
  TodayDate = Format(Date, "mmmm d, yyyy")
  Path = wb.Path & "\pushmerge1.dot"

  On Error GoTo ErrorHandler

'Create a new Word Session
  Set pappWord = CreateObject("Word.Application")

  On Error GoTo ErrorHandler

'Open document in word
  Set docWord = pappWord.Documents.Add(Path)

'Loop through names in the activeworkbook
  For Each xlName In wb.Names

    'if xlName's name is existing in document then put the value in place of the bookmark
        If docWord.Bookmarks.Exists(xlName.Name) Then

       'Identify current Bookmark range and insert text
        Set BMRange = docWord.Bookmarks(xlName.Name).Range '''Mismatch Error 13'''
        BMRange.Text = Range(xlName.Value)
        'Re-insert the bookmark
        docWord.Bookmarks.Add xlName.Name, BMRange
        End If

  Next xlName

'Activate word and display document
  With pappWord
      .Visible = True
      .ActiveWindow.WindowState = 0
      .Activate
  End With

'Release the Word object to save memory and exit macro
ErrorExit:
   Set pappWord = Nothing
   Exit Sub

'Error Handling routine
ErrorHandler:
   If Err Then
      MsgBox "Error No: " & Err.Number & "; There is a problem"
      If Not pappWord Is Nothing Then
        pappWord.Quit False
      End If
      Resume ErrorExit
   End If
End Sub

2 个解决方案

#1


The type mismatch comes from how you declare BMRange. A Word Range is not the same object type as an Excel Range, so you need to either set it as an Object (if you don't have a Word reference) or explicitly declare it as Word.Range. Since you appear to be using late binding, change it to Object.

类型不匹配来自您声明BMRange的方式。 Word范围与Excel范围不是同一对象类型,因此您需要将其设置为Object(如果您没有Word引用)或将其显式声明为Word.Range。由于您似乎使用后期绑定,因此将其更改为Object。

'Incorrect
'Dim BMRange As Range
'Correct
Dim BMRange As Object

After that, all you need to do is keep a copy of the Bookmark range in order to re-add it after changing the Text:

之后,您需要做的就是保留书签范围的副本,以便在更改文本后重新添加它:

'Loop through names in the activeworkbook
For Each xlName In wb.Names

    'if xlName's name is existing in document then put the value in place of the bookmark
    If docWord.Bookmarks.Exists(xlName.Name) Then
        'Copy the Bookmark's Range.
        Set BMRange = docWord.Bookmarks(xlName.Name).Range.Duplicate
        BMRange.Text = Range(xlName.Value)
        'Re-insert the bookmark
        docWord.Bookmarks.Add xlName.Name, BMRange
    End If

Next xlName

#2


The tweaks below worked perfectly. Since Cross References in Word don't automatically update when the original bookmarks change, I changed the following to "refresh" the references.

下面的调整完美无缺。由于Word中的交叉引用在原始书签更改时不会自动更新,因此我更改了以下内容以“刷新”引用。

'Activate word and display document
  With pappWord
      .Visible = True
      .Selection.WholeStory
      .Selection.Fields.Update
      .ActiveWindow.WindowState = 0
      .Activate
  End With

#1


The type mismatch comes from how you declare BMRange. A Word Range is not the same object type as an Excel Range, so you need to either set it as an Object (if you don't have a Word reference) or explicitly declare it as Word.Range. Since you appear to be using late binding, change it to Object.

类型不匹配来自您声明BMRange的方式。 Word范围与Excel范围不是同一对象类型,因此您需要将其设置为Object(如果您没有Word引用)或将其显式声明为Word.Range。由于您似乎使用后期绑定,因此将其更改为Object。

'Incorrect
'Dim BMRange As Range
'Correct
Dim BMRange As Object

After that, all you need to do is keep a copy of the Bookmark range in order to re-add it after changing the Text:

之后,您需要做的就是保留书签范围的副本,以便在更改文本后重新添加它:

'Loop through names in the activeworkbook
For Each xlName In wb.Names

    'if xlName's name is existing in document then put the value in place of the bookmark
    If docWord.Bookmarks.Exists(xlName.Name) Then
        'Copy the Bookmark's Range.
        Set BMRange = docWord.Bookmarks(xlName.Name).Range.Duplicate
        BMRange.Text = Range(xlName.Value)
        'Re-insert the bookmark
        docWord.Bookmarks.Add xlName.Name, BMRange
    End If

Next xlName

#2


The tweaks below worked perfectly. Since Cross References in Word don't automatically update when the original bookmarks change, I changed the following to "refresh" the references.

下面的调整完美无缺。由于Word中的交叉引用在原始书签更改时不会自动更新,因此我更改了以下内容以“刷新”引用。

'Activate word and display document
  With pappWord
      .Visible = True
      .Selection.WholeStory
      .Selection.Fields.Update
      .ActiveWindow.WindowState = 0
      .Activate
  End With