필수의
MS Word 문서에서 단일 텍스트의 모든 항목을 찾고, 각 항목을 하이퍼링크로 만들고, 일반 하이퍼링크 스타일을 내가 선택한 스타일로 변경하고 싶습니다.
내가 가진 것
위의 요구 사항을 전체적으로 달성하는 방법을 모르기 때문에 그 중 일부, 즉 단일 인스턴스를 찾고 이를 조정하는 작업부터 시작했습니다.
그래서 매크로를 기록했는데 그 결과 다음과 같은 코드가 나왔습니다. 그 코드는 서브가 매개변수를 취할 수 있도록 조정했습니다.하이퍼링크텍스트그리고 하이퍼링크하위 주소:
Sub AutoDetectHyperlinksForText(hyperlinkText As String, subaddress As String, style As String)
Selection.Find.ClearFormatting
With Selection.Find
.Text = hyperlinkText
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
subaddress:=subaddress, ScreenTip:="", TextToDisplay:= _
hyperlinkText
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Style = ActiveDocument.Styles(style)
End Sub
이를 통해 쉽게부르다다음과 같은 여러 단어 인스턴스의 하위 항목:
Call AutoDetectHyperlinksForText("broadcasts", "_broadcastService", "Subtle Emphasis")
질문
전체 문서를 확인하도록 이 매크로를 어떻게 조정합니까?
보너스 질문
선택 사항을 저장하고 .MoveLeft
?
의사코드에서는 다음과 같습니다.
Dim mySelect as Selection
mySelect = Selection.Find.Execute
ActiveDocument.Hyperlinks.Add Anchor:=mySelect.Range, Address:="", _
subaddress:=subaddress, ScreenTip:="", TextToDisplay:= _
hyperlinkText
mySelect.Style = ActiveDocument.Styles("Subtle Emphasis")
답변1
그러면 "google"(googles나 googled가 아님)이라는 단어가 검색되어 다음으로 하이퍼링크됩니다.http:\\google.com
스타일도 적용됩니다
Sub FindAndHyperlink()
'define the style
Dim strStyle As String
strStyle = "Subtle Emphasis"
'set the search range
Dim rngSearch As Range
Set rngSearch = ActiveDocument.Range
'set the search string
Dim strSearch As String
strSearch = "google"
'set the target address for the hyperlink
Dim strAddress As String
strAddress = "http:\\google.com"
With rngSearch.Find
Do While .Execute(findText:=strSearch, MatchWholeWord:=True, Forward:=True) = True
With rngSearch 'we will work with what is found as it will be the selection
ActiveDocument.Hyperlinks.Add Anchor:=rngSearch, Address:=strAddress
.Style = ActiveDocument.Styles(strStyle) 'throw the style on it after the link
End With
rngSearch.Collapse Direction:=wdCollapseEnd
'keep it moving
Loop
End With
End Sub
분명히 인수를 전달하려는 경우 인수를 사용하여 호출할 수 있도록 그렇게 할 수 있지만 이것이 필요한 것의 기초입니다.