Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert Hyperlink into Word Doc with Ole Automation

Status
Not open for further replies.

AHJ1

Programmer
Oct 30, 2007
69
US
I want to add a hyperlink by taking a value from an access form and passing it to a routine.

I started by copying some code, then I wrote a macro in a Word App and copied and pasted. It worked until I tried to add the hyperlink. Help, please.

Code:
Option Compare Database
Option Explicit

Sub AutoWord(strFileName As String)
  ' Declare the variable.
   Dim oWord As Word.Application
    
    ' Set the variable (runs new instance of Word).
    Set oWord = CreateObject("Word.Application")

    ' Add a new document.
    oWord.Documents.Add
'@ When I added this info it failed.
    oWord.Selection.TypeText "This is some text."
    oWord.Selection.TypeParagraph
    oWord.Selection.TypeText Text:="Alan Jordan"
'Fails on next line with err code 91, object or With Block variable not set
    Selection.MoveLeft Unit:=wdWord, Count:=2, Extend:=wdExtend
    ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
        "[URL unfurl="true"]http://www.alanjordan.com/",[/URL] SubAddress:="", ScreenTip:="", _
        TextToDisplay:="Alan Jordan"
'@


    ' Add some text.
    
    oWord.Selection.TypeText "This is some text."
    With oWord.Selection
        .TypeText Text:="File Name:"
        .TypeParagraph
        .TypeParagraph
        .TypeText Text:="Bill Number" & vbTab & vbTab
        .Font.Bold = wdToggle
        .TypeText Text:="Effective Date: "
        .Font.Bold = wdToggle
        .TypeParagraph
        .TypeParagraph
        .TypeText Text:="This is a paragraph of text"
        .TypeParagraph
        .TypeParagraph
        .TypeText Text:="Section "

    End With
    ' Save the document.
    oWord.ActiveDocument.SaveAs Filename:=strFileName
    ' Quit Word.
    oWord.Quit

    ' Clear the variable from memory.
    Set oWord = Nothing


End Sub
 
[!]oWord.[/!]Selection.MoveLeft Unit:=wdWord, Count:=2, Extend:=wdExtend
[!]oWord.[/!]ActiveDocument.Hyperlinks.Add Anchor:=[!]oWord.[/!]Selection.Range, Address:= _

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top