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
  •  | 
  •  

문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top