Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用VBA格式化Word页脚中的第XX页(共XX页)时出现问题_Vba_Ms Word - Fatal编程技术网

使用VBA格式化Word页脚中的第XX页(共XX页)时出现问题

使用VBA格式化Word页脚中的第XX页(共XX页)时出现问题,vba,ms-word,Vba,Ms Word,我正在通过代码构建我的页脚。当通过VBA添加字段时,我在正确格式化Word页脚中的页数时遇到问题。当代码运行时,格式总是以XX/XX(20中的1)而不是XX/XX(20中的1)结束。我尝试过以下方法,但数字总是显示页码,在单词“of”之前没有空格 还是这个 With rng .Text = "PAGE of NUMPAGES " Set oFooterRng1 = rng.Words(1) Set oFooterRng2

我正在通过代码构建我的页脚。当通过VBA添加字段时,我在正确格式化Word页脚中的页数时遇到问题。当代码运行时,格式总是以XX/XX(20中的1)而不是XX/XX(20中的1)结束。我尝试过以下方法,但数字总是显示页码,在单词“of”之前没有空格

还是这个

    With rng
        .Text = "PAGE of NUMPAGES "
        Set oFooterRng1 = rng.Words(1)
        Set oFooterRng2 = rng.Words(3)
        .Fields.Add Range:=oFooterRng1, Type:=wdFieldEmpty, Text:="PAGE  \* Arabic ", PreserveFormatting:=True
        .Fields.Add Range:=oFooterRng2, Type:=wdFieldEmpty, Text:="NUMPAGES  \* Arabic ", PreserveFormatting:=True
    End With

尝试以下内容:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
  .InsertAfter Text:="Page "
  .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="PAGE", PreserveFormatting:=False
  .InsertAfter Text:=" of "
  .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="NUMPAGES", PreserveFormatting:=False
End With
Application.ScreenUpdating = True
End Sub
而且,在你试图实现的过程中,反过来做:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
  .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="NUMPAGES", PreserveFormatting:=False
  .Collapse wdCollapseStart
  .Text = " of "
  .Collapse wdCollapseStart
  .Fields.Add Range:=.Duplicate, Type:=wdFieldEmpty, Text:="PAGE", PreserveFormatting:=False
  .Collapse wdCollapseStart
  .Text = "Page "
End With
Application.ScreenUpdating = True
End Sub

尝试以下内容:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
  .InsertAfter Text:="Page "
  .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="PAGE", PreserveFormatting:=False
  .InsertAfter Text:=" of "
  .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="NUMPAGES", PreserveFormatting:=False
End With
Application.ScreenUpdating = True
End Sub
而且,在你试图实现的过程中,反过来做:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
  .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="NUMPAGES", PreserveFormatting:=False
  .Collapse wdCollapseStart
  .Text = " of "
  .Collapse wdCollapseStart
  .Fields.Add Range:=.Duplicate, Type:=wdFieldEmpty, Text:="PAGE", PreserveFormatting:=False
  .Collapse wdCollapseStart
  .Text = "Page "
End With
Application.ScreenUpdating = True
End Sub

是否值得尝试在字符串中添加空格字符?e、 g.
rng.Text=Chr(32)和“of”
谢谢您的建议,但这不起作用。是否值得尝试在字符串中添加空格字符?e、 g.
rng.Text=Chr(32)和“of”
感谢您的建议,但这不起作用。页脚是反向构建的,这就是我使用rng.collapsestart语法的原因。我已经尝试了你的建议,并将其构建为2001页。我也尝试过反向构建它。当你使用我发布的代码时,它不会这样做——就像我发布的一样。为什么要反向构建页脚?无论如何,我已经为此添加了代码。谢谢。这很有效。我想我的问题是设置单词的范围,而不是使用字符。谢谢页脚是反向构建的,这就是我使用rng.collapseStart语法的原因。我已经尝试了你的建议,并将其构建为2001页。我也尝试过反向构建它。当你使用我发布的代码时,它不会这样做——就像我发布的一样。为什么要反向构建页脚?无论如何,我已经为此添加了代码。谢谢。这很有效。我想我的问题是设置单词的范围,而不是使用字符。谢谢