Close Word document within excel vba

eduzs

Well-known Member
Joined
Jul 6, 2014
Messages
704
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hi there,

I was workind in a code to:

If there's only one word app window opened, close word app without saving changes.

If there's omore than one word document opened, close the active document without saving changes.

This is the code:

(wordapp is an object classe word.application)

Code:
    If wordapp.ActiveDocument.Name = "Capa.docx" And wordapp.Windows.Count = 1 Then
    
        wordapp.Quit SaveChanges:=wordapp.wdDoNotSaveChanges
        
    Else
    
         If wordapp.ActiveDocument.Name = "Capa.docx" Then  wordapp.ActiveDocument.Close SaveChanges:=wordapp.wdDoNotSaveChanges
    
    End If

The code is not working :(

I think that the problem is within parsing "SaveChanges:=wordapp.wdDoNotSaveChanges".

Thanks.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Your code isn't trying to terminate the Word session; it's only trying to close a particular document.

A simpler approach is to explicitly run your own Word instance, which you can terminate when finished. For example:
Code:
Sub Demo()
'Note: A reference to the Word library must be set, via Tools|References
Dim wdApp As New Word.Application, wdDoc As Word.Document
Const StrDocNm As String = "Full document name & path"
If Dir(StrDocNm) = "" Then Exit Sub
Set wdDoc = wdApp.Documents.Open(Filename:=StrDocNm, ReadOnly:=False, AddToRecentfiles:=False)
With wdDoc
    'process the document
    
    'save & close
    .Close SaveChanges:=True
End With
wdApp.Quit
Set wdDoc = Nothing: Set wdApp = Nothing
End Sub
Testing whether Word is already running and conditionally starting and ending it accordingly - or using the existing session - is much more involved; it's something you'd probably only want to do if the user running the macro may or may not already have the document opened.
 
Upvote 0
I realy need to test if there's more than 1 instance of word, so the code will not close other opened documents, nor leave a "blank" word instance opened after printing, avoidind any prompts.

My code opens a word document, prints this document based on cells values, then closes without saving the "docx" file.

Also, it not creates a new word object if there's one instace of word runing, avoidind problems with ".dot" already opened, etc.
 
Last edited:
Upvote 0
The code I posted will not interfere with any Word document that might already be opened. The fact it creates a new instance ensures that. Neither will it create "problems with ".dot" already opened, etc." or leave an empty Word instance running. You really should have tested the code before posting...

If you want to guard against any changes being saved, you could change:
ReadOnly:=False
to:
ReadOnly:=Truee
and/or change:
.Close SaveChanges:=True
to:
.Close SaveChanges:=False
Those changes are both obvious and trivial.
 
Upvote 0
Thanks I will try the changes.
The problem might be that excel vba does not allow "wdDoNotSaveChanges", instead I should inform raw values, like 0, 1, -1, true, false.
I will also add wordapp.displayalerts = 0.
In fact, I didn't try your first code cause I don't want to create new instance, cause this is causing some problems here,
those problems also ocurrs when opening multiple excel instances with the boring message "PERSONAL.XLSB" is already open.
I will update later.



 
Last edited:
Upvote 0
The problem might be that excel vba does not allow "wdDoNotSaveChanges", instead I should inform raw values, like 0, 1, -1, true, false.
That is nonsense. Again, you should try the code before making such claims. Even if it was true, the code I posted doesn't use it (and doesn't need to)!
I will also add wordapp.displayalerts = 0.
I don't know what you expect to achieve by that. In all likelihood it is quite unnecessary.
In fact, I didn't try your first code cause I don't want to create new instance, cause this is causing some problems here
How could you possibly 'know' that when you haven't tried the code? Your "PERSONAL.XLSB" problems have nothing to do with the code I posted!
 
Last edited:
Upvote 0
Thanks. This topic is now concluded and can be closed.
 
Upvote 0

Forum statistics

Threads
1,221,525
Messages
6,160,328
Members
451,637
Latest member
hvp2262

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top