[英]Update Word bookmarks with formatted Excel data from Excel
The following code is intended to update Word bookmarks with formatted data from Excel, however the formatting doesn't come across and unsure why, would appreciate any suggestions.以下代码旨在使用来自 Excel 的格式化数据更新 Word 书签,但是格式没有出现并且不确定原因,希望得到任何建议。 The formatted data is text with certain works underlined.
格式化数据是带有下划线的某些作品的文本。
Set wb = ActiveWorkbook
TodayDate = Format(Date, "mmmm d, yyyy")
Path = wb.Path & "\update_file.docx"
'Create a new Word Session
Set pappWord = CreateObject("Word.Application")
'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
docWord.Bookmarks(xlName.Name).Range.Text = Range(xlName).Text
End If
Next xlName
Try this instead, using Copy
and the (poorly-documented) ExecuteMso
method.试试这个,使用
Copy
和(记录不佳的) ExecuteMso
方法。 You need to use Copy
against the range (in order to capture formatting) and then you can effectively do the same as the right-click Paste + Keep Source Formatting option:您需要对范围使用
Copy
(为了捕获格式),然后您可以有效地执行与右键单击Paste + Keep Source Formatting选项相同的操作:
If docWord.Bookmarks.Exists(xlName.Name) Then
xlName.RefersToRange.Copy
docWord.Bookmarks(xlName.Name).Select
docWord.Application.CommandBars.ExecuteMso "PasteSourceFormatting"
End If
Alternatively, and this might be better because ExecuteMso
is asynchronous and can result in timing issues:或者,这可能会更好,因为
ExecuteMso
是异步的并且可能导致计时问题:
xlName.RefersToRange.Copy
docWord.Bookmarks(xlName.Name).Range.PasteAndFormat 16 'wdFormatOriginalFormatting
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.