Using Word automation, is it possible to find out if the active document contains VBA code?

StackOverflow https://stackoverflow.com/questions/7171916

  •  11-01-2021
  •  | 
  •  

Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top