KickStarter15

Would it be possible to find and remove hyperlink in Word document?

10 posts in this topic

Hi Experts,

I just want to ask if is there any functions that can find the word with hyperlink and remove the hyperlink after?

I've been searching for a while now and can't seems to find that fits my inquiry. I tried this function _Word_DocLinkGet() but it's not what I'm looking for.^_^

 

Any advise or any link is appreciated. Thanks in advance Experts.

KS15

Share this post


Link to post
Share on other sites



I would give _Word_DocLinkGet a try. It returns a collection of all links if parameter 2 is empty. You could then loop through this collection and delete the wanted link.
How do you determine which link to delete?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

@water, Yes, I already tried using it from help file and it only display the text and it's hyperlink by the below function with msgbox(). The tool should be validating active document.

For $oLink In $oLinks
    $sResult = $sResult & "Text: " & $oLink.TextToDisplay & @CRLF & "Address: " & $oLink.Address & _
            @CRLF & "------------------------------------------" & @CRLF
Next
MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocLinkGet Example - Process all hyperlinks", $sResult)

The one thing that keeps in my mind is that it should be user interactive tool to decide whether to remove or not.

Here's what I've got so far from help and I don't know where to start.:(

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

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

Local $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\Hyperlink\Test.doc", Default, Default, True)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Error!", "Error opening '.\Extras\Test.doc'. @error = " & _
        @error & ", @extended = " & @extended)

Local $oLinks = _Word_DocLinkGet($oDoc)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Error!", _
        "Error accessing link collection of the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
Local $sResult = "Total number of hyperlinks in the document: " & @extended & @CRLF & @CRLF
For $oLink In $oLinks
    $sResult = $sResult & "Text: " & $oLink.TextToDisplay & @CRLF & "Address: " & $oLink.Address & _
            @CRLF & "------------------------------------------" & @CRLF
Next
; I created a log to for checking.
$FileLog = FileOpen(@ScriptDir & "\Hyperlink.log")
FileWriteLine(@ScriptDir & "\Hyperlink.log", $sResult)
FileClose($FileLog)

 

Share this post


Link to post
Share on other sites

Something like this:

#include <Word.au3>
HotKeySet("{F2}", "_DeleteHyperLink")
Global $oWord = _Word_Create()
Global $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\test.docx")
Global $oLinks = _Word_DocLinkGet($oDoc)
Global $oLink
MsgBox(0, "", "Number of Links in this document: " & $oLinks.Count)
For $i = $oLinks.Count To 1 Step -1
    $oLink = $oLinks($i)
    $oLink.Range.Select
    MsgBox(0, "Hyperlink", "Address: " & $oLink.Address & @CRLF & _
            "Name: " & $oLink.Name & @CRLF & _
            "SubAddress: " & $oLink.SubAddress & @CRLF & _
            "TextToDisplay: " & $oLink.TextToDisplay & @CRLF & _
            "Type: " & $oLink.Type & @CRLF & @CRLF & _
            "Press 'F2' if you want to remove this Hyperlink, then close this MsgBox!")
Next

Func _DeleteHyperLink()
    If IsObj($oLink) Then $oLink.Delete
EndFunc   ;==>_DeleteHyperLink

The links need to be processed from last to first. Like you would delete rows from an array.

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

@water, sorry for the late response, it was weekend here. Anyways, this is very great and no issue when attempting to check.
 Thanks Water, you save me again.:D

 

Share this post


Link to post
Share on other sites

No need to hurry, it has been weekend here too ;)
I'm glad my script solves your problem :)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

@water, Thank you so much, but I have one last question. There is another type of issue recently in our station (just now opened-up in our meeting).

My question is: If I want to remove all these hyperlink but except for this type "https://doi.org/*", can that be done by your existing code above?:>

They want my to have an option "Yes" and "No" button. If yes, then all hyperlink will be removed, however, this hyperlink "https://doi.org/*" should be excluded with the removal. If No, then it will apply the interactive GUI whether to remove or not (which is the code you have).:D

I ask them what's the difference? and they said a big difference no further explanation.:'(

EDIT: I tried this "If IsObj($oLink) Then $oLink.Delete" and it removes all the hyperlink but I need to exclude some hyperlink with "https://doi.org/*".

 

Edited by KickStarter15

Share this post


Link to post
Share on other sites

Something like this?

#include <Word.au3>
#include <msgBoxConstants.au3>
HotKeySet("{F2}", "_DeleteHyperLink")
Global $oWord = _Word_Create()
Global $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\test.docx")
Global $oLinks = _Word_DocLinkGet($oDoc)
Global $oLink, $sTitle = "Link Removal Tool", $iCount = 0, $sLinkExclude = "https://doi.org/"
If MsgBox(BitOR($MB_ICONQUESTION, $MB_YESNO), $sTitle, "Do you want to remove all Hyperlinks (except " & $sLinkExclude & "*)?") = $IDYES Then
    _RemoveAll()
    MsgBox($MB_ICONINFORMATION, $sTitle, "Hyperlinks removed: " & $iCount)
Else
    _RemoveSelected()
EndIf
Exit

Func _RemoveSelected()
    MsgBox($MB_ICONINFORMATION, $sTitle, "Number of Links in this document: " & $oLinks.Count)
    $iCount = 0
    For $i = $oLinks.Count To 1 Step -1
        $oLink = $oLinks($i)
        $oLink.Range.Select
        MsgBox(0, "Hyperlink", "Address: " & $oLink.Address & @CRLF & _
                "Name: " & $oLink.Name & @CRLF & _
                "SubAddress: " & $oLink.SubAddress & @CRLF & _
                "TextToDisplay: " & $oLink.TextToDisplay & @CRLF & _
                "Type: " & $oLink.Type & @CRLF & @CRLF & _
                "Press 'F2' if you want to remove this Hyperlink, then close this MsgBox!")
    Next
    MsgBox($MB_ICONINFORMATION, $sTitle, "Hyperlinks removed: " & $iCount)
EndFunc   ;==>_RemoveSelected

Func _RemoveAll()
    $iCount = 0
    For $i = $oLinks.Count To 1 Step -1
        $oLink = $oLinks($i)
        If IsObj($oLink) And StringLeft($oLink.Address, StringLen($sLinkExclude)) <> $sLinkExclude Then
            $oLink.Delete
            $iCount = $iCount + 1
        Endif
    Next
EndFunc   ;==>_RemoveAll

Func _DeleteHyperLink()
    If IsObj($oLink) Then
        $oLink.Delete
        $iCount = $iCount + 1
    EndIf
EndFunc   ;==>_DeleteHyperLink

 

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

:lmao:OMG, @water, how easy you made it. How did you do it? honestly, I'm having hard time searching and reading for this project to be completed but just an instance, you save me.:lol:

Thank you, thank you, thank you so much.......

 

Share this post


Link to post
Share on other sites

Just a matter of time ;) I joined this forum in 2005 and have completely rewritten the Excel and Word UDF :)
Diving into the Word object model helps as well.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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