在Word中替换书签中的文本而不删除书签 [英] Replace Text in Bookmark in Word without Deleting Bookmark

查看:589
本文介绍了在Word中替换书签中的文本而不删除书签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

当我运行宏时,我得到的最新错误是错误号:13这是代码 - 任何人都可以帮助我指出我在做什么请问使用以下建议可以帮助我大部分的方式,但是当我将BMRange设置为书签时,我会收到错误13 - http://word.mvps.org/faqs/macrosvba/InsertingTextAtBookmark.htm

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

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


推荐答案

下面的调整工作完美。由于Word中的交叉引用在原始书签更改时不会自动更新,所以我将以下内容更改为刷新引用。

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.

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

这篇关于在Word中替换书签中的文本而不删除书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆