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.
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