如何隐藏Word文档中两个标记(<hidden> ...... </hidden>)之间的数据

时间:2013-05-15 07:54:01

标签: vba word-vba

需要一个代码,以便...两个标签之间存在的数据应该被隐藏..为此我所做的是首先我在文档中搜索tag并用注册符号替换该标签(®)得到那样的范围。我需要一种代码,而无需替换标签

Sub Test()
Dim i As Long
i = 0
Dim myWord As String
myWord = "®"
TargetList = Array("</hidden>")
With ActiveDocument.Content.Find
     .Text = "</hidden>"
     .Replacement.Text = myWord
     .Wrap = wdFindContinue
     .Execute Replace:=wdReplaceAll
End With 
  Counter = 1
Set Range = ActiveDocument.Range
Selection.HomeKey Unit:=wdStory
With Selection.Find
    .Forward = True
    .Wrap = wdFindStop
    .Text = "<hidden>"
While .Execute
    Counter = Counter + 1
Wend
End With
For i = 0 To Counter
Set Range = ActiveDocument.Range
Selection.HomeKey Unit:=wdStory
With Selection.Find
    .Forward = True
    .Wrap = wdFindStop
    .Text = "<hidden>"
If .Execute(Forward:=True) = True Then
With Selection.Range
Selection.MoveEndUntil Cset:="®", Count:=wdForward
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Font.Hidden = True
End With
End If
End With
Next i
End Sub

1 个答案:

答案 0 :(得分:3)

您可以使用通配符使用简单的单词查找和替换来实现此目的:

With Selection.Find
    .ClearFormatting
    .ClearAllFuzzyOptions

    .text = "[<]hidden[>](*)[<]/hidden[>]"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True

    .Replacement.text = ""
    .Replacement.ClearFormatting

    .Replacement.Font.Hidden = True

    .Execute Replace:=wdReplaceAll
End With

如果您还想删除标签,请使用以下替换文字:

    .Replacement.text = "\1"