Solved

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

Posted on 2013-11-30
11
3,591 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

730 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