Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-11-30
11
Medium Priority
?
4,426 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1000 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 1000 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

609 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