Solved

Visual BAsic For Microsoft Word Manipulation - Hyperlink Copy and Paste

Posted on 2004-10-28
1,209 Views
Last Modified: 2013-11-25
I have approximately 700 word documents that have a hyperlink at the bottom.  It is a pure hyperlink, entered as
{http://intranet.tollbrothersinc.com/tdrive/Intranet Specs/AZ Division/Spec Appliances.doc}.  We would like to use a formatted hyperlink that would show up as
- CLICK HERE TO DISPLAY INTRANET SPECS PORTION OF DOCUMENT -.  I would like to copy the hyperlink address and apply it to the new hyperlink.  I am trying to put everything together to execute on a virtual machine on the startup of all files.  Greatly decreasing the time needed to manually enter everything.  I suupose 2 hours to investigate is better than 10 of sitting and doing Manual.  The current address of the new hyperlink is a file located on my C:\Drive.  I can do almost everything except when I copy the original and try to paste it pastes explicitly.  It isn't actually pasting.  The macro of course says:
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
       "http://intranet.tollbrothersinc.com/tdrive/Intranet Specs/AZ Division/Spec Appliances.doc" _
       , SubAddress:=""

When I really want to use
Address:= Selection.Paste however I am getting compile error expected function or variable pointing at that line.  

The problem is I am using a RECORDED MACRO to actually go into the Hyperlink Window and paste a new address while retaining the formatting, ie the Link Text.  If I could create a new hyperlink that would work also, but I cannot find anything to help me.  I need to be able to copy a string from the document and create a new formatted hyperlink using that address.  Please Help.

The complete Macro is:

Sub Macro4()
'
' Macro4 Macro
' Macro recorded 10/28/04 by MIS Dept.
'
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "http://intranet"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.Cut              ' trying to copy the selected text here
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "- CLICK"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.Range.Hyperlinks(1).Range.Fields(1).Result.Select
    Selection.Range.Hyperlinks(1).Delete
    ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
       "http://intranet.*********.com/tdrive/appliances.doc"     'want to paste here not declare explicitly
    Selection.Collapse Direction:=wdCollapseEnd
    Selection.HomeKey Unit:=wdLine
    Selection.TypeBackspace
End Sub
0
Question by:jmmahon
    2 Comments
     
    LVL 5

    Accepted Solution

    by:
    I am a bit confused about what you want to have as the Address parameter.  

    If I read your code right then you have hunted out your 'ugly' hyperlink (the "http:// ..." one) and cut it and then you want to paste that as the new address of the pretty hyperlink.

    The problem is the Selection.Paste - that's telling word to paste over the current selection whilst in the middle of creating the hyperlink.

    I think you'd be better to just grab the ugly hyperlink's text and then apply to the address as a string.  So.

    Dim sHyperLink as string

    < then everything you have down to the 'cut' command >

    sHypeLink = Selection.Text

    < then the Selection.cut and everything down to your add >

     ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= sHyperLink

    < then the end of your code >

    I tested it as best I could and it accepts a string as the address
    0
     

    Author Comment

    by:jmmahon
    Thank you.  I've seen this other places and everyone was doing the same.  Using selection.cut and then trying use the string for whatever field as selection.paste.  
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: MongoDB Object-Document Mapper for NodeJS

    NodeJS (JavaScript on the server) is awesome, but some developers get confused about NoSQL when it comes to working in Node with MongoDB (NoSQL database). Do you need a better explanation of how to use Node.js with MongoDB? The most popular choice is the Mongoose library.

    A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
    Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now