Solved

cant add hyperlink to word doc

Posted on 2006-07-17
7
300 Views
Last Modified: 2008-02-01
Hello,

I am trying to insert a hyperlink into a word document using this code:

  Dim objWord
   Sub Btn1_onclick()
   call OpenDoc("C:\database\test.doc")
   End Sub



   Sub OpenDoc(strLocation)

   Set objWord = CreateObject("Word.Application")  
   objWord.Visible = True
   

    Set objDoc = objWord.Documents.Open("C:\database\test.doc")
    Set objSelection = objWord.Selection
 
    objWord.Selection.TypeText("This is the text ")
    objWord.Selection.TypeParagraph()
   
    With objSelection.Find
        .Text = "Bensley"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    objSelection.Find.Execute
   ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
    "http://www.website.com/index.aspx?definitionID=d091283la", _
    SubAddress:="", ScreenTip:= _
    "Screentip Text" _
    , TextToDisplay:="TextToDisplayWord"
   
    objDoc.SaveAs "c:\database\test2_no_hyperlinks4.doc"



I am using a website to do this.

The problem occurs on the ActiveDocument.Hyperlinks.Add line... it wont add a hyperlink.  nothing happens.


Does anyone know how to fix this, or to insert a hyperlink?


Thanks,
0
Comment
Question by:alexthecodepoet
  • 4
  • 3
7 Comments
 
LVL 26

Expert Comment

by:EDDYKT
ID: 17129287
try


objWord.ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
    "http://www.website.com/index.aspx?definitionID=d091283la", _
    SubAddress:="", ScreenTip:= _
    "Screentip Text" _
    , TextToDisplay:="TextToDisplayWord"
0
 

Author Comment

by:alexthecodepoet
ID: 17131077
Hm, i tried that, but still, nothing happens..
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 17131197
Try this
                           
objWord.ActiveDocument.Hyperlinks.Add Anchor:=objselection.Range, Address:= _
    "http://www.website.com/index.aspx?definitionID=d091283la", _
    SubAddress:="", ScreenTip:= _
    "Screentip Text" _
    , TextToDisplay:="TextToDisplayWord"
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:alexthecodepoet
ID: 17131467
still nothing :(
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 17131838
well it works for me. Do you have any error?

----------                                                                ---
objWord.ActiveDocument.Hyperlinks.Add Anchor:=objselection.Range,

change there


0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 500 total points
ID: 17131850
i use this


Set objWord = CreateObject("Word.Application")  
   objWord.Visible = True
   

    Set objDoc = objWord.Documents.Open("C:\database\test.doc")
    Set objSelection = objWord.Selection
 
    objWord.Selection.TypeText("This is the text ")
    objWord.Selection.TypeParagraph()
   
    With objSelection.Find
        .Text = "Bensley"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    objSelection.Find.Execute
   objWord.ActiveDocument.Hyperlinks.Add Anchor:=objselection.Range, Address:= _
    "http://www.website.com/index.aspx?definitionID=d091283la", _
    SubAddress:="", ScreenTip:= _
    "Screentip Text" _
    , TextToDisplay:="TextToDisplayWord"
   
    objDoc.SaveAs "c:\database\test2_no_hyperlinks4.doc"
0
 

Author Comment

by:alexthecodepoet
ID: 17132474
Thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

840 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