Add Hyperlink to Word Document through Excel VBA

dhancy

Board Regular
Joined
Jul 10, 2013
Messages
120
Office Version
  1. 2016
Platform
  1. Windows
My Excel VBA creates a Word Document.

I want to add a hyperlink to some of the text in this document.

Here is a snippet of the code:

Code:
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document

    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
    Set wrdDoc = wrdApp.Documents.Add(Template:=folder & "\DD3.dotx")
...

   With wrdApp.Selection

       .TypeParagraph
       .TypeText sometext

       .Hyperlinks.Add Anchor:=Selection.Range, Address:=file & ".pdf", _
                        SubAddress:="", ScreenTip:="", TextToDisplay:=file

...

end with


The ".hyperlinks" line I obtained from recording a macro in Word.


When I attempt to run this, I get the error:

--
Run-time error '450':
Wrong number of arguments or invalid property assignment
---


In my debugging efforts, I noticed this: When I type out ".hyperlinks.add ", I see the pop up list of help tips to complete the command. There are six properties shown: Anchor, Address, SubAddress, ScreenType, TextToDispay AND Target.

I am guessing the error means there is another argurment I need to pass - Target. What is that? What value to I use for it? And how come that doesn't show up when I recorded the Word macro?


Thanks,
Dennis
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
This always happens.

Once I ask a question, I stumble across the answer.

Thought I'd share what I found out in case anyone else has this problem.


The problem isn't with the TARGET parameter. The problem is that I needed to add "wrdApp." in front of "selection.range" on the Anchor parameter. I am guessing that the code thought it was an Excel selection and not a Word selection.

The correct code is

Code:
Hyperlinks.Add Anchor:=wrdApp.,Selection.Range, Address:=file & ".pdf", _
                        SubAddress:="", ScreenTip:="", TextToDisplay:=file



Now if I could just figure out how to select the text to which I want to add the hyperlink.

That will be my next task.


Dennis
 
Upvote 0

Forum statistics

Threads
1,216,113
Messages
6,128,905
Members
449,478
Latest member
Davenil

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top