PDA

View Full Version : Word crashes when I use VBA to close documents



stevetalaga
03-03-2014, 08:16 PM
I have a piece of code that seems to work reasonably well until it comes to tidying up at the end and closing the Word documents. The code takes the open document, does a bit of formatting and saves a second copy of the document with a prefix to the file name (eg cc_source.doc) and a third copy as a PDF file.

All works well up to this point but when I try to close the new document, Word crashes. I suspect that it is something to do with closing the document that has the VBA code in its' template. I would like the code to close all documents that it was working on at the end to give a "clean" appearance to the user (but if the user has other documents open that are not connected with this code I would like to leave them open).

I'm sure there must be a way of closing the documents that my code has specifically worked on without crashing Word but I am struggling to find it. Any help would be appreciated.

Here's a trimmed down version of my code that saves the files and tries to close them (The password is set properly in the real code!):


Sub Test()
Dim strPwd As String
strPwd = "123456qwerty"

Documents.Open FileName:="C:\Test\source.doc"
ActiveDocument.SaveAs2 FileName:="C:\Test\CC_source.pdf", FileFormat:=wdFormatPDF, WritePassword:=strPwd

' Reopen the source file to create the color coded text file
Documents.Open FileName:="C:\Test\source.doc"
ActiveDocument.SaveAs2 FileName:="C:\Test\CC_source.doc", FileFormat:=wdFormatDocument, WritePassword:=strPwd

' Close the new file
Documents("C:\Test\CC_source.doc").Close
End Sub

fumei
03-03-2014, 09:39 PM
What do you mean by "Reopen the source file"? Does that mean the code you are working with is in that file? Why reopen if you have not changed anything? The original is still on disk. If you have not changed anything (just saved it as PDF), then if you just want to save it with the new name (from source.doc to CC_source.doc) you can do that without reopening.

Please expand on "crash". What exactly happens?

I would like the code to close all documents that it was working on at the end to give a "clean" appearance to the user (but if the user has other documents open that are not connected with this code I would like to leave them open). This is of course a good idea. The BEST way is to use document objects, rather than ActiveDocument. That way you can be very specific and not touch any other documents that may be open.

All that said, it is hard to say what the problem may be. The code as posted does not give a good enough clue.

fumei
03-03-2014, 09:47 PM
Ooops. I see that you did some formatting (but not in the code you posted). OK. So the change from "source.doc" to "CC_source.doc" is just a name change? CC_source will NOT have the format changes?

stevetalaga
03-03-2014, 10:22 PM
Sorry I was unclear. I didn't want to post lots of code that wasn't related to the problem and would mask the issue. In trying to simplify the explanation I didn't provide enough detail. If I just run the code that I posted in my previous post that also causes Word to crash. No error code, just the attached screen shot.
11362

Some background
1) The users are medical transcribers who create medical reports based on doctors notes and then color code blocks of text within their report as the basis for billing the client. The color coded file needs to be preserved in case the billing is queried (my boss wants this saved as both a PDF file and a password protected Word file). Also the client needs to receive a black and white version of the report without the color coding.
2) The VBA code is stored in a .dotm file in the Word Startup directory so that it is always available.
3) User invokes the code when a document is already open by clicking on a button on the Quick Access Toolbar
(the code checks if there is an open document and only runs if it finds an open document)

What my actual code does is as follows:
1) Takes the ActiveDocument (eg source.doc which is a color coded medical report) and saves it under a new file name that is password protected (eg cc_source.doc) to create an instance of the color coded document at the point when the billing is calculated.
2) Saves another version of the same color coded report in PDF format.
3) Opens the original source document, selects all of the text and makes it black and then saves it with another prefix (eg black_source.doc)
4) Closes CC_source.doc
5) Closes black_source.doc
(Actually it crashes before number 4 above.

Hope this helps to explain the problem.

Frosty
03-05-2014, 02:27 PM
Word crashes can be version specific... what version of word are you in?Have you tested this code when no other addins are loaded (i.e., start > run > winword /a -- then manually load your .dotm file in the Developer Tab > Templates and Addins, then run your code). If the crash still happens then, I would suggest testing on a totally new word document created in that "clean" winword process, save it locally, give it some dummy text, and then try the code again.There are a lot of things that could go wrong on the crash. I'll try to see if I can duplicate on a machine, but I suspect it's going to have something to do with the source document.I have had word crashes during the above type or process in a combination with legacy documents (.doc format), the Quick Styles Gallery showing, and another addin doing additional processing during the Document_Close event. I could eliminate the crash by hiding the Quick Styles Gallery (which is something you could do automatically in your .dotm)

snb
03-06-2014, 03:03 AM
Sub M_snb()
with Getobject("C:\Test\source.doc")
.SaveAs2 "C:\Test\CC_source.pdf", wdFormatPDF, WritePassword:="123456qwerty"
.SaveAs2 "C:\Test\CC_source.doc", wdFormatDocument, WritePassword::="123456qwerty"
.close
end with
End Sub