使用VBA在Word中设置嵌套字段 [英] Setting up a nested field in Word using VBA

查看:355
本文介绍了使用VBA在Word中设置嵌套字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从模板构建一个大文件。每个模板在页脚中都有一个关键字 #OVERALLPAGENUMBER#,我以编程方式用一个字段替换(使用Excel VBA)。



如果我需要的是该文档的页码,则满足以下条件:

  Dim storyRange As Object'Word.Range 
对于每个storyRange在oDoc.StoryRanges
Do
With storyRange.Find
.Text =#OVERALLPAGENUMBER#
.Wrap = 1'wdFindContinue
.Execute
虽然.found
storyRange.Fields.Add范围:= storyRange,Type:= - 1,Text:=PAGE,PreserveFormatting:= True
.Execute
Wend
结束
错误恢复下一步
设置storyRange = storyRange.NextStoryRange
错误GoTo 0
循环而不是storyRange是没有
下一个

我已经测试了这段代码,并且成功地将pag e数字在页脚。然而,我想要的是一个嵌套(公式)字段,它为页码添加了一个固定数字,以便我可以跨多个文档显示页数。我的解决方案,如果我手动(使用Ctrl + F9),给出字段代码如下所示:

  {= 5 + {PAGE}} 

正确生成第1页上的6,第2页上的7 ,等...



无论我尝试什么,我无法使用VBA复制这种字段嵌套。 (宏记录器在这里没用)。任何人都可以通过编程方式找到创建这些字段的方法?






解决方案



我的问题是,有了 PreserveFormatting:= True 正阻碍我将一个字段嵌套在另一个字段中。现在以下简单的解决方案有效:

 使用storyRange.Find 
.Text =#POLICYPAGENO#
.Wrap = 1'wdFindContinue
.Execute
虽然.found
storyRange.Select
带有oDoc.ActiveWindow
.Selection.Fields.Add范围:=。选择.Range,Type:= - 1,Text:=PAGE,PreserveFormatting:= False
.Selection.MoveLeft单位:= 1,计数:= 1,扩展:= 1
.Selection.Fields .Add Range:=。Selection.Range,Type:= - 1,PreserveFormatting:= False
.Selection.TypeText Text:==&总页数+
结束
.Execute
Wend
结束


解决方案

我很确定您的查找和替换方法会每次选择文本 #OVERALLPAGENUMBER#循环通过。如果是这样,您可以替换 storyRange.Fields.Add Range:= storyRange,Type:= - 1,Text:=PAGE,PreserveFormatting:= True 以下,这将简单地将您所需的字段代码放在当前选择:

  Selection.Fields.Add Range:= Selection.Range ,Type:= wdFieldEmpty,PreserveFormatting:= False 
Selection.TypeText Text:== 5 +
Selection.Fields.Add范围:= Selection.Range,Type:= wdFieldEmpty,PreserveFormatting:= False
Selection.TypeText文本:=PAGE
Selection.Fields.Update

编辑:以前的代码仅在 PreserveFormatting 设置为 False 时有效。如果 PreserveFormatting 设置为 True ,则Word会更新一个空字段代码。您可以浏览选择以使用以下格式保留格式。您应该只需要外部字段的 PreserveFormatting

 选择。 Fields.Add Range:= Selection.Range,Type:= wdFieldEmpty,PreserveFormatting:= True 
Selection.MoveRight单位:= wdCharacter,Count:= 1,Extend:= wdExtend
Selection.Fields.ToggleShowCodes
Selection.MoveLeft单位:= wdCharacter,Count:= 1
Selection.MoveRight单位:= wdCharacter,Count:= 1
Selection.TypeText Text:== 5 +
Selection.Fields.Add Range:= Selection.Range,Type:= wdFieldEmpty,PreserveFormatting:= False
Selection.TypeText Text:=PAGE
Selection.Fields.Update


I am building a large document in pieces from templates. Each template has a keyword #OVERALLPAGENUMBER# in the footer which I am programmatically replacing (using Excel VBA) with a field.

If all I needed were that document's page number, the following would suffice:

Dim storyRange As Object 'Word.Range
For Each storyRange In oDoc.StoryRanges
    Do
        With storyRange.Find
            .Text = "#OVERALLPAGENUMBER#"
            .Wrap = 1 'wdFindContinue
            .Execute
            While .found
                storyRange.Fields.Add Range:=storyRange, Type:=-1, Text:="PAGE", PreserveFormatting:=True
                .Execute
            Wend
        End With
        On Error Resume Next
        Set storyRange = storyRange.NextStoryRange
        On Error GoTo 0
    Loop While Not storyRange Is Nothing
Next

I've tested this code, and it successfully puts the page number in the footer. What I want, however, is a nested (formula) field which adds a fixed number to the page number so that I can display a page count across multiple documents. My solution, if I do it manually (using Ctrl+F9), gives field codes that look like this:

{ = 5 + { PAGE } }

And correctly produces "6" on page 1, "7" on page 2, etc...

No matter what I try though, I cannot replicate this sort of field nesting using VBA. (Macro recorder is useless here). Can anyone find a way to create these fields programmatically?


Solution

My problem was that having PreserveFormatting:=True was getting in the way of my attempts to nest one field within the other. Now the following simple solution works:

With storyRange.Find
    .Text = "#POLICYPAGENO#"
    .Wrap = 1 'wdFindContinue
    .Execute
    While .found
        storyRange.Select
        With oDoc.ActiveWindow
            .Selection.Fields.Add Range:=.Selection.Range, Type:=-1, Text:="PAGE", PreserveFormatting:=False
            .Selection.MoveLeft Unit:=1, Count:=1, Extend:=1
            .Selection.Fields.Add Range:=.Selection.Range, Type:=-1, PreserveFormatting:=False
            .Selection.TypeText Text:="= " & OverallPageNumber & " +"
        End With
        .Execute
    Wend
End With

解决方案

I'm pretty sure that your method of Find and Replacing will Select the text #OVERALLPAGENUMBER# each time it loops through. If that is the case you can replace storyRange.Fields.Add Range:=storyRange, Type:=-1, Text:="PAGE", PreserveFormatting:=True with the following, which will simply place your required field code at the current selection:

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, PreserveFormatting:=False
Selection.TypeText Text:="= 5 +"
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, PreserveFormatting:=False
Selection.TypeText Text:="PAGE"
Selection.Fields.Update

Edit: The previous code only works if PreserveFormatting is set to False. It appears that Word updates an empty field code if PreserveFormatting is set to True. You can navigate the selection to keep the formatting with the following. You should only need to PreserveFormatting for the outer field.

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, PreserveFormatting:=True
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Fields.ToggleShowCodes
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="= 5 +"
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, PreserveFormatting:=False
Selection.TypeText Text:="PAGE"
Selection.Fields.Update

这篇关于使用VBA在Word中设置嵌套字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆