Hi all
I am doing the automation of mail-merging of Word Document in VB. After mail-merging, I need to save it in separate documents. It is okay for the first time, but from second time onwards, I have the error :-
This command is not available because no document is open
My program goes like this and I have put a comment beside the line that causes the error :-
-------------------------------------------------------
Public wrdApp As Word.Application
Public wrdDoc As Word.Document
Dim file As String
Dim filenm As String
Dim wrdMailMerge As Word.MailMerge
Dim wrdMerged As Word.Document
file = "c:\CoverLetter\AutoFax3.doc"
filenm = "c:\faxdoc\AutoFax3_12345.doc"
Set wrdApp = CreateObject("Word.Application"
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open(file)
Set wrdMailMerge = wrdDoc.MailMerge
wrdMailMerge.Destination = wdSendToNewDocument
wrdMailMerge.DataSource.FirstRecord = wdDefaultFirstRecord
wrdMailMerge.DataSource.LastRecord = wdDefaultLastRecord
wrdMailMerge.Execute False
Set wrdMerged = ActiveDocument '*** this is the errorline
wrdMerged.SaveAs FileName:=filenm, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
wrdDoc.Close False
wrdMerged.Close False
Set wrdMerged = Nothing
Set wrdDoc = Nothing
Set wrdApp = Nothing
Set wrdMailMerge = Nothing
----------------------------------------------------------
When I copy this codes to run in MSWord Macro, it's ok.
Pls help... thanks alot for your attention..
Rgds
Jennifer Chew
I am doing the automation of mail-merging of Word Document in VB. After mail-merging, I need to save it in separate documents. It is okay for the first time, but from second time onwards, I have the error :-
This command is not available because no document is open
My program goes like this and I have put a comment beside the line that causes the error :-
-------------------------------------------------------
Public wrdApp As Word.Application
Public wrdDoc As Word.Document
Dim file As String
Dim filenm As String
Dim wrdMailMerge As Word.MailMerge
Dim wrdMerged As Word.Document
file = "c:\CoverLetter\AutoFax3.doc"
filenm = "c:\faxdoc\AutoFax3_12345.doc"
Set wrdApp = CreateObject("Word.Application"
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open(file)
Set wrdMailMerge = wrdDoc.MailMerge
wrdMailMerge.Destination = wdSendToNewDocument
wrdMailMerge.DataSource.FirstRecord = wdDefaultFirstRecord
wrdMailMerge.DataSource.LastRecord = wdDefaultLastRecord
wrdMailMerge.Execute False
Set wrdMerged = ActiveDocument '*** this is the errorline
wrdMerged.SaveAs FileName:=filenm, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
wrdDoc.Close False
wrdMerged.Close False
Set wrdMerged = Nothing
Set wrdDoc = Nothing
Set wrdApp = Nothing
Set wrdMailMerge = Nothing
----------------------------------------------------------
When I copy this codes to run in MSWord Macro, it's ok.
Pls help... thanks alot for your attention..
Rgds
Jennifer Chew