繁体   English   中英

VBA:格式化MS Word文本

[英]VBA: Format MS Word text

我正在尝试格式化多个单词的文本。 到目前为止,下面的代码将只允许我格式化一个单词的字体。 我需要添加/删除什么内容才能格式化输入的单词?

干杯!

Sub FnFindAndFormat()

    Dim objWord
    Dim objDoc
    Dim intParaCount
    Dim objParagraph
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Open("C:\USERPATH")
    objWord.Visible = True
    intParaCount = objDoc.Paragraphs.Count

    Set objParagraph = objDoc.Paragraphs(1).range
    objParagraph.Find.Text = "deal"

    Do
        objParagraph.Find.Execute
        If objParagraph.Find.Found Then
            objParagraph.Font.Name = "Times New Roman"
            objParagraph.Font.Size = 20
            objParagraph.Font.Bold = True
            objParagraph.Font.Color = RGB(200, 200, 0)
        End If


    Loop While objParagraph.Find.Found

End Sub

假设您的Word文档看起来像这样

在此处输入图片说明

由于我不确定您是从Word-VBA还是从其他应用程序(如Excel-VBA执行此操作,因此我同时包括了这两种方法。

现在,如果您是通过Word-VBA执行此操作,则无需使用LateBind。 使用此简单代码。

Option Explicit

Sub Sample()
    Dim oDoc As Document
    Dim MyAr() As String, strToFind As String
    Dim i As Long

    '~~> This holds your search words
    strToFind = "deal,contract, sign, award"

    '~~> Create an array of text to be found
    MyAr = Split(strToFind, ",")

    '~~> Open the relevant word document
    Set oDoc = Documents.Open("C:\Sample.docx")

    '~~> Loop through the array to get the seacrh text
    For i = LBound(MyAr) To UBound(MyAr)
        With Selection.Find
            .ClearFormatting
            .Text = MyAr(i)
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Execute

            '~~> Change the attributes
            Do Until .Found = False
                With Selection.Font
                    .Name = "Times New Roman"
                    .Size = 20
                    .Bold = True
                    .Color = RGB(200, 200, 0)
                End With
                Selection.Find.Execute
            Loop
        End With
    Next i
End Sub

但是,如果您说的是Excel-VBA使用此

Const wdFindContinue = 1

Sub FnFindAndFormat()
    Dim objWord As Object, objDoc As Object, Rng As Object
    Dim MyAr() As String, strToFind As String
    Dim i As Long

    '~~> This holds your search words
    strToFind = "deal,contract, sign, award"

    '~~> Create an array of text to be found
    MyAr = Split(strToFind, ",")

    Set objWord = CreateObject("Word.Application")
    '~~> Open the relevant word document
    Set objDoc = objWord.Documents.Open("C:\Sample.docx")

    objWord.Visible = True

    Set Rng = objWord.Selection

    '~~> Loop through the array to get the seacrh text
    For i = LBound(MyAr) To UBound(MyAr)
        With Rng.Find
            .ClearFormatting
            .Text = MyAr(i)
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Execute

            Set Rng = objWord.Selection

            '~~> Change the attributes
            Do Until .Found = False
                With Rng.Font
                    .Name = "Times New Roman"
                    .Size = 20
                    .Bold = True
                    .Color = RGB(200, 200, 0)
                End With
                Rng.Find.Execute
            Loop
        End With
    Next i
End Sub

输出值

在此处输入图片说明

对我来说就像一个魅力:

Public Sub Find_some_text()

'setting objects
Dim objWord
Dim objDoc
Dim objSelection

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("H:\Test.docx")

'set visibility
objWord.Visible = True

'set array of words to format
words_list = Array("Findme_1", "Findme_2", "etc")

'formatting text
For Each w In words_list
    Set Frange = objDoc.Range
    Frange.Find.Text = w
    Do
      Frange.Find.Execute
      If Frange.Find.Found Then
         Frange.Font.Name = "Times New Roman"
         Frange.Font.Size = 20
         Frange.Font.Bold = True
         Frange.Font.Color = RGB(200, 200, 0)
      End If
    Loop While Frange.Find.Found
Next

'de-set visibility
objWord.Visible = False

'saving (optional)
objDoc.Save

End Sub

这段代码:

For Each w In words_list
    Set Frange = objDoc.Range
    Frange.Find.Text = w
    Do
      Frange.Find.Execute
      If Frange.Find.Found Then
         Frange.Font.Name = "Times New Roman"
         Frange.Font.Size = 20
         Frange.Font.Bold = True
         Frange.Font.Color = RGB(200, 200, 0)
      End If
    Loop While Frange.Find.Found
Next

效率低下。 尝试:

With objDoc.Range.Find
  .ClearFormatting
  With .Replacement
    .ClearFormatting
    .Text = "^&"
    With .Font
      .Name = "Times New Roman"
      .Size = 20
      .Bold = True
      .Color = RGB(200, 200, 0)
    End With
  End With
  .Format = True
  .Forward = True
  .Wrap = 1 'wdFindContinue
  For Each w In words_list
    .Text = w
    .Execute Replace:=2 'wdReplaceAll
  Next
End With

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM