我希望设置一个邮件合并(目录合并以使用分页符)模板,然后让用户在创建文件后使用宏来生成目录。
我尝试记录宏以转到文档中的第一个空格,添加分节符,然后插入目录并更改编号,以使其不包括第一页,但是我看到当使用宏时,它会加载构建模块模板。
我意识到这很麻烦,因为它已被记录下来,并且找到了一些更简洁的代码,但是它没有TOC的标头。
有没有办法让使用该代码的任何用户都能完成这项工作?
nginx -t
尝试了以下代码,但仍使用mypath,该路径必须是当前用户的路径。每个用户的其余路径是否相同?然后也许我可以以某种方式提取当前用户的用户名并将其插入myUsername中。
Sub Macro1()
'
' Macro1 Macro
'
Set myTemplate = ActiveDocument.AttachedTemplate
Selection.HomeKey Unit:=wdStory
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.HomeKey Unit:=wdStory
Application.Templates( _
"C:\Users\myUsername\AppData\Roaming\Microsoft\Document Building Blocks\1033\16\Built-In Building Blocks.dotx" _
).BuildingBlockEntries("Automatic Table 1").Insert Where:=Selection.Range _
, RichText:=True
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
答案 0 :(得分:0)
由于要创建要分发的模板,因此将Building Block存储在该模板中,而不要存储在“私有” Building Block模板中。然后,使用该模板的任何人都可以使用它。
将问题中的代码更改为:
Sub Macro1()
'
' Macro1 Macro
'
Set myTemplate = ActiveDocument.AttachedTemplate
Selection.HomeKey Unit:=wdStory
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.HomeKey Unit:=wdStory
myTemplate.BuildingBlockEntries("Automatic Table 1").Insert _
Where:=Selection.Range _
, RichText:=True
'Note: the macro probably does not need the rest of the lines of code
'that were generated by the macro recorder - comment them out or delete them
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
答案 1 :(得分:0)
我继续以下操作,因为“自动表1”在其他用户的计算机上不可用:
Sub ToCAndTitle()
Dim tmptmplt As Template
With ActiveDocument
'Insert a Section break before existing content
.Range(0, 0).InsertBreak Type:=wdSectionBreakNextPage
'Insert a page break before existing content
.Range(0, 0).InsertBreak Type:=wdPageBreak
'Insert Table of contents on first page
.TablesOfContents.Add _
Range:=.Range(0, 0)
'Manually enter text and format as TOC Heading from default template that all users should have
.Range(0, 0).InsertAfter ("Table of Contents") & vbCr
.Range(0, 0).Style = ActiveDocument.Styles("TOC Heading")
'Different first page header to remove number from TOC page
.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = True
End With
End Sub