Macro doesn't work

K

Kerry

I have a macro which saves my Word doc as a pdf based on words within the
document itself. Sometimes I get an error and the macro stops on the
following line:

ActiveDocument.ExportAsFixedFormat OutputFileName:=sFileName, _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False,
OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=False, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False

sFileName is a string

If I close out of all my Office apps (Outlook, Word 2007) then reopen Word
and invoke the macro right away, it will work beautifully. I don't know if
it has to do with the other apps or not, but if I restart everything fresh,
it works again. Is the problem with the macro or something else?
 
G

Gordon Bentley-Mix

Probably best asked in the VBA newsgroup, but since I'm here...

Two questions:
1. What does the error say?
2. What does the code that creates sFileName look like?
 
K

Kerry

Wouldn't you know, it's working right now and I don't know how to get it to
replicate the problem, so I don't know exactly what the error says.

The code that creates sFileName is:

sFileName = "C:\Users\Kerry.Atkinson\Documents\" & (year + "-" + base +
"-" + Rank + first + last) & ".pdf"

But like I said the whole macro works beautifully, and when it doesn't, all
I have to do is close out of the apps and it works again. I have noticed,
though, that if I just restart Word but not Outlook, then it still doesn't
work. I have to close out of both for it to work again. I don't know if it
makes a difference, but I'm on Vista.

thanks
 
G

Gordon Bentley-Mix

Kerry,

If it does crash again, make a careful note of the error message and post
back. I'll watch this thread for any further developments. However, I can't
see anything glaringly wrong with the code - although it's not exactly clear
what "year", "base", etc. are and where they come from. (I'm assuming that
they're variables who's values are extracted from the content of the
document, yes?) It could be that one of these values goes missing under
certain circumstances, and this is causing sFile to be malformed somehow.
When the error does occur, if you hover over sFile in the VBA editor, it
should show you the current value. Something to investigate...

As for having to close Outlook as well as Word, when the code crashes it's
apparently doing so in a way that cause the VBA compiler to hang - possibly
because memory allocated for stuff associated with the code isn't being
released. If you are running Office 2003 or older and using Word as your
email editor in Outlook, then the "hung" instance of Word is still running
even after Word is closed. Consequently, you have to close Outlook before
Word will truly restart as a fresh instance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top