none
Check macro enabled in word or not RRS feed

  • Question

  • Hi to all

    i developed a project to convert word to pdf. but now i have requirement to not to process macro enabled word document in c#. now i want to check whether current word or any document is macro enabled or not.

    Please help me with this guys.

    Thanks in Advance


    S. Mukesh Kumar

    Wednesday, April 4, 2012 4:55 PM

Answers

  • No, I mean that, if it's a docx file, you can be reasonably confident that it doesn't contain macros. Whilst it's possible someone might change the extension of a docm file to docx, Word won't allow the macros in such a file to run.

    Beyond that, the only reliable way of testing whether a docm or doc file contains macros is to open & test it. VBA code for that looks like:

    Sub Demo()
    With ActiveDocument
      If .HasVBProject Then
        'Do Something
      Else
        'Do Something Else
      End If
    End With
    End Sub

     


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Proposed as answer by Bruce Song Wednesday, April 18, 2012 8:13 AM
    • Edited by macropodMVP Wednesday, April 18, 2012 8:25 AM Added code for testing
    • Marked as answer by Bruce Song Monday, April 23, 2012 8:10 AM
    Thursday, April 5, 2012 12:15 PM

All replies

  • If it's Word 2007 & later, docx files cannot be macro-enabled. Files with a docm extension may or may not contain macros; usually, though, you wouldn't use that extension unless it was for saving a macro-enabled document.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, April 4, 2012 11:40 PM
  • Hi

    Thanks for your reply.

    So you mean to say that i cant check whether docx files contains macrs or not in c#


    S. Mukesh Kumar

    Thursday, April 5, 2012 7:55 AM
  • No, I mean that, if it's a docx file, you can be reasonably confident that it doesn't contain macros. Whilst it's possible someone might change the extension of a docm file to docx, Word won't allow the macros in such a file to run.

    Beyond that, the only reliable way of testing whether a docm or doc file contains macros is to open & test it. VBA code for that looks like:

    Sub Demo()
    With ActiveDocument
      If .HasVBProject Then
        'Do Something
      Else
        'Do Something Else
      End If
    End With
    End Sub

     


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Proposed as answer by Bruce Song Wednesday, April 18, 2012 8:13 AM
    • Edited by macropodMVP Wednesday, April 18, 2012 8:25 AM Added code for testing
    • Marked as answer by Bruce Song Monday, April 23, 2012 8:10 AM
    Thursday, April 5, 2012 12:15 PM