前段时间,我用vba创建了这个宏,它在Office 2003中运行得很好。只要你不修改Word文件,它仍然可以在Office 2021中运行。如果我更改它,它就无法再看到要替换的书签。微软 Office 之谜。需要强调的是,我必须更换多个(100 个或更多)书签。
一个例子:
Sub MA01()
'
' MA01 Macro
' Macro registrata il 18/12/2015 da Esterni
'
Sheets("Lineare").Select 'go to a specific excel sheet where are the imputs
' get all the imputs from the cell (true or false value) to check or uncheck a specific formfield
Cross01 = Range("C669").Value ' get first value to check or uncheck a specific formfield after
Cross02 = Range("C668").Value ' get second value to check or uncheck a specific formfield after
'continue and so on
' get all the imputs from the cell to replace after a specific bookmark
Stringa001 = Range("C650").Value 'get first value to replace after a specific bookmark
Stringa002 = Range("C28").Value 'get second value to replace after a specific bookmark
'continue and so on
' open a specific file word to make the change after
Const sFILENAME As String = "C:\test\test.doc" ' file name here
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open(sFILENAME)
With wrdDoc
' check or uncheck a specific formfield
.FormFields("Controllo01").CheckBox.Value = Cross01
.FormFields("Controllo02").CheckBox.Value = Cross02
'continue and so on
' replace a specific Bookmark
.Bookmarks("X001").Range.Text = Stringa001
.Bookmarks("X002").Range.Text = Stringa002
'continue and so on
' go to a spefific sheet of excel and into a specific cell
Sheets("Ins Dati").Select
Range("Q68").Select
End With
MsgBox ("OK !!!! Document created")
End Sub
我发现我插入的书签在新版本中无法被VBA识别。 我使用了旧书签,但由于未知原因,VBA 在最新版本的 Office 中看不到它们。
为了确保在最新版本的 Office 中识别书签,您需要从下拉菜单插入 - 书签 (ICON) 中插入书签,然后添加书签, VBA 可以看到该书签,而通过下拉菜单开发遗留模块(图标)插入的其他书签则不起作用。
新版office对我来说是个大BUG。 (我花了很多时间试图弄清楚到底发生了什么)感谢 MS 让我的生活变得复杂。