Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

creating a hyperlink in a word document from Excel

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ariel - 27 Sep 2006 19:27 GMT
I am creating a report in Word that is written in Excel (due to a large
number of actions that needs to occur there).

In any case, I need to insert hyperlinks in the document, and the following
code does not work. WordApp has already been dim'ed:

With WordApp.ActiveDocument
   .Hyperlinks.Add Anchor:=Selection.Range, Address:= _
       "http://www.ohsu.edu/hms/survey/activity/brief", SubAddress:="", _
       ScreenTip:="", TextToDisplay:="Activity"
End With
Tony Jollans - 27 Sep 2006 19:55 GMT
I suspect that Selection.Range refers to Excel's Selection, and you want
Word's - you must qualify it.

--
Enjoy,
Tony

> I am creating a report in Word that is written in Excel (due to a large
> number of actions that needs to occur there).
[quoted text clipped - 7 lines]
>         ScreenTip:="", TextToDisplay:="Activity"
> End With
Ariel - 28 Sep 2006 02:50 GMT
Actually, this was recorded in Word, so selection.range is germaine to Word
(however, when recorded in Excel the code is identical).

Any ideas?

> I suspect that Selection.Range refers to Excel's Selection, and you want
> Word's - you must qualify it.
[quoted text clipped - 15 lines]
> >         ScreenTip:="", TextToDisplay:="Activity"
> > End With
Tony Jollans - 28 Sep 2006 06:34 GMT
When you record code in Word it has implicit references to Word objects. If
you copy  that code to Excel you must make the implicit references explicit.
Have you tried qualifying the Selection reference?

When you record code in Excel it has implicit references to Excel objects.
The fact that code with implicit references to Word and code with implicit
references to Excel are superficially the same is little more than
coincidence.

--
Enjoy,
Tony

> Actually, this was recorded in Word, so selection.range is germaine to Word
> (however, when recorded in Excel the code is identical).
[quoted text clipped - 20 lines]
> > >         ScreenTip:="", TextToDisplay:="Activity"
> > > End With
Tony Strazzeri - 28 Sep 2006 04:07 GMT
What error are you actually getting?

You say "WordApp has already been dim'ed" but has it been "set" ?
ie does it have a value (corresponding to the running word application
object that has the open document).

Cheers
TonyS.

> I am creating a report in Word that is written in Excel (due to a large
> number of actions that needs to occur there).
[quoted text clipped - 7 lines]
>         ScreenTip:="", TextToDisplay:="Activity"
> End With
Ariel - 28 Sep 2006 04:43 GMT
Hi Tony,

Yes, at the start of the sub routine I have the following:

Dim WordApp As Word.Application
Set WordApp = New Word.Application
Dim WordDoc As Word.Document

Everything else works up until the point (plenty of lines of code) where I
want to insert this hyperlink. The error I get is the following: Object
doesn't support this property or method (Error 438)

Any help is appreciated.

Ariel

> What error are you actually getting?
>
[quoted text clipped - 16 lines]
> >         ScreenTip:="", TextToDisplay:="Activity"
> > End With
Ed - 28 Sep 2006 15:11 GMT
Two thoughts:
-- Don't use ActiveDocument when you already have WordDoc as a document
object reference
-- Dim a range as Word.Range and Set it to the place you need the hyperlink
installed, and use that reference instead of "Selection".

Depending on what else is going on, "ActiveDocument" and "Selection" can
give you many headaches!
Ed

> Hi Tony,
>
[quoted text clipped - 34 lines]
>> >         ScreenTip:="", TextToDisplay:="Activity"
>> > End With
Tony Strazzeri - 28 Sep 2006 15:43 GMT
Hi Ariel,

I tried your code both from Word and Excel. I was getting slightly
different error. I am not sure how Word's selection method canwork when
automating the application from outside Word so have substituted a
Range variable instead.

The code below may help you.  It works from Excel.

   Dim WordApp As Word.Application
   Dim WordDoc As Word.Document

   Set WordApp = New Word.Application

   'This allows you to see the application
   WordApp.Visible = True

   'This creates a document in the new Application process
   Set WordDoc = WordApp.Documents.Add

   With WordDoc        'WordApp.ActiveDocument
       'playing around here to demonstrate moving around the doc
       WordDoc.Range.InsertAfter "aaaaaaa" & vbCr & "bbbbbbbbbbbbb" &
vbCr
       Dim rng As Range
       Set rng = .Range
       rng.Collapse wdCollapseStart
       rng.MoveStartUntil "b"
       .Hyperlinks.Add Anchor:=rng, _

Address:="http://www.ohsu.edu/hms/survey/activity/brief", _
                       SubAddress:="", _
                       ScreenTip:="tip", _
                       TextToDisplay:="Activity"
   End With

Once you have the above working you should be able to substitute the
range with say a bookmark range in order to insert your link where you
want it.

Hope this helps.

Cheers
TonyS.

> Hi Tony,
>
[quoted text clipped - 32 lines]
> > >         ScreenTip:="", TextToDisplay:="Activity"
> > > End With
Ariel - 28 Sep 2006 16:08 GMT
Hi Tony,

I tried your code and I get a "type mismatch" error at Set rng = .Range

thanks

Ariel

> Hi Ariel,
>
[quoted text clipped - 77 lines]
> > > >         ScreenTip:="", TextToDisplay:="Activity"
> > > > End With
Helmut Weber - 28 Sep 2006 16:17 GMT
Hi Ariel,

try:

Dim rng As word.Range

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Ariel - 28 Sep 2006 17:10 GMT
Works like a charm. Thank you all for your input. It is great having such
support!

> Hi Ariel,
>
> try:
>
> Dim rng As word.Range
Ariel - 06 Oct 2006 01:50 GMT
Hi Tony,

I have been trying unsuccessfully to get the cursor to move past the
inserted hyperlink so that I can add in another one (or a series of them if
necessary) and then continue on with a new paragraph.

Do you have a suggestion of how I can do this (remember that this code is
being written in Excel VBA).

Thanks for your help!

Ariel

> Hi Ariel,
>
[quoted text clipped - 77 lines]
> > > >         ScreenTip:="", TextToDisplay:="Activity"
> > > > End With
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.