Messy_Code_Guy

MS Word Find and Replace Hyperlinks.

4 posts in this topic

Greetings!

First things first!  I just wanted to say thanks to all the people that make Autoit possible. Without you guys and gals I would not be able to do the things I do at my current job.
Autoit ROCKS!

I am not "New" to Autoit but I consider myself "functionally dangerous"!  (I have been a long time lurker on the forums).

I am however new to using objects and Word files.

I have an issue with finding and replacing a hyperlink(s) in a Word doc.  I can manually find the range and the add a link but I want to do a find and replace without manually finding the range.  I have a sample of the code that works. (Thanks to the Help file)!  The stuff that is remed\commented out is the code that does not work.

I hope that makes sense and any help would be much appreciated.

#include <MsgBoxConstants.au3>
#include <Word.au3>
Local $oWord = _Word_Create()
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _
        "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
; Open test document read-only
Local $oDoc = _Word_DocOpen($oWord, @TempDir & "\test.docx", Default, Default, False)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _
        "Error opening " & @TempDir & "\test.docx." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

$emailold = "joe.smith@somecompany.com"
$emailnew = "mailto:jane.doe@somecompany.com"
Local $oRange = _Word_DocRangeSet($oDoc, -1, $wdWord,135, $wdWord,3)
 _Word_DocLinkAdd($oDoc, $oRange, $emailnew,Default,Default,"mike.smith@somecompany.com")
;$mail = _Word_DocLinkAdd($oDoc, $oRange, $emailnew,Default,Default,"mike.smith@somecompany.com")
;_Word_DocFindReplace($oDoc, $emailold, $mail,Default,0)

If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocLinkAdd Example", _
        "Error adding a link to the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
MsgBox($MB_SYSTEMMODAL, "Link Added", "E-mail Address." & @CRLF & $emailnew)

$text = "CLICK HERE"
$applyold = "http://oldurl.com/"
$applynew = "http://newurl.com"
Local $oRange = _Word_DocRangeSet($oDoc, -1, $wdWord,192, $wdWord,3)
_Word_DocLinkAdd($oDoc, $oRange, $applynew,Default,Default,"CLICK HERE")
;$applyonline = _Word_DocLinkAdd($oDoc, $oRange, $applynew,Default,Default,"CLICK HERE")
;_Word_DocFindReplace($oDoc, $text, $applyonline,Default,0)

If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocLinkAdd Example", _
        "Error adding a link to the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
MsgBox($MB_SYSTEMMODAL, "Link Added", "Apply on-line." & @CRLF & $applynew)


$text = "Link to Map"
$addressold = "https://www.google.com/maps/place/oldurl"
$addressnew = "https://www.google.com/maps/place/newurl"
Local $oRange = _Word_DocRangeSet($oDoc, -2, $wdWord,-125, $wdWord,3)
_Word_DocLinkAdd($oDoc, $oRange, $addressnew,Default,Default,"Link to Map")
;$map = _Word_DocLinkAdd($oDoc, $oRange, $addressnew,Default,Default,"Link to Map")
;_Word_DocFindReplace($oDoc, $text, $map,Default,0)

If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocLinkAdd Example", _
        "Error adding a link to the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
MsgBox($MB_SYSTEMMODAL, "Link Added", "Link to Map." & @CRLF & $addressnew)

_Word_DocSaveAs($oDoc, @TempDir & "\test.htm", $wdFormatHTML)
_Word_Quit($oWord)

Again, thanks to all who make this forum and Autoit work!


 

"The only thing necessary for the triumph of evil is for good men to do nothing".

Edmund Burke
 

Share this post


Link to post
Share on other sites



Sorry! I forgot to show what is not working.

 This how the link looked in the word document before I run the code. (the find and replace)

 This is what it looked like after running the code. It wipes out the text and does not change the link.

  This is before I run the code....

 and this is after I run the code.

I still can't figure out what I am doing wrong.

Thanks,


 

"The only thing necessary for the triumph of evil is for good men to do nothing".

Edmund Burke
 

Share this post


Link to post
Share on other sites

Still new to the forums.....could not get my screenshots to show in the post above.

See attached file for screen shots of the errors.

Errors.docx


 

"The only thing necessary for the triumph of evil is for good men to do nothing".

Edmund Burke
 

Share this post


Link to post
Share on other sites

Issue solved!  I was not doing a search for the text I wanted to use as a Range marker.

This code worked for me:

#include <MsgBoxConstants.au3>
#include <Word.au3>

Local $oWord = _Word_Create()
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _
        "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

Local $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\test.docx", Default, Default, False)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _
        "Error opening '.@ScriptDir & "\test.docx." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

$emailold = "billy.bob@somecompany.com"
$emailnew = "mailto:john.boy@somecompany.com"
 Local $oRange = _Word_DocFind ( $oDoc , $emailold ); this is what was missing from my code. The word doc find.
_Word_DocLinkAdd($oDoc, $oRange, $emailnew,Default,Default,"john.boy@somecompany.com")

 

1 person likes this

 

"The only thing necessary for the triumph of evil is for good men to do nothing".

Edmund Burke
 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now