有没有一种方法可以使用VBA for Word将书签附加到页眉中的字符串



我处理了您的所有评论,并根据以下内容修改了我的代码:

子页眉页脚宏((

'Delete the existing Header and Footer
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Delete
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Delete

Dim headerRange As Range
Set headerRange = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
With headerRange

.ParagraphFormat.Alignment = wdAlignParagraphRight
.Bookmarks.Add Range:=headerRange, Name:="DocID"
.Bookmarks.Add Range:=headerRange, Name:="UnderScore1"
.Bookmarks.Add Range:=headerRange, Name:="DocName"
.Bookmarks.Add Range:=headerRange, Name:="UnderScore2"
.Bookmarks.Add Range:=headerRange, Name:="DocVersion"
.InsertAfter Text:=vbTab & vbTab & "Page "
.Collapse wdCollapseEnd
.Fields.Add Range:=headerRange, Type:=wdFieldEmpty, Text:= _
"PAGE  * Arabic "
.End = .Paragraphs(1).Range.End
.Collapse wdCollapseEnd
.InsertAfter Text:=" of "
.Collapse wdCollapseEnd
.Fields.Add Range:=headerRange, Type:=wdFieldEmpty, Text:= _
"NUMPAGES"

End With

'Show the Form
VersionForm.Show

表格:私有子OKBtn_Click((

Dim DocName, DocVersion, DocID, UnderScore1, UnderScore2 As Range
Set DocID = ActiveDocument.Bookmarks("DocID").Range
Set UnderScore1 = ActiveDocument.Bookmarks("UnderScore1").Range
Set DocName = ActiveDocument.Bookmarks("DocName").Range
Set UnderScore2 = ActiveDocument.Bookmarks("UnderScore2").Range
Set DocVersion = ActiveDocument.Bookmarks("DocVersion").Range

DocID.Text = Me.TextBox1.Value
UnderScore1.Text = "_"
DocName.Text = Me.TextBox2.Value
UnderScore2.Text = "_"
DocVersion.Text = Me.TextBox3.Value
Me.Repaint
VersionForm.Hide

结束子

请找到下面的屏幕截图:

Scrreenshot

我仍然得到";DocVersion";一开始但是,我需要它的格式:"DocID"下芯1";DocName";下芯2";DocVersion。请帮忙。

非常感谢您的帮助:(

问题的快速解决方法只是更改代码的顺序。

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 = wdSeekCurrentPageHeader
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
'Insert the Header | Top Left
With ActiveDocument.Bookmarks
.add Range:=Selection.Range, name:="DocID"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
'Selection.HomeKey Unit:=wdLine
Selection.TypeText text:="_DocName_V."
With ActiveDocument.Bookmarks
.add Range:=Selection.Range, name:="DocVersion"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.TypeText text:=vbTab & vbTab
Selection.TypeText text:="Page "
Selection.Fields.add Range:=Selection.Range, Type:=wdFieldEmpty, text:= _
"PAGE  * Arabic ", PreserveFormatting:=True
Selection.TypeText text:=" of "
Selection.Fields.add Range:=Selection.Range, Type:=wdFieldEmpty, text:= _
"NUMPAGES  ", PreserveFormatting:=True

但是,您的代码确实需要重写,以避免使用Selection对象。例如:

Sub AddTextToHeaderRange()
Dim headerRange As Range

Set headerRange = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
With headerRange
.ParagraphFormat.Alignment = wdAlignParagraphRight
.Bookmarks.add Range:=headerRange, name:="DocID"
.text = "_DocName_V."
.Collapse wdCollapseEnd
.Bookmarks.add Range:=headerRange, name:="DocVersion"
.InsertAfter text:=vbTab & vbTab & "Page "
.Collapse wdCollapseEnd
.Fields.add Range:=headerRange, Type:=wdFieldEmpty, text:= _
"PAGE  * Arabic "
.End = .Paragraphs(1).Range.End
.Collapse wdCollapseEnd
.InsertAfter text:=" of "
.Collapse wdCollapseEnd
.Fields.add Range:=headerRange, Type:=wdFieldEmpty, text:= _
"NUMPAGES"
End With

End Sub

因修改问题而编辑:如果您要使用您在修改问题中发布的代码,您只需要1个书签,而不是5个。

然后,您将使用以下代码来填充文本:

DocID.Text = Me.TextBox1.Value & "_" & Me.TextBox2.Value & "_" Me.TextBox3.Value

您的代码没有显示页码是如何插入的。我的猜测是,它的设置有正确的理由。

您使用固定的docID而不是FileName字段是有原因的吗?

您可以将包含所需内容(即书签和页面字段(的页眉保存为包含代码的模板中的页眉构建块或自动图文集,并使用代码插入比使用代码构建更容易。这允许格式化,这可能是棘手的代码。

以下是我关于如何做到这一点的信息:http://www.addbalance.com/usersguide/autotextautocorrect.htm#UsingVBABuildingBlock

"录制宏在一定程度上是可行的。录制的宏不会在其他人的系统上工作,即使他们有相同的构建块,或者即使他们是同一台计算机上的不同用户。如果模板包含多个具有相同名称的构建块,则录制的宏将不起作用,即使它们是不同的类型。请注意,Word";内置积木.dotx";有许多具有相同名称的构建块。您可以使用构建块管理器并按名称排序,以查看同一模板中是否有多个具有相同名称的构建块。

由于这些限制,您通常需要实际编写宏。

但是,您可以尝试录制一个。关键是在录制宏时使用构建块管理器插入构建块,而不是使用快捷方式或其他库/菜单。

任何记录或写入的宏都应存储在包含构建块的同一模板中。这样,如果宏可用,则构建块可用。

  • 请注意,原件比可以在这里发布的要长,并且包含屏幕截图。它还包括示例宏

最新更新