Word 宏可使用文本框查找和替换 Word 文档中的所有内容 [英] Word Macro to find and replace all in word document with textboxes

查看:397
本文介绍了Word 宏可使用文本框查找和替换 Word 文档中的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个 VBA Word 宏,该宏将执行查找和替换操作,以将一种字体中出现的所有文本更改为另一种字体.我拥有的代码(如下所列)执行此操作,但忽略了文档中文本框中的所有文本.我如何修改此宏以搜索文档中文本框内外的所有文本(页眉和页脚将是一个加号,但不是绝对必要的),或者在宏中以不同的方式执行此操作.此宏是处理数以万计文档的更大宏的一部分,因此无法手动执行任何操作.

I need to write a VBA Word macro that will do a find and replace to change all occurrences of text in one font to another font. The code I have (listed below) does this but in ignores all the text in text boxes in the document. How do I either modify this macro to search all text both inside and outside textboxes in the document (headers and footers would be a plus but not absolutely necessary) or do it a different way in a macro. This macro is part of a larger macro that processes tens of thousands of documents so doing anything manually isn't an option.

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Font.Name = "PPalotina2007"
    .Replacement.Font.Name = "Palotina X"
End With
Selection.Find.Execute Replace:=wdReplaceAll

推荐答案

http 上找到了这个://word.mvps.org/faqs/customization/ReplaceAnywhere.htm 我应该注意这仅适用于每种类型的故事的第一个......提供的链接上有更好的代码用于获取所有故事范围.

Found this at http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm I should note this only works on the FIRST of each type of Story... There are better code on the link provided for getting to all story ranges.

Sub FindAndReplaceFirstStoryOfEachType()
  Dim rngStory As Range
  For Each rngStory In ActiveDocument.StoryRanges
    With rngStory.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Font.Name = "PPalotina2007"
        .Replacement.Font.Name = "Palotina X"
    End With
    rngStory.Find.Execute Replace:=wdReplaceAll
  Next rngStory
End Sub 

这篇关于Word 宏可使用文本框查找和替换 Word 文档中的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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