一般来说,问题是这样的:我编写了一个插入页眉和页脚的宏,并且它工作成功,但是在使用宏之前,您必须打开下拉菜单(插入页眉或插入页脚),这是唯一的方法宏将会起作用。如果刚刚创建了 .docx 文档,则在打开带有页眉和页脚的菜单之前,宏将不起作用。问题是,这个问题有没有解决方案,例如初始化页眉或页脚或加载页眉
Sub macr_horiz_A3()
Dim objTemplate As Template
Dim objBB As BuildingBlock
Dim folderPath As String
' Загружаем шаблон
folderPath = Environ("AppData") & "\Microsoft\Document Building Blocks\1049\16\Building Blocks.dotx"
Set objTemplate = Application.Templates(folderPath)
Set objBB = objTemplate.BuildingBlockEntries("a3horizontal")
' Сохраняем текущую позицию курсора
Dim currentPosition As Range
Set currentPosition = Selection.Range
' Меняем формат и колонтитул на текущей странице
currentPosition.Sections(1).Headers(wdHeaderFooterPrimary).Range.Delete
currentPosition.PageSetup.PaperSize = wdPaperA3
currentPosition.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = ""
objBB.Insert Where:=currentPosition.Sections(1).Headers(wdHeaderFooterPrimary).Range, RichText:=True
currentPosition.PageSetup.Orientation = wdOrientLandscape
End Sub