Discussion Locked

Word Automation

0 helpful
8 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I created a document assembly program in using Word's (2000) mail merge, VBA and Outlook. When the merge is performed all of information is inserted into a new document. I need to find a way to automate the closing of the original document withoutsaving the changes. Does anyone have any ideas.

8 replies

by peyison

You could create a VBA routine to close the document. Something like:

Public Sub CloseOrig()
'--replace MyDoc.doc with the name of your document
With Application.Documents("MyDoc.doc")
'--set the saved flag so Word won't ask if you want tosave first
.Saved = True
.Close
End With
End Sub

You can also use a more generic version:

Public Sub CloseOrig(asDocName as String)
With Application.Documents(asDocName)
.Saved = True
.Close
End With
End Sub

Hope this helps.

by bytemi

The 1st script did not run properly, when I entered my document name it told me that it did not exist. I tried entering the full path but that did not work either. So I tried the bottom one and it worked, but it closed the new document instead of the original document.

This is what I came up with yesturday after posting my question. The problems is it does not go back to the orignial document it just picks a window and closes it.

Sub CloseSource()
ActiveWindow.Previous.Activate ActiveDocument.Close (wdDoNotSaveChanges)
End Sub

I really appreciate your help, thank you.

by andersos

You could save the document using saveas.

Example:

ActiveDocument.SaveAs FileName:="Text.rtf", FileFormat:=wdFormatRTF

See saveas in the microsoft word visual basic help file. I use this command in access with excel documents quite often.

by wayne m.

Just a pure guess here, but could you "Save As" the original document to the new name, then do the merge? If you don't want to save the new document either, you may have to explicitly delete the file.

by bytemi

The problem is not saving the template that holds the merge codes, because I would prefer it if it was not saved. Once the merge is performed it opens a new document and leave the template open. Having the two documents open that are almost exactly alike. I only want to save the new document not the template. I know how to close the template without saving automatically, it is getting back to the template once the new document is opened that is throwing me for a loop.

by bytemi

Here is the answer. Please feel free to use this code if you need it. If you have any question feel free to e-mail me.

' This line needs to be entered when the source document is Opened
Set objSrcDoc = ActiveDocument
' This line needs to be entered after the documents are merged.
Set objDstDoc = ActiveDocument
' This will close the source document
objSrcDoc.Close savechanges:=wdDoNotSaveChanges

This discussion is closed to new replies.