【问题标题】:Editing a specific footer column in Word with VBA使用 VBA 在 Word 中编辑特定的页脚列
【发布时间】:2020-10-10 21:23:37
【问题描述】:

我正在尝试在 Word 中编辑三栏页脚的右侧栏。我可以很好地编辑整个页脚,但我想保持前两列不变。

Public WithEvents App As Word.Application

Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
     'ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = "my value"
End Sub

我见过各种编辑页眉和页脚的解决方案,但没有一个只编辑单独页脚的特定部分。 我开始怀疑页脚是否只是被定义为一个无法通过左右分隔部分区分的大字段。

有谁知道这是否可行?

【问题讨论】:

  • 页脚没有列。一些模板创建者会在页脚添加一个表格,将其划分为类似于列的部分。
  • 您需要确定是什么分隔了文本。文本可以在表格中,也可以简单地用制表符分隔。单击主页选项卡上的显示/隐藏按钮以显示非打印字符。
  • 我是这么怀疑的。那么,似乎做我想做的唯一方法就是识别这些“列”之间的分隔字符。不幸的是,这已经成为它自己的另一个问题。 “显示格式”选项显示这些是制表符的所有指示,制表位分别设置为 3.25 和 6.5,但搜索工具不会将它们识别为这样。好像它们是他们自己的独特角色,但我不知道如何识别它们以便通过搜索功能找到它们。
  • @ConnorKurschat - 回复评论类型 @ 后跟用户名时,因为这会通知他们您的评论。

标签: vba ms-word


【解决方案1】:

下面的代码应该对您有所帮助。只需取消注释您要编辑的页脚部分的相关代码块

Sub EditFooterText(ByVal Doc As Document, NewText As String)
   Dim footerRange As Range
   Dim leftTab As Long, rightTab As Long, footerLength As Long
   Set footerRange = Doc.Sections(1).Footers(wdHeaderFooterPrimary).Range
   With footerRange
      'find the tabs
      leftTab = InStr(.Text, vbTab)
      rightTab = Len(.Text) - InStrRev(.Text, vbTab)
      
      'to edit the left text
'      .Collapse wdCollapseStart
'      .MoveEnd wdCharacter, leftTab - 1
      
      'to edit the centre text
'      .Collapse wdCollapseStart
'      .Move wdCharacter, leftTab
'      .MoveEnd wdCharacter, rightTab
      
      'to edit the right text
      .Collapse wdCollapseEnd
      .MoveStart wdCharacter, (0 - (rightTab - 1))
      
      .Text = NewText
   End With
End Sub

如果您从 BeforeSave 事件中调用它,您可以使用以下内容:

Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
     EditFooterText Doc, "my value"
End Sub

如果页脚由对齐选项卡分隔,它看起来与打开显示/隐藏的普通选项卡相同,但具有不同的字符代码,那么您可以使用以下例程:

Sub EditFooterTextWithAlignmentTabs(ByVal Doc As Document, NewText As String)
   Dim footerRange As Range
   Dim leftTab As Long, rightTab As Long, footerLength As Long
   Set footerRange = Doc.Sections(1).Footers(wdHeaderFooterPrimary).Range
   With footerRange
      'find the tabs
      leftTab = InStr(.Text, Chr(48))
      rightTab = Len(.Text) - InStrRev(.Text, Chr(48))
      
      Debug.Print leftTab
      Debug.Print rightTab
      
      'to edit the left text
'      .Collapse wdCollapseStart
'      .MoveEnd wdCharacter, leftTab - 1
      
      'to edit the centre text
'      .Collapse wdCollapseStart
'      .Move wdCharacter, leftTab
'      .MoveEnd wdCharacter, rightTab
      
      'to edit the right text
      .Collapse wdCollapseEnd
      .MoveStart wdCharacter, (0 - (rightTab - 1))
      
      .Text = NewText
   End With
End Sub

您可以通过在即时窗口中输入?AscW(Selection.Text) 来获取所选字符的 unicode 值

【讨论】:

    猜你喜欢
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 2013-09-25
    • 2014-01-08
    • 2019-08-26
    • 1970-01-01
    • 2020-12-24
    相关资源
    最近更新 更多