dcsimg
Results 1 to 13 of 13

Thread: Removing macros from Word files

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,195

    Removing macros from Word files

    Hello. I need to open some Word files from my program using automation, they don't need to have any macro but sometimes they could have, because the original author may have them to automate something.

    The issue is that with new Office versions, if the file has any macro it raises error 6304.
    I know that if the user changes the security policies in the machine they can be opened, but what I would like is to remove the macros from the files or to open the files with the macros disabled whithout requiring any security policy change in the user machine.
    Any idea?
    TIA

    Edit: they are *.doc files, in Word 97 format.
    Last edited by Eduardo-; Oct 10th, 2018 at 09:10 AM.
    vbExtra: Print preview for VB6, print FlexGrids and more.
    MSDN online for VB6, Language reference.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,466

    Re: Removing macros from Word files

    Well, since a docm (or xlsm) is basically a zip, you could try to use one of those zip-libraries and remove the vbadata.xml and vbaproject.bin from the word-folder inside the zip.

    Just do a Test: Create a Bogus-Word-File with some Macros in it and save it as docm.
    Try to import it with your program (will probably cause error)
    Rename the file-ending to zip, open it in 7zip (or whatever), go to the folder "word", remove vbadata.xml and vbaproject.bin, save the zip, rename it back to docm, and then try to import it with your program, and look if the error occurs.
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    I say you're crazy not to!
    --------------------------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,195

    Re: Removing macros from Word files

    Quote Originally Posted by Zvoni View Post
    Well, since a docm (or xlsm) is basically a zip, you could try to use one of those zip-libraries and remove the vbadata.xml and vbaproject.bin from the word-folder inside the zip.

    Just do a Test: Create a Bogus-Word-File with some Macros in it and save it as docm.
    Try to import it with your program (will probably cause error)
    Rename the file-ending to zip, open it in 7zip (or whatever), go to the folder "word", remove vbadata.xml and vbaproject.bin, save the zip, rename it back to docm, and then try to import it with your program, and look if the error occurs.
    Sorry, I didn't specify that they are files in Word 97 format (*.doc).
    Thank you anyway.
    vbExtra: Print preview for VB6, print FlexGrids and more.
    MSDN online for VB6, Language reference.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,195

    Re: Removing macros from Word files

    I already tried:

    Code:
    WordServer.AutomationSecurity = msoAutomationSecurityForceDisable '(3)
    before WordServer.Documents.Open but keep getting the error 6304.
    vbExtra: Print preview for VB6, print FlexGrids and more.
    MSDN online for VB6, Language reference.

  5. #5
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,296

    Re: Removing macros from Word files

    Hi,

    can't remember correctly, but should be somthing like..


    Code:
    Dim wdDoc As Word.Document
    Set wdDoc = wdAppl.Documents.Add("I:\Test1.doc")
    wdAppl.WordBasic.DisableAutoMacros
    within the doc is open ...
    Code:
    Sub DeleteAllCode()
     'Doc is opend
     Dim codeModul As Object
     Dim wdDoc As Word.Document
     
    On Error Resume Next
    Set wdDoc = wdAppl.Documents("test1.doc")
    For Each codeModul In wdDoc.VBProject.VBComponents
     codeModul.CodeModule.DeleteLines Startline:=1, Count:=codeModul.CodeModule.CountOfLines
     wdDoc.VBProject.VBComponents.Remove codeModul
    Next
     
    End Sub

    regards
    Chris
    Last edited by ChrisE; Oct 10th, 2018 at 10:25 AM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,195

    Re: Removing macros from Word files

    According to documentation Documents.Add returns a new (empty) document.

    What I find in internet, the code seems to be:

    [/code]Set WordServer = CreateObject("Word.Application")
    WordServer.WordBasic.DisableAutoMacros 1
    Set iDoc = nWordServer.Documents.Open(...[/code]

    But it fails at the Open method with error 6304.
    It seems that that worked in older Office versions, but I'm testing with Word version 16 and it raises that error.
    The description of the error is something like "You are attempting to open a file that is blocked with File Block settings in the Trust Center" (it was translated from Spanish).

    It seems to be something new because I dont find much information on Internet.

    OK, for now I'll show an error message and intruct the user to open the file (in another computer if necessary) and to remove the macros. Then try again.
    vbExtra: Print preview for VB6, print FlexGrids and more.
    MSDN online for VB6, Language reference.

  7. #7
    Frenzied Member
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,188

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,195

    Re: Removing macros from Word files

    Yes, thank you. So far it seems that it must be done manually.

    Edit:
    Info for anyone that can read this thread in the future.
    There can be not only error 6304, but also I found error 6295, its description is: "Office has detected a problem with this file. To help protect your computer this file cannot be opened".
    I still don't know what determines to get one error or the other.
    Last edited by Eduardo-; Oct 11th, 2018 at 06:41 AM.
    vbExtra: Print preview for VB6, print FlexGrids and more.
    MSDN online for VB6, Language reference.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,195

    Re: Removing macros from Word files

    Update:

    I'm with the same files in an older computer (the files are indeed loaded inside a database, but I extract them to a tremporary file to be able to open them in Word). Surprisingly none of the files has macros.
    I assumed that the problem was that the files had macros because when I searched the internet with the error message I got all sort of pages explaining that files with macros would be the cause of a message similar to that.

    But not. I have, in about 1000 files, 3 with problems. 2 raiuse error 6304, and 1 error 6295.
    The two with error 6304 are in Word6/95 format. That must be the cause, because all the other files are in Word 97/2003 format and do not cause the error.

    About the one that raise error 6295 (all the errors I'm talking are raised in Word version 16, in older Word versions there is no problem) I still don't know. It doesn't have macros, it is in Word 97 format. I don't know what to look for.
    vbExtra: Print preview for VB6, print FlexGrids and more.
    MSDN online for VB6, Language reference.

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,783

    Re: Removing macros from Word files

    i would try opening them in some version > 2007 then saving as .docx and update the database
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,195

    Re: Removing macros from Word files

    Quote Originally Posted by westconn1 View Post
    i would try opening them in some version > 2007 then saving as .docx and update the database
    I don't see the point. I work with Word 97/2003 format so the program can be used in machines that have Word version 2003 (or newer of course).
    vbExtra: Print preview for VB6, print FlexGrids and more.
    MSDN online for VB6, Language reference.

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,783

    Re: Removing macros from Word files

    I work with Word 97/2003 format so the program can be used in machines that have Word version 2003 (or newer of course).
    sorry i missed that point
    i would still try similar, copy the document content to a new file and save again or saveAs .docx then resaveAs .doc, presumably the saving in different formats should completely rewrite the files and fix the errors
    as you have now found it is only a small number of known files, not hard to test
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,195

    Re: Removing macros from Word files

    Quote Originally Posted by westconn1 View Post
    sorry i missed that point
    i would still try similar, copy the document content to a new file and save again or saveAs .docx then resaveAs .doc, presumably the saving in different formats should completely rewrite the files and fix the errors
    as you have now found it is only a small number of known files, not hard to test
    Yes, but I also see that I won't be able to automate that in the program. That action will have to be done by someone manually.
    I didn't find any "hack" for doing this, but even if there was one I prefer not to bypass this kind of security measures.

    I added to the program an utility to check all Word files that are stored in the database, trying to open them and making a list of the files that have issues.
    Last edited by Eduardo-; Oct 11th, 2018 at 04:53 PM.
    vbExtra: Print preview for VB6, print FlexGrids and more.
    MSDN online for VB6, Language reference.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width