0

这是我的 word 文档中的文本示例: https ://www.noelshack.com/2018-31-2-1533054408-word.png

我是 VBA 新手,我正在尝试编写一个宏来查找特定文本“”““合格货币”是指基础货币和此处指定的其他货币:“并替换以下两行(用一些点填充,不一定在同一段落中)带有文本列表(例如:欧元,美元)。

到目前为止,我已经能够遍历文档,找到特定的文本并使用以下代码对其进行编辑:

Sub FindAndFormat()
    Dim objWord As Object
    Dim wdDoc As Object
    Dim ParagraphRange As Object
    Dim intParaCount
    On Error Resume Next
    Set objWord = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
        Set objWord = CreateObject("Word.Application")
    End If
    On Error GoTo 0

    Set wdDoc = objWord.Documents.Open("D:\Modele.docx")
    objWord.Visible = True
    Dim Paragraph As Word.Paragraph
    For Each Paragraph In wdDoc.Paragraphs
        Set ParagraphRange = Paragraph.Range
        ParagraphRange.Find.Text = """Eligible Currency"" means the Base Currency and each other currency specified here:"
        ParagraphRange.Find.Execute
        If ParagraphRange.Find.Found Then
            ParagraphRange.Text = """Eligible Currency"" means the Base Currency and each other currency specified here: Euro, Dollar"
        End If
    Next
End Sub

请注意,整行的样式变得粗体和斜体。

https://www.noelshack.com/2018-31-2-1533055581-word2.png

我真正想要实现的是替换虚线:

https://www.noelshack.com/2018-31-2-1533055647-word3.png

现在我的文档中可能还有其他几条虚线,它们可能并不总是包含完全相同数量的点。

感谢您的阅读。

4

1 回答 1

0

尝试以下方式:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = """Eligible Currency""[!:]@:[ ….^13^l^t]{2,}"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    .End = .End - 1
    .Start = .Start + InStr(.Text, ":")
    .Text = Chr(11) & vbTab
    .Collapse wdCollapseEnd
    .Text = "Euro, Dollar"
    .Font.Bold = True
    .Font.Italic = True
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub
于 2018-08-01T00:07:53.240 回答