Hi hi
I need help to write a word macro to check if urls exists in the word document. So user open the file, the macro first check if the url exist, if url does not exist, message to appear like 'url not exist' and close the file.
Thanks.
Hi hi
I need help to write a word macro to check if urls exists in the word document. So user open the file, the macro first check if the url exist, if url does not exist, message to appear like 'url not exist' and close the file.
Thanks.
Last edited by ElaineMok; 10-07-2022 at 12:09 AM.
Do you want to check for a particular URL, or just any URL? Your post is unclear about this.
Also, does the URL have to be a working hyperlink, or just a text string?
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Hi Macropod
It is a particular URL that is in my company intranet. The URL have to be a working hyperlink. Thanks for the help.
Place the following macro in the 'This Document' code module of the document's template:
You will need to supply your intranet url where indicated.Please Login or Register to view this content.
Note that this will affect all documents you open.
Cross-posted at: https://www.msofficeforums.com/word-...-word-doc.html
Please read Excel Forum's Cross-Posting policy in rule 8: http://www.excelforum.com/forum-rule...rum-rules.html
Here's a Word VBA macro that checks if URLs exist in the document and displays a message if a URL is not found:
mathematica
Copy code
Sub CheckURLs()
Dim i As Integer
Dim URL As Hyperlink
Dim URLFound As Boolean
Set URLFound to False by default
URLFound = False
Loop through all hyperlinks in the document
For Each URL In ActiveDocument.Hyperlinks
If InStr(1, URL.Address, "http") > 0 Then
URLFound = True
Exit For
End If
Next URL
' Display a message if no URLs were found
If URLFound = False Then
MsgBox "No URLs were found in this document."
ActiveDocument.Close False
End If
End Sub
To use this macro, follow these steps:
Open the Word document that you want to check for URLs.
Press Alt + F11 to open the Visual Basic Editor.
In the Visual Basic Editor, click Insert > Module.
Paste the macro code into the new module.
Press F5 to run the macro.
The macro will loop through all hyperlinks in the document and check if the address starts with "http". If it finds a URL, it will set URLFound to True and exit the loop. If no URLs are found, it will display a message and close the document.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks