【问题标题】:Word VBA: find line and replace fontWord VBA:查找行并替换字体
【发布时间】:2013-11-09 21:35:59
【问题描述】:

我编写了一个读取.txt 文件的 VBA Word 宏,将其复制并粘贴到设置新字体的 Word 文档中。

一切正常!现在我想用bold + italic 字体突出显示一些特定的行,但我想不出一个可行的解决方案。

特定行以特定单词开头(例如Simulation Nr.xxx),或者它们以某些单词开头但随后有很长的一系列空格(例如Turbine)。

我该如何解决这个问题?


P.s.:这里是将 .txt 文件复制/粘贴到 word 文档中的工作代码。

Sub ACTUS_Table_Converter()

Dim pName As String
Dim bDoc As Document
Dim AppPath, ThisPath As String
Dim Rng As Range

ThisPath = ActiveDocument.Path
pName = ActiveDocument.Name

With Dialogs(wdDialogFileOpen)
    If .Display Then
        If .Name <> "" Then
            Set bDoc = Documents.Open(.Name)
            AppPath = bDoc.Path
        End If
    Else
        MsgBox "No file selected"
    End If
End With

Call ReplaceAllxSymbolsWithySymbols
Call ChangeFormat

Selection.Copy
Windows(pName).Activate
Selection.Paste
Selection.Collapse
bDoc.Close savechanges:=False

End Sub

Sub ChangeFormat()

Selection.WholeStory
With Selection.Font
    .Name = "Courier New"
    .Size = 6
End With

End Sub

Sub ReplaceAllxSymbolsWithySymbols()

'Call the main "ReplaceAllSymbols" macro (below),
'and tell it which character code  and font to search for, and which to replace with

Call ReplaceAllSymbols(FindChar:=ChrW(-141), FindFont:="(normal text)", _
        ReplaceChar:=ChrW(179), ReplaceFont:="(normal text)")
Call ReplaceAllSymbols(FindChar:=ChrW(-142), FindFont:="(normal text)", _
        ReplaceChar:=ChrW(178), ReplaceFont:="(normal text)")
Call ReplaceAllSymbols(FindChar:=ChrW(-144), FindFont:="(normal text)", _
        ReplaceChar:=ChrW(176), ReplaceFont:="(normal text)")
Call ReplaceAllSymbols(FindChar:="°", FindFont:="(normal text)", _
        ReplaceChar:="", ReplaceFont:="(normal text)")

End Sub

Sub ReplaceAllSymbols(FindChar As String, FindFont As String, _
    ReplaceChar As String, ReplaceFont As String)

Dim FoundFont As String, OriginalRange As Range, strFound As Boolean
Application.ScreenUpdating = False

Set OriginalRange = Selection.Range
'start at beginning of document
ActiveDocument.Range(0, 0).Select

strFound = False
If ReplaceChar = "" Then
With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = FindChar
    .Replacement.Text = ReplaceChar
    .Replacement.Font.Name = "Courier New"
    .Replacement.Font.Size = 6
    .MatchCase = True
End With
If Selection.Find.Execute Then
    Selection.Delete Unit:=wdCharacter, Count:=2
    Selection.TypeText ("°C")
End If
Else
With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = FindChar
    .Replacement.Text = ReplaceChar
    .Replacement.Font.Name = "Courier New"
    .Replacement.Font.Size = 6
    .MatchCase = True
    .Execute Replace:=wdReplaceAll
End With
End If

OriginalRange.Select

Set OriginalRange = Nothing
Application.ScreenUpdating = True

Selection.Collapse

End Sub

【问题讨论】:

    标签: vba replace fonts ms-word format


    【解决方案1】:

    下面的代码应该在文档上运行,寻找以Simulation Nr. 开头的行并将整个行字体替换为粗体和斜体。

    Sub ReplaceLinesStartWith()
    
    Dim startingWord As String
    'the string to search for
    startingWord = "Simulation Nr."
    
    Dim myRange As range
    'Will change selection to the document start
    Set myRange = ActiveDocument.range(ActiveDocument.range.Start, ActiveDocument.range.Start)
    myRange.Select
    
    While Selection.End < ActiveDocument.range.End
       If Left(Selection.Text, Len(startingWord)) = startingWord Then
            With Selection.Font
                .Bold = True
                .Italic = True
            End With
        End If
    
        Selection.MoveDown Unit:=wdLine
        Selection.Expand wdLine
    
    Wend
    
    End Sub
    

    请注意,我对要搜索的字符串进行了硬编码,您可以将其设置为函数参数。

    【讨论】:

    • 谢谢!这对我有帮助,但不要解决第二种情况,即随机单词加上许多空格的情况。我该如何解决?我可能不得不使用某种通配符,但我不知道怎么说搜索“未知单词+空格”。你可能知道吗?提前致谢。 MLC
    • 您应该清楚地定义您要搜索的内容。什么被认为是“未知词”?多少个空格?有规律吗?
    • 我有以下问题:我想用粗体突出显示一个带有一些标题的结构化文本。这些标题很多,我无法为每个标题定义查找和替换,但我知道如果该行中有一个标题,那么该行将在标题词之后至少有 25 个空格。我如何搜索并找到这些行并将其字体更改为粗体?谢谢
    猜你喜欢
    • 2018-03-24
    • 2012-03-08
    • 1970-01-01
    • 2014-10-12
    • 2014-04-06
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 2014-07-15
    相关资源
    最近更新 更多