Solved

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

Posted on 2013-11-30
11
3,045 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

760 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

19 Experts available now in Live!

Get 1:1 Help Now