[英]A few specific word bookmarks not being preserved after updating their text
So, I'm trying to use an excel spreadsheet to populate a word template.因此,我正在尝试使用 excel 电子表格来填充 word 模板。 The named ranges in the.xlsm file are the same as the word template bookmark names. .xlsm 文件中的命名范围与单词模板书签名称相同。 Most run just fine, but for two of them, the.bookmarks.add method fails without an error or warning and the bookmark is then lost.大多数运行得很好,但对于其中两个,.bookmarks.add 方法失败,没有错误或警告,然后书签丢失。 As far as I can see, there is nothing special or particular about these two.据我所知,这两者并没有什么特别或特别之处。 What might be the cause?可能是什么原因?
The offending bookmarks are called "client_address2" (of 2) and "test_name2" (of 3).有问题的书签称为“client_address2”(共 2 个)和“test_name2”(共 3 个)。
Sub preencher_word_2dd()
'Substitui os bookmarks do word pelo conteúdo do Bookmarks.xlsm
Dim wdApp As Word.Application
Dim wDoc As Word.Document
Dim bmkwb As Workbook
Dim bmk As Worksheet
Dim nome_report As String
Dim i As Integer
Set bmkwb = ActiveWorkbook
Set bmk = bmkwb.Sheets(1)
'Get the running word application
Set wApp = GetObject(, "Word.Application")
'get report name to be updated
nome_report = bmk.Range("project_number").Value & "_" & bmk.Range("report_number") & "_01 Overview.docx"
'select the correct, already open, word document
Set wDoc = wApp.Documents(nome_report)
Debug.Print wDoc.Bookmarks.Count
i = 1
With wDoc
For i = 1 To .Bookmarks.Count
For Each nm In bmkwb.Names
If .Bookmarks(i).Name = nm.Name Then
'On Error Resume Next
UpdateBookmark wDoc, .Bookmarks(i), nm.RefersToRange.Value
'On Error GoTo 0
Exit For
End If
Next nm
Next i
.Fields.Update
End With
Debug.Print wDoc.Bookmarks.Count
End Sub
Sub UpdateBookmark(wDoc As Word.Document, b As Word.Bookmark, TextToUse As String)
Dim BMRange As Word.Range
Dim BMName As String
Dim n_bmks As Integer
n_bmks = wDoc.Bookmarks.Count
Set BMRange = b.Range
BMName = b.Name
BMRange.Text = TextToUse
wDoc.Bookmarks.Add BMName, BMRange
If n_bmks <> wDoc.Bookmarks.Count Then
Debug.Print BMName, BMRange
Debug.Print wDoc.Bookmarks.Count, "Bookmark lost"
End If
End Sub
Thanks in advance!提前致谢!
You refer to bookmarks «called "client_address2" (of 2) and "test_name2" (of 3)».您指的是书签«称为“client_address2”(2 个)和“test_name2”(3 个)»。 I assume these are named "client_address1", "client_address2", "test_name1", "test_name2", and "test_name3", as Word documents cannot have two or more bookmarks with the same name.我假设它们被命名为“client_address1”、“client_address2”、“test_name1”、“test_name2”和“test_name3”,因为 Word 文档不能有两个或多个同名的书签。
Are any bookmarks contained within or overlapping another bookmark?是否有任何书签包含在另一个书签内或与另一个书签重叠? if so, that might result in your 'For i = 1 To.Bookmarks.Count' crashing out if those embedded/overlapping bookmarks longer exist.如果是这样,如果那些嵌入/重叠的书签不再存在,这可能会导致您的“For i = 1 To.Bookmarks.Count”崩溃。 As for:至于:
i = 1
With wDoc
For i = 1 To .Bookmarks.Count
For Each nm In bmkwb.Names
If .Bookmarks(i).Name = nm.Name Then
'On Error Resume Next
UpdateBookmark wDoc, .Bookmarks(i), nm.RefersToRange.Value
'On Error GoTo 0
Exit For
End If
Next nm
Next i
.Fields.Update
End With
that could all be reduced to:这都可以简化为:
With wDoc
For Each nm In bmkwb.Names
If .Bookmarks.Exists(nm.Name).Name Then _
Call UpdateBookmark(wDoc, .Bookmarks(nm.Name), nm.RefersToRange.Value)
Next nm
.Fields.Update
End With
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.