[英]To replace a text in word document with Hyperlinks, using VBScript
I would like to replace a text ,say, 'hello', anywhere in a word document and replace it with Hyperlink - ' http://www.google.com '.I am using a replace function to achieve the same.我想在word文档中的任何位置替换文本,比如“你好”,并将其替换为超链接 - ' http://www.google.com '。我正在使用替换功能来实现相同的功能。 I understand that the .Range() should be pointing to the text that needs to be replaced.
我知道 .Range() 应该指向需要替换的文本。 But how.
但是如何。 And how will I pass the hyperlink argument to the replace().
以及如何将超链接参数传递给 replace()。
Here a sample of the defective code :这是有缺陷的代码示例:
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("C:\Test\Test_Hyperlink.docx")
Set objRange = objDoc.Range()
'passing the text to be found 'hello' and hyperlink to be replaced
FnSearchAndReplaceText "hello", (objDoc.Hyperlinks.Add objRange, " http://www.google.com", , ,)
Function FnSearchAndReplaceText(argFindText, argReplaceText)
Const wdReplaceAll = 2
Set objSelection = objWord.Selection
objWord.Visible = True
objSelection.Find.Text = argFindText
objSelection.Find.Forward = TRUE
objSelection.Find.MatchWholeWord = True
objSelection.Find.Replacement.Text = argReplaceText
objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll
End Function
Any input is welcome.欢迎任何意见。
The following code works as expected in Word-VBA for ActiveDocument/ThisDocument
.以下代码在
ActiveDocument/ThisDocument
Word-VBA 中按预期工作。 I think you could easily adopt it to use in VBScript subroutine.我认为您可以轻松采用它在 VBScript 子程序中使用。
Sub Replace_text_Hyperlink()
Dim txtToSearch
Dim txtHyperLink
Dim txtNew
txtToSearch = "hello"
txtHyperLink = "http://www.google.com"
ThisDocument.Content.Select
With Selection.Find
.ClearFormatting
.Text = txtToSearch
.Forward = True
.Wrap = wdFindStop
End With
Do While Selection.Find.Execute
Selection.Text = "'http://www.google.com'" 'your new text here
ActiveDocument.Hyperlinks.Add Selection.Range, txtHyperLink 'but hyperlink is created here
Loop
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.