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

To automate mail-merging in VB and saving of mail-merged document

Status
Not open for further replies.

jche

Programmer
Nov 5, 2002
4
0
0
SG
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
 
It looks to me like ActiveDocument is a valid object while your working in Word (VBA environment), but when you want to reference the ActiveDocument from VB you'll have to reference it through the Word application object. So in your case I would give wrdApp.ActiveDocument a try.

The gap between theory and practice is not as wide in theory as it is in practice.
 
I lived through this in several apps with Word and Excel... let me know if you still have problems... we'll get more specific... for right now, if you are getting Err.Number = 462, then you as developer, or, the user, closed the background Word application with the first doc and now your application no longer can access it for future docs.

FIRST, using YOUR CODE..
Public wrdApp As Word.Application..........OK
'Public wrdDoc As Word.Document............COMMENT OUT

Dim file As String
Dim filenm As String
Dim wrdMailMerge As Word.MailMerge
'Dim wrdMerged As Word.Document ...........COMMENT OUT


file = "c:\CoverLetter\AutoFax3.doc"..........OK
filenm = "c:\faxdoc\AutoFax3_12345.doc"..........OK
----------------------------
'ADD A KILLWORD ROUTINE AND CALL IT BEFORE ANY CODING IN 'WORD ...ADD ROUTINE'
Public Sub KillWord()
On Error Resume Next
If Not wrdApp Is Nothing Then
wrdApp.ActiveDocument.Close wdDoNotSaveChanges
wrdApp.Visible = False
wrdApp.Quit
Set wrdApp= Nothing
End If
Exit Sub
ErrorHdl:
MsgBox "KillWord" & Chr(10) & Chr(10) & Chr(13) & Err.Description
End Sub
----------------------------
. ...COMMENT OUT
' Set wrdApp = CreateObject "Word.Application")
.....
AND PUT IN THESE TWO LINES:
Set wrdApp = New Word.Application
wrdApp.Add Template:="Normal.dot", NewTemplate:=False, DocumentType:=0
.....
note: ADDING 2 letters in a row when the first one has an edit error causes vb error 462 for no apparent reason (IN MY CASE IT HAPPENS when setting the top margin property), so FOR ANY ERROR bypass errors when setting these properties
AND PUT AT THIS POINT IN CODE:
On Error Resume Next
----------------------------
'wrdApp.Visible = True ..........OK BUT PUT IT AFTER YOU SET IT
----------------------------
' Set wrdDoc = wrdApp.Documents.Open(file) ....COMMENT OUT
.....
AND PUT IN THESE TWO LINES:
Set wrdDoc= New Word.Application
wrdDoc.Documents.Open g_sPath & file

wrdApp.Visible = True 'NOW OK HERE
----------------------------
I SEE THAT AN ERROR IS ABOUT TO HAPPEN AS YOU ARE WORKING BETWEEN TWO DIFF OBJECTS... OPENNING ONE-- AND SAVING IT WITH THE OTHER... I'D DO A FILE SAVE AS TO MY WRDDOC BEFORE SETTING ANOTHER OBJECT AND PUT IN THIS LINE NOW EVEN IF NEEDS REPEATING AFTER MORE MANIPULATION:

'ready to begin by copying the doc
wrdDoc.Application.ActiveDocument.SaveAs FileName:=filenm, _
FileFormat:=wdFormatDocument, _
LockComments:=False, Password:="", AddToRecentFiles:= _
True, WritePassword:="", _
ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, _
SaveFormsData:=False, _
SaveAsAOCELetter:=False



----------------------------
THEN SET ANOTHER OBJECT:

Set wrdMailMerge = wrdDoc.MailMerge
wrdMailMerge.Destination = wdSendToNewDocument
wrdMailMerge.DataSource.FirstRecord = wdDefaultFirstRecord
wrdMailMerge.DataSource.LastRecord = wdDefaultLastRecord
wrdMailMerge.Execute False

---------------
COMMENT OUT
'Set wrdMerged = ActiveDocument '*** this is the errorline
AND ADD
Set wrdMerged = wrdDoc.Application.ActiveDocument
SO IT KNOWS THE OBJECT
------------
THEN I'D PROCEED...

wrdMerged.SaveAs FileName:=filenm, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
---------------------
COMMENT OUT:

' wrdDoc.Close False
' wrdMerged.Close False

ADD THIS IN PLACE after each letter EXCEPT FOR THE APP OBJECT!!!!!!!
I THINK THIS IS A PROBLEM:
Set wrdDoc= Nothing
Set wrdMailMerge = Nothing
Set wrdMerged = Nothing
Set wrdDoc = Nothing
'''''''''''''''''COMMENT OUT Set wrdApp = Nothing
Set wrdMailMerge = Nothing







ABSOLUTE LAST THING TO DO ONLY ONCE WHEN EXITING APP:
If Not wrdApp Is Nothing Then
wrdApp.ActiveDocument.Close (wdSaveChanges)
wrdApp.Visible = False
wrdApp.Quit
wrdApp= Nothing
End If
----------------------------------------------------------
By doing this way, you make the application save your document changes and close Word automatically. If you, or the user is closing out the APP OBJECT then trying to do a 2nd letter... while app is running, then you WILL getthat error... If case USER hits the File Exit in Word put an error handler in:

On Error GoTo ErrorHdl
If Err.Number = 462 Then
MsgBox "You closed the background Word application so this application can no longer access it. The application will save your document changes and close Word automatically. To avoid this message in the future, please allow this application to close Word."

Good Luck, Rdc, Developer
email me at dimariacrowe@aol.com if you are still stuck!!!
 
I don't have much experience with this kind of development, but I am executing mailmerges from a VB.NET application. The code in the original post looks correct up to the error point except for one line that I have in my code. It would look like this:

Set wrdMailMerge = wrdDoc.MailMerge

wrdMailMerge.OpenDataSource([dataSourceName])
wrdMailMerge.Destination = wdSendToNewDocument
...

Do you have On Error Resume Next enabled? If so, turn it off. You've opened a document in the code. Even if the mailmerge didn't run, there still should be an active document.

Hope this helps.
 
I am also use VB to control Word for mail merge. I agree with "TerraSamba".

use "wrdApp.ActiveDocument", instead of "worddoc".

And

If you want to open WordApp every time:
Set wrdApp = CreateObject("Word.Application")
You need to close it when finish:
wrdapp.quit false
Else
wordDoc.Close False <-- This code close the Worddoc document
wordApp.ActiveDocument.Close False <-- This code close the mailmerged document.

Endif

Of course you can try the code from &quot;RDC&quot; first.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top