Solved

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

Posted on 2013-11-30
11
3,321 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
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…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

777 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