# Close Word document within excel vba



## eduzs (Mar 13, 2017)

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)


```
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.


----------



## Macropod (Mar 13, 2017)

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:

```
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.


----------



## eduzs (Mar 13, 2017)

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.


----------



## Macropod (Mar 14, 2017)

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.


----------



## eduzs (Mar 14, 2017)

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.


----------



## Macropod (Mar 14, 2017)

eduzs said:


> 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)!


eduzs said:


> 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.


eduzs said:


> 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!


----------



## eduzs (Mar 14, 2017)

Thanks. This topic is now concluded and can be closed.


----------

