Results 1 to 5 of 5

Thread: Extract URL in Word

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    3

    Extract URL in Word

    Hi, I'm not really a programmer, but I've found VBA to be useful in many areas of my work. But right now I'm a bit stuck. I need a way to search through documents and extract hypertext URLs as HTML code so I can cut and paste into a web CMS. I believe it can be done. Here's an example of what I want to achieve:

    I want to turn:
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Quisque a.

    Into:
    Lorem ipsum dolor sit amet, <a href="#">consectetuer adipiscing</a> elit. Quisque a.

    Does anyone know how to perform this operation? Really appreciate any help.

  2. #2
    Lively Member JustinLabenne's Avatar
    Join Date
    Jul 2005
    Location
    Ohio
    Posts
    64

    Re: Extract URL in Word

    Maybe like this?

    VB Code:
    1. Option Explicit
    2.  
    3. Sub Extraction()
    4.     Dim hlnk As Hyperlink
    5.     Dim C As String
    6.     Dim d As String
    7.    
    8. For Each hlnk In ActiveDocument.Hyperlinks
    9.     C = "<a href=""#"">"
    10.     d = "</a>"
    11.    
    12.     hlnk.TextToDisplay = C & hlnk.TextToDisplay & d
    13.  
    14. Next hlnk
    15. End Sub
    Justin Labenne
    www.jlxl.net

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    3

    Re: Extract URL in Word

    Thanks Justin, but just one problem. I need the URL that was extracted to replace the "#". Can that be done? Should I replace the "#" with "hlnk.hyperlink"?

    Also, is it possible to make the text not a link anymore?

    Sorry, I'm really not familiar with the code. Thanks again for the help.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    3

    Re: Extract URL in Word

    Oh, I got it. it's "hlnk.Address". =) Thanks!

  5. #5
    Lively Member JustinLabenne's Avatar
    Join Date
    Jul 2005
    Location
    Ohio
    Posts
    64

    Re: Extract URL in Word

    Try this, had an issue at first with not getting the hyperlinks removed, stubborn little buggers,

    VB Code:
    1. Option Explicit
    2.  
    3. Sub Extraction()
    4.     Dim hlnk As Hyperlink
    5.     Dim C As String
    6.     Dim d As String
    7.     Do Until ThisDocument.Hyperlinks.Count = 0
    8.     For Each hlnk In ActiveDocument.Hyperlinks
    9.     C = "<a href=" & hlnk.Address & ">"
    10.     d = "</a>"
    11.         With hlnk
    12.             .TextToDisplay = C & hlnk.TextToDisplay & d
    13.             .Delete
    14.         End With
    15.     Next hlnk
    16.     Loop
    17. End Sub
    Justin Labenne
    www.jlxl.net

Posting Permissions

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



Click Here to Expand Forum to Full Width