Solved

How do I close a Word document in my Excel VBA application ?

Posted on 2013-11-30
11
3,185 Views
Last Modified: 2013-11-30
Hi All,

I have an Excel 2010 VBA application in which I open a Word document and save it with a different filename.

When I close the Word document the 'copied' version is still open. It might be obvious but how do I close this 'Doc' file within my VBA code ?

The code looks like this....

   Dim WordApp As Object
   Dim WordDoc As Object

      WordDoc.Save
      WordApp.Documents.Add WordDoc.FullName
      WordDoc.SaveAs strDocumentName
      MsgBox "Your Word document has been saved as " & strDocumentName, vbInformation

      WordDoc.Close
0
Comment
Question by:Tocogroup
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 13

Expert Comment

by:duncanb7
ID: 39686848
Is it WordDoc.quit ? it might work

Duncan
0
 

Author Comment

by:Tocogroup
ID: 39686863
Fraid it didn't. I got an Error 438 - Object doesn't support this property or method
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39686880
Try

wordapp.activedocument.close
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 39686888
Probably, you want to close word application not word document since
you care the copied one is still open
 if so, just try  this

WordApp.Quit
set WordApp = Nothing
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39686893
Set the document object when you open it in the first place, otherwise there is confusion about which document is being worked on.
    Dim WordApp As Object 'Word.Application
    Dim WordDoc As Object 'Word.Docunent
    '...
    
    Set WordDoc = WordApp.Documents.Open("C:\MyFolder\MyFile.doc")
    
    WordDoc.SaveAs strDocumentName
    MsgBox "Your Word document has been saved as " & strDocumentName, vbInformation
    WordDoc.Close

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Expert Comment

by:duncanb7
ID: 39686904
if you just want to close all documents not application, you can try this

'--Loop Through open documents
With WordApp
Do Until .Documents.Count = 0

'--Close with save
.Documents(1).Close SaveChanges:=wdSaveChanges

'--Close no save
'--.Documents(1).Close SaveChanges:=wdDoNotSaveChanges

Loop
End With

Open in new window


Duncan
0
 

Author Comment

by:Tocogroup
ID: 39686921
Neither of your suggestions worked.
The second one didn't recognise the variable wdDoNotSaveChanges
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 250 total points
ID: 39686923
Can you say what happens with my suggestion, please?

wdDoNotSaveChanges is a constant defined in the Microsoft Word object library, but you are using late binding
0
 
LVL 13

Accepted Solution

by:
duncanb7 earned 250 total points
ID: 39686935
GrahamSkan is correct,

Tocogroup,Please read it that might help you about late or early binding.

http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=169:automate-microsoft-word-from-excel-using-vba&catid=79&Itemid=475

If not, I have no more comment in this thread.

Have a nice day

Duncan
0
 

Author Closing Comment

by:Tocogroup
ID: 39686959
Hi Duncan and Graham,

Thank you both very much for your suggestions regarding wdDoNotSaveChanges.

I'm splitting the points on this one as I combined your codes and got there in the end as well as having understood how it works. It's a big read to close two documents !!

Much appreciated
Toco
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 39686962
Thanks for your pt and  Graham's reminder is a key solution in this thread

have a nice day

Duncan
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now