替换部分Word页脚文本,而不会丢失格式和活动对象 [英] Replace part of Word footer text without losing formatting and active objects

查看:80
本文介绍了替换部分Word页脚文本,而不会丢失格式和活动对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Word文档中维护标准页脚.页脚包含文档ID和版本,可以更改,因此我不仅必须能够插入此标准页脚,还可以对其进行更新.

I have requirement to maintain a standard footer in Word documents. The footer contains doc id and version, which can change, so I must be able not only insert this standard footer, but also update it.

我只需要识别页脚的标准字符串部分并对其进行更新.字符串看起来像<<Libray:DocId:vX>>(尖括号用于帮助匹配标准页脚的开头和结尾).

I need to identify just the standard string part of the footer and update it. The string looks like <<Libray:DocId:vX>> (the angle brackets are to help match the beginning and end of the standard footer).

一个令人担心的问题是,仅括号不足以可靠地识别标​​准字符串,因此我正在进行RegEx ("<<[A-Za-z0-9_-]+:\d+v\d+>>")匹配和替换以实现更新,但是似乎修改Text属性会擦除所有格式和活动内容.

A concern is that the brackets alone aren't enough to reliably identify the standard string, so I am doing a RegEx ("<<[A-Za-z0-9_-]+:\d+v\d+>>") match and Replace to achieve the update, but it seems modifying the Text property wipes out any formatting and active content.

如果我不能在不丢失格式的情况下对Text属性使用RegEx.Replace,是否还有另一种方法来仅标记和更新标准字符串?

If I can't use RegEx.Replace on the Text property w/o losing formatting, is there another way to tag and update just the standard string?

If oRegEx.Test(strFooter) Then
        ' Set the footer for the active document
        With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
            .Style = wdStyleFooter
            .Font.Size = 8
            .Text = oRegEx.Replace(strFooter, strTextString) 'this wipes out page numbering and formatting
        End With
    Else
        With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
            .Style = wdStyleFooter
            .Font.Size = 8
            .InsertAfter strJoiner & strTextString 'this preserves formatting and active content
        End With
    End If

推荐答案

您遇到的问题是oRegEx.Replace仅适用于文本内容.它不了解并且无法保留strFooter中可能存在的任何格式.第一段代码实际上是在用RegEx的结果替换页脚的全部内容.

The problem you're running into is that oRegEx.Replace will work only with text content. It has no knowledge of and cannot retain any formatting that might be present in strFooter. What the first block of code is essentially doing is replacing the entire content of the Footer with the result of the RegEx.

InsertAfter,则保留原始Range的内容不变.还有其他类似的方法-具体取决于您要做什么.

InsertAfter, on the otherhand, leaves the original Range's content intact. There are other, similar approaches - which is appropriate depends on exactly what you want to do.

由于您没有详细说明使用RegEx的原因,尽管您提到了查找",但很难提供其他有用的信息.作为Range对象的属性,Word确实具有自己的查找/替换"功能. Word的Find确实支持通配符,类似于RegEx.

Since you don't detail why you're using the RegEx, although you mention "find", it's difficult to give provide additional helpful information. Word does have its own Find/Replace functionality, as a property of the Range object. Word's Find does support Wildcards, which is similar to RegEx.

谢谢您的其他信息.在Word中,切换到页眉/页脚视图.按Ctrl + F以显示查找"功能.如果这将打开任务窗格,请单击下拉菜单右侧的箭头,然后选择高级查找"以获取完整的对话框.点击替换"标签.

thank you for the additional info. In Word, switch to the header/footer view. Press Ctrl+F to display the Find functionality. If this opens a task pane, click on the arrow at the right of the drop-down and choose "Advanced find" to get the full dialog box. Click the "Replace" tab.

激活通配符"复选框.在查找"框中,输入类似于以下内容的搜索代码,但将有效文本替换为您要在其上进行测试的文档:(\< \<)Library:DocId:vs#(>>)

Activate the "wildcards" checkbox. In the "Find" box enter a search code similar to the following, but substitute valid text for the document you're testing on: (\<\<)Library:DocId:vs#(>>)

在替换"框中:\ 1Abc:efg:vsn2 \ 2

In the Replace box: \1Abc:efg:vsn2\2

Find括号定义了表达式",可以在替换"中引用该表达式,以便保留此内容.以上结果应为<>.

The parentheses Find define "expressions" that can be referenced in Replace so that this content can be retained. The result of the above should be <>.

完成此操作后,将执行查找/替换"步骤的记录记录在宏中.这将为您提供在代码中使用的语法.

Once you have this working, record executing the Find/Replace steps in a macro. That will give you the syntax to use in your code.

代码的另一提示:如果整个页脚应具有不同的字体大小,则应更改页脚样式定义,而不是将其添加为直接格式.

An additional tip for your code: If the entire footer should have the different font size you should change the Footer style definition, rather than adding it as direct formatting.

这篇关于替换部分Word页脚文本,而不会丢失格式和活动对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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