Results 1 to 9 of 9
  1. #1
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Code in Document, Not Template (Word XP)

    Once again, I humble myself before you...

    We are finding that many documents we're required to process contain VBComponents in the document itself. Our problem is that this code hijacks native Word command (such as FileSave, FileSaveAs or FileClose) and interfere with a larger, overall process.

    What I'd like to be able to do is to create a simple tool that will, at the very least, allow the user to quickly determine if there is "code behind the document" without them having to learn to utiliize the VB Editor. (Ideally, it like to also give or have the option of either disabling or stripping such code from the document, since once the ownership is transferred to our area, we certainly don't need or want anything other than a plain vanilla document.)

    My eyes started to glaze over as I delved into the VB Extensions object, and the terminology started to elude me. Can anyone kindly steer me towards good advice or resource? Thank you very much, in advance.
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Code in Document, Not Template (Word XP)

    The easiest way might be to set Macro security to High, then sign your own trusted Macros so that they are able to run.

    StuartR

  3. #3
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Code in Document, Not Template (Word XP)

    To enable that to run you have to set the check box Tools > Macro > Security > Trusted Publishers > Trust Access to Visual Basic Project

    This is a little risky, as it makes it possible for code to add/remove other VBA code

    StuartR

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Code in Document, Not Template (Word XP)

    Stuart,

    Thanks for pointing that out. I just ran it at home (where I am right now), and with macro security set to Medium, and that check box not checked, the code definitely stops, with a warning about access to the code project being disallowed (which is a good thing).

    At work, where I wrote the macro (and where I'm not part of IT), the macro runs fine. I was already aware that, incredibly, they keep macro security set to Low, but I didn't know about that checkbox - undoubtedly, they've got it checked.

    Definitely not worth the risk, keeping the settings that way - the IT folks at my firm are crazy (and unfortunately, it's not the first time I've posted a 'solution' here, only to find that my firm's environment is the only place in the world where it runs!)

    Gary

  5. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Code in Document, Not Template (Word XP)

    <P ID="edit" class=small>(Edited by Gary Frieder on 12-Feb-05 17:15. )</P>Mike,

    The following should strip the code from any modules that might be associated with a document (ThisDocument module, any userform modules, and any standard modules) - it won't delete the modules, but will strip the code from them.

    It doesn't require any reference to VBE Extensibility.

    [Edit: See Stuart R.'s post below. This code won't run in an environment that has sensible macro security settings!]

    <pre>Public Sub RemoveCodeFromModules()

    If ActiveDocument.Type = wdTypeTemplate Then Exit Sub

    Dim n As Long
    Dim LineCt As Long

    With ActiveDocument.VBProject
    For n = 1 To .VBComponents.Count
    LineCt = .VBComponents(n).CodeModule.CountOfLines
    .VBComponents(n).CodeModule.DeleteLines 1, LineCt
    Next 'n
    End With
    End Sub
    </pre>

    Gary

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Code in Document, Not Template (Word XP)

    Gary,

    Thanks for sharing.

    I reckon your employers are daft, unless they have a magic anti-virus product that protects them against viruses that haven't been seen in the wild before (which is exactly what these Macro security settings do).

    StuartR

  7. #7
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code in Document, Not Template (Word XP)

    Hmmm... let's just say that I can use the code "as-is," then. (I checked, and our standard image has security set to "low"... or should that be "too low"?)
    I appreciate all the input... and I'll be back if they change security, especially after the latest MS patches are deployed tomorrow!
    Peace...
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  8. #8
    2 Star Lounger
    Join Date
    Jan 2002
    Location
    Putnam Valley, New York, USA
    Posts
    113
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code in Document, Not Template (Word XP)

    Stuart --
    I may be mistaken, but I am under the impression that the macro security in Word is only protecting against viruses that are Word Macros -- other viruses are not prevented even if Macro Security is High.

  9. #9
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Code in Document, Not Template (Word XP)

    I think you are right that Word Macro security generally protects you against Word viruses, although I suspect the particular setting "Trust Access to Visual Basic Project" probably protects you against other VBA viruses too (e.g. from Excel). i have never checked this, just seems likely.

    StuartR

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •