Question

Using Word Automation, I want to save the active document programmatically. In Office 2007/2010, the document needs to be saved explicitly as "macro enabled" to preserve any VBA code in that document.

Rather than asking the user to choose, I would like my application to be able to determine if there is VBA code in the active document. Is that possible?

Was it helpful?

Solution

Yes this can be determined via the HasVBProject property. For example:

If ActiveDocument.HasVBProject = True Then

    'Code to save as .dotm

Else

    'Code to save as .dotx

End If

For those who might stumble upon this post later, it is worth noting that this code should be placed outside the document being tested for the presence of macros (otherwise it would detect itself). Two often-used options would be to access the code from an external application or from a template stored in Word's Startup folder.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top