G
Gal Zilberman
Hi All
I'm trying to export a report into word and then run some VB code on it from
Access.
My code exports the report, changes the font to Arial and Adds my company's
letter head to it, everything works fine on the first run, but then the code
crashes the following times (Error 462).
What am I not closing?
thanks
Gal
here's the code (for the Word changes only)
Function UpdateWordFormat(strFileName As String)
On Error GoTo Err_UpdateWordFormat
' Vars
Dim wdApp As Word.Application
Dim wdDcmnt As Word.Document
Dim strTemplate As String
' Init
strTemplate = "C:\Letterhead.dot"
' Word Macro Code
' Create a Microsoft Word object.
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
' Open the Document to which you exported the data to.
Set wdDcmnt = wdApp.Documents.Open(strFileName)
' Change font to Arial
wdDcmnt.Range.WholeStory
With wdDcmnt.Range
.Font.Name = "Arial"
End With
' Save the Document in case of Errors
With wdDcmnt
.Save
End With
' Add Letterhead - Error occurs here
Set myLetter = wdDcmnt.CreateLetterContent( _
DateFormat:="", IncludeHeaderFooter:=True, PageDesign:= _
strTemplate, LetterStyle:=wdFullBlock, _
Letterhead:=False, LetterheadLocation:=wdLetterTop, LetterheadSize:=
_
CentimetersToPoints(0), RecipientName:="", RecipientAddress:="", _
Salutation:="", SalutationType:=wdSalutationOther,
RecipientReference:="" _
, MailingInstructions:="", AttentionLine:="", Subject:="",
CCList:="", _
ReturnAddress:="", SenderName:="", Closing:="", SenderCompany:="", _
SenderJobTitle:="", SenderInitials:="", EnclosureNumber:=0,
InfoBlock:= _
False, RecipientCode:="", RecipientGender:=wdGenderNeutral, _
ReturnAddressShortForm:="", SenderCity:="", SenderCode:="",
SenderGender _
:=wdGenderNeutral, SenderReference:="")
wdDcmnt.RunLetterWizard LetterContent:=myLetter, WizardMode:=False
' Save the Document
With wdDcmnt
.Save
End With
Exit_UpdateWordFormat:
Set myLetter = Nothing
Set wdDcmnt = Nothing
Set wdApp = Nothing
Exit Function
Err_UpdateWordFormat:
MsgBox CStr(Err) & " " & Err.Description
wdDcmnt.Close
wdApp.Quit
Resume Exit_UpdateWordFormat
End Function
I'm trying to export a report into word and then run some VB code on it from
Access.
My code exports the report, changes the font to Arial and Adds my company's
letter head to it, everything works fine on the first run, but then the code
crashes the following times (Error 462).
What am I not closing?
thanks
Gal
here's the code (for the Word changes only)
Function UpdateWordFormat(strFileName As String)
On Error GoTo Err_UpdateWordFormat
' Vars
Dim wdApp As Word.Application
Dim wdDcmnt As Word.Document
Dim strTemplate As String
' Init
strTemplate = "C:\Letterhead.dot"
' Word Macro Code
' Create a Microsoft Word object.
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
' Open the Document to which you exported the data to.
Set wdDcmnt = wdApp.Documents.Open(strFileName)
' Change font to Arial
wdDcmnt.Range.WholeStory
With wdDcmnt.Range
.Font.Name = "Arial"
End With
' Save the Document in case of Errors
With wdDcmnt
.Save
End With
' Add Letterhead - Error occurs here
Set myLetter = wdDcmnt.CreateLetterContent( _
DateFormat:="", IncludeHeaderFooter:=True, PageDesign:= _
strTemplate, LetterStyle:=wdFullBlock, _
Letterhead:=False, LetterheadLocation:=wdLetterTop, LetterheadSize:=
_
CentimetersToPoints(0), RecipientName:="", RecipientAddress:="", _
Salutation:="", SalutationType:=wdSalutationOther,
RecipientReference:="" _
, MailingInstructions:="", AttentionLine:="", Subject:="",
CCList:="", _
ReturnAddress:="", SenderName:="", Closing:="", SenderCompany:="", _
SenderJobTitle:="", SenderInitials:="", EnclosureNumber:=0,
InfoBlock:= _
False, RecipientCode:="", RecipientGender:=wdGenderNeutral, _
ReturnAddressShortForm:="", SenderCity:="", SenderCode:="",
SenderGender _
:=wdGenderNeutral, SenderReference:="")
wdDcmnt.RunLetterWizard LetterContent:=myLetter, WizardMode:=False
' Save the Document
With wdDcmnt
.Save
End With
Exit_UpdateWordFormat:
Set myLetter = Nothing
Set wdDcmnt = Nothing
Set wdApp = Nothing
Exit Function
Err_UpdateWordFormat:
MsgBox CStr(Err) & " " & Err.Description
wdDcmnt.Close
wdApp.Quit
Resume Exit_UpdateWordFormat
End Function