+ Reply to Thread
Results 1 to 6 of 6

Word macro to check url exists

  1. #1
    Registered User
    Join Date
    03-26-2015
    Location
    SINGAPORE
    MS-Off Ver
    MS2013
    Posts
    56

    Word macro to check url exists

    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.

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Word macro to check url exists

    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]

  3. #3
    Registered User
    Join Date
    03-26-2015
    Location
    SINGAPORE
    MS-Off Ver
    MS2013
    Posts
    56

    Re: Word macro to check url exists

    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.

  4. #4
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Word macro to check url exists

    Place the following macro in the 'This Document' code module of the document's template:

    Please Login or Register  to view this content.
    You will need to supply your intranet url where indicated.

    Note that this will affect all documents you open.

  5. #5
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Word macro to check url exists

    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

  6. #6
    Registered User
    Join Date
    04-05-2023
    Location
    Alskas, USA
    MS-Off Ver
    Office 16
    Posts
    6

    Re: Word macro to check url exists

    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.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Check if Word Exists in Array?
    By Merf in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 01-14-2022, 05:25 PM
  2. A macro to check if a URL exists
    By martijnrikkert in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-31-2017, 06:53 AM
  3. [SOLVED] Macro code to check if worksheet already exists
    By robertguy in forum Excel General
    Replies: 3
    Last Post: 06-24-2016, 06:42 AM
  4. Macro to check if file exists
    By mvsareli in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-16-2013, 04:51 PM
  5. Check cell for word, If word exists in cell, assign that cell to corresponding category.
    By Trevasaurus in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-12-2012, 12:35 PM
  6. Check if word exists and count how many times, help
    By execine in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-07-2010, 02:08 PM
  7. Check if a list of Files exists Macro
    By cjconnor24 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-01-2008, 07:44 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1