Word宏,可以找到并创建类似单词的超链接

时间:2013-07-31 09:10:00

标签: loops ms-word word-vba

所以,这是我的宏代码(Macro1):

Sub Macro1()
'
' Macro1 Macro
'
     Selection.Find.ClearFormatting
     With Selection.Find
         .Text = "REQ"
         .Replacement.Text = ""
         .Forward = True
         .Wrap = wdFindContinue
         .Format = False
         .MatchCase = True
         .MatchWholeWord = False
         .MatchWildcards = False
         .MatchSoundsLike = False
         .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.MoveRight Unit:=wdCharacter, Count:=8, Extend:=wdExtend
    Selection.Copy
    ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
    "http://www.neki.com/REQ12345678", SubAddress:="", ScreenTip:="", _
    TextToDisplay:="REQ12345678"
End Sub

代码可以正常查找REQxxxxxxxx文本,但随后粘贴错误的TextToDisplay和错误的地址结尾。在两个地方而不是REQ12345678应该粘贴我之前复制的相同文本:Selection.Copy。 我也不知道如何创建循环或类似的东西,以便Macro1运行直到它到达文档的末尾。 请帮帮我!

嘿,我解决了创建超链接的第一个问题。现在我必须循环“超链接”宏。我决定创建另一个宏,它将循环第一个宏。这是我的代码:

Sub Macro2()
'
' Macro2 Macro
'

Do Until ActiveDocument.Bookmarks.Exists("Konec")
Application.Run MacroName:="Macro1"
Loop
End Sub

Macro1工作得非常好,但我无法弄明白如何将其循环直到文档结束 - 直到结束书签......

1 个答案:

答案 0 :(得分:0)

我使用以下代码将所选文本链接到文档稍后出现的类似文本。

Sub Macro1()
'
' Macro1 Macro
'
'
   With Selection.Find
         .Text = Selection
         .Replacement.Text = ""
         .Forward = True
         .Wrap = wdFindContinue
         .Format = False
         .MatchCase = True
         .MatchWholeWord = True
         .MatchWildcards = True
         .MatchSoundsLike = False
         .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    With ActiveDocument.Bookmarks
        .Add Range:=Selection.Range, Name:=Selection
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With
      With Selection.Find
         .Text = Selection
         .Replacement.Text = ""
         .Forward = False
         .Wrap = wdFindContinue
         .Format = False
         .MatchCase = True
         .MatchWholeWord = True
         .MatchWildcards = True
         .MatchSoundsLike = False
         .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
        SubAddress:=Selection, ScreenTip:="", TextToDisplay:=Selection

End Sub