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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Editing a Word template from Access VBA

Status
Not open for further replies.

newboy18

MIS
Apr 11, 2003
45
GB
Please help, this routine will work the first time it is
run, the 2nd time it will fail at the first "Selection."
statment with the error Run-time error '91' Object
variable or With block variable not set. What have I done
wrong?
Another error message I sometimes get is Run-time error '462' The remote server machine does not exist or is unavailable

Option Compare Database
Public Const DocsMail As String = "C:\DocsMail\"
Public Const WordDot As String = "c:\Test.dot"
Public Const WordDot2 As String = "c:\Test2.dot"
Public strName As String
Public strMail As String
Public strType As String
Public docType1 As Word.Document
Public docType2 As Word.Document

Sub Start_Proc()
strMail = "c:\test1.doc"
strName = "data to insert"
Dim objWord As Word.Application
Set objWord = CreateObject("Word.Application")
Set docType1 = objWord.Documents.Open(WordDot, , ReadOnly)
Set docType2 = objWord.Documents.Open(WordDot2, , ReadOnly)

docType1.Select
'objWord.ScreenUpdating = False
Selection.HomeKey Unit:=wdStory '## Edit template ##
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.TypeText Text:=strName

Selection.EndKey Unit:=wdStory
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.TypeText Text:=strName

Selection.HomeKey Unit:=wdLine
Selection.MoveUp Unit:=wdLine, Count:=5
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.TypeText Text:=strName

Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveRight Unit:=wdCell, Count:=2
Selection.HomeKey Unit:=wdLine
'objWord.ScreenUpdating = True

ActiveDocument.SaveAs strMail
ActiveDocument.Close (wdDoNotSaveChanges)
Set docType1 = Nothing
Set docType2 = Nothing
objWord.Quit
Set objWord = Nothing

End Sub
 
You error is caused by

Public docType1 As Word.Document
Public docType2 As Word.Document

being outside the procedure.
At the end of your procedure you set doctype1 & 2 to nothing, so the next time you run your code, VBA doesn't know how to handle them.

Either put your declarations inside the procedure, or put the
Set docType1 = Nothing
Set docType2 = Nothing

at the very end of the procedure that calls Start_Proc.


hth


Ben

----------------------------------------------
Ben O'Hara

"Where are all the stupid people from...
...And how'd they get so dumb?"
NoFX-The Decline
----------------------------------------------
 
Thanks Ben, I changed it as you said but it still get stuck in the same place

Sub Start_Proc()
Dim DocsMail As String
DocsMail = "C:\DocsMail\"
Dim WordDot As String
WordDot = "c:\Test.dot"
Dim WordDot2 As String
WordDot2 = "c:\Test2.dot"
Dim strName As String
Dim strMail As String
Dim strType As String
Dim docType1 As Word.Document
Dim docType2 As Word.Document

strMail = "c:\test1.doc"
strName = "data to insert"
Dim objWord As Word.Application
Set objWord = CreateObject("Word.Application")
Set docType1 = objWord.Documents.Open(WordDot, , ReadOnly)
Set docType2 = objWord.Documents.Open(WordDot2, , ReadOnly)

docType1.Select
Selection.HomeKey Unit:=wdStory '## Edit template ##
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.TypeText Text:=strName

Selection.EndKey Unit:=wdStory
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.TypeText Text:=strName

Selection.HomeKey Unit:=wdLine
Selection.MoveUp Unit:=wdLine, Count:=5
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.TypeText Text:=strName

Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveRight Unit:=wdCell, Count:=2
Selection.HomeKey Unit:=wdLine
docType1.Close (wdDoNotSaveChanges)
docType2.Close (wdDoNotSaveChanges)
Set docType1 = Nothing
Set docType2 = Nothing
objWord.Quit
Set objWord = Nothing

End Sub
 
Now you've done the same thing with your word object.
When you set objWord=nothing, you destroy that object. Then you don't set it again before you run the proc again.
Try this:

Sub Start_Proc()
Dim objWord As Word.Application
Set objWord = CreateObject("Word.Application")

Dim DocsMail As String
DocsMail = "C:\DocsMail\"
Dim WordDot As String
WordDot = "c:\Test.dot"
Dim WordDot2 As String
WordDot2 = "c:\Test2.dot"
Dim strName As String
Dim strMail As String
Dim strType As String
Dim docType1 As Word.Document
Dim docType2 As Word.Document

strMail = "c:\test1.doc"
strName = "data to insert"
Dim objWord As Word.Application
Set objWord = CreateObject("Word.Application")
Set docType1 = objWord.Documents.Open(WordDot, , ReadOnly)
Set docType2 = objWord.Documents.Open(WordDot2, , ReadOnly)

docType1.Select
Selection.HomeKey Unit:=wdStory '## Edit template ##
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.TypeText Text:=strName

Selection.EndKey Unit:=wdStory
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.TypeText Text:=strName

Selection.HomeKey Unit:=wdLine
Selection.MoveUp Unit:=wdLine, Count:=5
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.TypeText Text:=strName

Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveRight Unit:=wdCell, Count:=2
Selection.HomeKey Unit:=wdLine
docType1.Close (wdDoNotSaveChanges)
docType2.Close (wdDoNotSaveChanges)
Set docType1 = Nothing
Set docType2 = Nothing
objWord.Quit
Set objWord = Nothing

End Sub


----------------------------------------------
Ben O'Hara

"Where are all the stupid people from...
...And how'd they get so dumb?"
NoFX-The Decline
----------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top