Closing Word in Automated VBA Process (1 Viewer)

alguzman

Registered User.
Local time
Today, 13:04
Joined
Aug 2, 2001
Messages
63
I have a mail merge button with the following code.

Dim objWord As Word.Document
Set objWord = GetObject("G:\MSAPPS\Share\Dueshor2.doc", "Word.Document")
' Make Word visible.
objWord.Application.Visible = True
' Set the mail merge data source as the Northwind database.
objWord.MailMerge.OpenDataSource _
Name:="G:\MSAPPS\Share\NMember.mdb", _
LinkToSource:=True, _
Connection:="Query qryinvoice", _
SQLStatement:="Select * from [qryinvoice]"
' Execute the mail merge.
objWord.MailMerge.Execute
objWord.MailMerge.Destination = wdSendToNewDocument
objWord.MailMerge.Execute
objWord.Application.Options.PrintBackground = False
objWord.Application.ActiveDocument.PrintOut
objWord.Application.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
objWord.Application.Visible = False

At the end word is not visible and appears to be closed but when you try to shut down it asks you if you want to save the changes to the word document. How can I close word with out saving it in vb code. It seems that "SaveChanges:=wddonotsavechanges" is not working.
 

David R

I know a few things...
Local time
Today, 08:04
Joined
Oct 23, 2001
Messages
2,633
Will objWord.Application.Quit acQuitSaveNone work?

David R
 

Fornatian

Dim Person
Local time
Today, 13:04
Joined
Sep 1, 2000
Messages
1,396
try:

objWord.Application.Quit wdDoNotSaveChanges

If not look at the Quit method in Word VBA for the exact syntax but it is much like the above

Ian
 

Fornatian

Dim Person
Local time
Today, 13:04
Joined
Sep 1, 2000
Messages
1,396
Also you need to release the variable by using

objWord.close
Set objWord = Nothing

Ian

Message for David R, I haven't forgot about you I'm just puzzling over it in my spare minutes...
 

alguzman

Registered User.
Local time
Today, 13:04
Joined
Aug 2, 2001
Messages
63
Thanks guys I will give both of the suggestions a shot sometime today. Thanks for your help.
 

alguzman

Registered User.
Local time
Today, 13:04
Joined
Aug 2, 2001
Messages
63
I tried it and still doesn't work. Either I get a runtime error or the document asks to be saved and I have to manually close word.

Dim objWord As Word.Document
Set objWord = GetObject("G:\MSAPPS\Share\Dueshor2.doc", "Word.Document")
' Make Word visible.
objWord.Application.Visible = True
' Set the mail merge data source as the Northwind database.
objWord.MailMerge.OpenDataSource _
Name:="G:\MSAPPS\Share\NMember.mdb", _
LinkToSource:=True, _
Connection:="Query qryinvoice", _
SQLStatement:="Select * from [qryinvoice]"
' Execute the mail merge.
objWord.MailMerge.Execute
objWord.MailMerge.Destination = wdSendToNewDocument
objWord.MailMerge.Execute
objWord.Application.Options.PrintBackground = False
objWord.Application.ActiveDocument.PrintOut
objWord.Application.ActiveDocument.Quit wdDoNotSaveChanges
objWord.Close
objWord.Application.Quit
Set objWord = Nothing
End Sub

I tried objword.application.quit acquitsavenone and objword.application.quit wddonotsavechanges
objword.close
set objword=nothing

Any other suggestions.
 

Fornatian

Dim Person
Local time
Today, 13:04
Joined
Sep 1, 2000
Messages
1,396
1.First quit the application
2.Then close the object
3.Then set it to nothing to release it.

objWord.Application.Quit wdDoNotSaveChanges
objWord.Close
Set ObjWord = Nothing

This should work, otherwise I will post my code that I've used before if you still have problems

Ian
 

alguzman

Registered User.
Local time
Today, 13:04
Joined
Aug 2, 2001
Messages
63
Hey fornation, Here is my exact code:
Dim objWord As Word.Document
Set objWord = GetObject("G:\MSAPPS\Share\Dueshor2.doc", "Word.Document")
' Make Word visible.
objWord.Application.Visible = True
' Set the mail merge data source as the Northwind database.
objWord.MailMerge.OpenDataSource _
Name:="G:\MSAPPS\Share\NMember.mdb", _
LinkToSource:=True, _
Connection:="Query qryinvoice", _
SQLStatement:="Select * from [qryinvoice]"
' Execute the mail merge.
objWord.MailMerge.Execute
objWord.MailMerge.Destination = wdSendToNewDocument
objWord.MailMerge.Execute
objWord.Application.Options.PrintBackground = False
objWord.Application.ActiveDocument.PrintOut
objWord.Application.Quit wdDoNotSaveChanges
objWord.Close
Set objWord = Nothing
End Sub
It opens word merges the access info prints and then hangs there after the print. I don't know what I'm doing wrong here.
 

Fornatian

Dim Person
Local time
Today, 13:04
Joined
Sep 1, 2000
Messages
1,396
..can't see the wood for the trees, I now see your problem. You have an object reference set to the word document NOT the application - therefore although you have set a manageable reference to the document you can't close the word application because you have no object to manipulate.(Phew!).

Try this code instead - note don't delete your original as I have not tested this one:

Code:
Dim objWord As Word.Application
Dim TheDoc As String

TheDoc = "G:\MSAPPS\Share\Dueshor2.doc"

    'Switch to Microsoft Word so it won't go away when you finish.
    On Error Resume Next
    AppActivate "Microsoft Word"
    
    'If Word isn't running, start and activate it
    If Err Then
    Shell "c:\Program Files\Microsoft Office\Office\" _
        & "Winword /Automation", vbHide
    AppActivate "Microsoft Word"
    End If
    On Error GoTo 0
    
    'Get an Application object so you can automate Word.
    Set appWord = GetObject(, "Word.Application")
    
    'Open a document
    appWord.Documents.Open Worddoc
    
    With appWord.ActiveDocument.MailMerge
    .OpenDataSource _
    Name:="G:\MSAPPS\Share\NMember.mdb", _
    LinkToSource:=True, _
    Connection:="Query qryinvoice", _
    SQLStatement:="Select * from [qryinvoice]"
    .Execute
    .Destination = wdSendToNewDocument
    .Execute
    End With
    appWord.Options.PrintBackground = False
    appWord.ActiveDocument.PrintOut
    appWord.Quit wdDoNotSaveChanges
    
Set appWord = Nothing

Ian




[This message has been edited by Fornatian (edited 03-09-2002).]
 

Users who are viewing this thread

Top Bottom