如何使用VBA在Microsoft Word中隐藏字符?

时间:2019-07-08 07:36:19

标签: vba ms-word hide

我想在Microsoft Word中隐藏字符,更具体地说,是在某些字符之间隐藏文本。

例如,如果我明白了:

::00-58-96:: Hello there
::00-58-97:: This is a test
::00-58-98:: Nothing else
::00-58-99:: Good bye !

我想在

之间隐藏文本
::        ::

结果将是

Hello there
This is a test
Nothing else
Good bye !

另一个例子是

==this:example== Again this
==this:example== Is a
==this:example== Test

结果将是

Again this
Is a
Test

我不知道我是否很好地暴露了我的问题。

我已经尝试这样做(其他人帮助了我),但这代替了文字,而不是将其隐藏:

Sub l()
'
'
'
    ActiveDocument.Range.Select '

With Selection.Find
     .MatchWildcards = True
     .Text = "::*::"
     .Replacement.Text = ""
     .Execute Replace:=wdReplaceAll, Forward:=True, _
     Wrap:=wdFindContinue
End With


End Sub


更新:

Word仍然崩溃/只是隐藏了文档的第一行,我只修改了一行,如下所示:

Private Sub SelFind()


    Dim Rng As Range
    Dim Fnd As Boolean

G:
    Set Rng = ActiveDocument.Range


    With Rng.Find
        .ClearFormatting
        .MatchWildcards = True
        .Execute FindText:=";;*;;*;;", Forward:=True, _
                 Format:=False, Wrap:=wdFindStop
        Fnd = .Found
    End With

    If Fnd = True Then
        With Rng
            .MoveStart wdWord, 0
            .Select
            With .Font
                .Hidden = True
            End With
        End With
        GoTo G

    Else:

    MsgBox "All done"

    End If
End Sub

1 个答案:

答案 0 :(得分:1)

尝试一下:

Private Sub SelFind()


    Dim Rng As Range
    Dim Fnd As Boolean

G:
    Set Rng = ActiveDocument.Range


    With Rng.Find
        .ClearFormatting
        .MatchWildcards = True
        .Execute FindText:="::*::", Forward:=True, _
                 Format:=False, Wrap:=wdFindStop
        Fnd = .Found
    End With

    If Fnd = True Then
        With Rng
            .MoveStart wdWord, 0
            .Select
            With .Font
                .Hidden = True
            End With
        End With
        GoTo G

    Else:

    MsgBox "All done"

    End If
End Sub

从此Answer

寻求帮助