Results 1 to 11 of 11
  1. #1
    3 Star Lounger
    Join Date
    Jan 2007
    Location
    Massachusetts, USA
    Posts
    272
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Macro to remove graphics from RTF files (MS Word 2003, XP SP2)

    Hello,

    I am looking for a macro that will:

    1. Point to a folder containing RTF files.
    2. Open each and every RTF file.
    3. Remove all {SHAPE *MERGEFORMAT} fieldcodes.
    4. Remove all fieldcodes containing {INCLUDEPICTURE...}
    5. Remove all graphics that are embedded or pasted into the
    word document. Another words, any graphic found in the word
    documents that DO NOT use a field code.


    I have been tasked to remove hundreds of old photos with text boxes and callout arrows on each photo. This macro will save
    me a lot of time, if it is possible to create.

    Also, if it is not possible to remove embedded graphics via a
    macro (see step 5 above), then I will have to remove those graphics manually.


    As mentioned before, I am using MS Word 2003/ Windows XP SP2
    and all the files have been saved to RTF format.

    We have created a new graphic library and will bring in all the new graphics, once the old graphics are removed. I will leave all the original figure titles in the RTF files. The macro only
    needs to remove any graphics that are linked to file or embedded.


    Please see the attached screen shot, which represents the two of the ways the graphics are referenced in the RTF file. Obviously, there is no fieldcode for embedded graphics.


    Thanks in advance for your help.


    Regards,


    Jim

    P.S. Most (if not all) of the graphics in the RTF files are contained inside text boxes. Is there also a simple Macro I can create to globally remove text boxes?
    Attached Images Attached Images

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to remove graphics from RTF files (MS Word 2003, XP SP2)

    You already know how to loop through a folder of files, open each of them in turn, manipulate them, then save and close them. Say that you have a variable of type Document and you set it within the loop using a line like this:

    Set doc = Documents.Open(strPath & strFile)

    You can then "clean" it by using

    Call CleanDoc(doc)

    where CleanDoc is the following procedure:

    Sub CleanDoc(doc As Document)
    Dim i As Integer
    For i = doc.Fields.Count To 1 Step -1
    Select Case doc.Fields(i).Type
    Case wdFieldIncludePicture, wdFieldShape
    doc.Fields(i).Delete
    End Select
    Next i
    For i = doc.Shapes.Count To 1 Step -1
    Select Case doc.Shapes(i).Type
    Case msoPicture, msoLinkedPicture, msoAutoShape
    doc.Shapes(i).Delete
    End Select
    Next i
    For i = doc.InlineShapes.Count To 1 Step -1
    Select Case doc.InlineShapes(i).Type
    Case wdInlineShapePicture, wdInlineShapeLinkedPicture
    doc.InlineShapes(i).Delete
    End Select
    Next i
    End Sub

  3. #3
    3 Star Lounger
    Join Date
    Jan 2007
    Location
    Massachusetts, USA
    Posts
    272
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Macro to remove graphics from RTF files (MS Word 2003, XP SP2)

    Hello Hans,

    I am sorry, but you lost me.
    I tried bringing together Macro code with this new code, but only got a Runtime error '424': object required.

    I guess I do not have a firm handle on how the macro code should be configured.

    I have attached a screen shot of the code that I put together and right now I am lost.
    Perhaps, I am missing the obvious.

    The macro contains two subroutines. The first one points to the temporary folder that I have created on my "C" drive (e.g. C:Temp01).
    The macro also references the .RTF format (NOT .DOC) that I am using.

    Thanks for your patience on this macro.

    Oh one last thing, is there a way to get the macro to ignore "comments" in various word documents? This will save me from
    having to manually acknowledge the popup prompt, before it will continue?

    Thanks as always for your assistance.

    Regards,

    -J
    Attached Images Attached Images

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to remove graphics from RTF files (MS Word 2003, XP SP2)

    Please note that the first line of the CleanDoc procedure is

    Sub CleanDoc(doc As Document)

    instead of the line you have

    Sub CleanDoc()

    In the ReplaceInFolder macro, you only need to replace the line

    ReplaceInDoc doc

    with

    CleanDoc doc

    What exactly do you mean by "ignore comments"? You could try inserting a line

    Application.DisplayAlerts = False

    at the beginning of ReplaceInFolder, and

    Application.DisplayAlerts = True

    at the end.

  5. #5
    3 Star Lounger
    Join Date
    Jan 2007
    Location
    Massachusetts, USA
    Posts
    272
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Macro to remove graphics from RTF files (MS Word 2003, XP SP2)

    Hello Hans,

    I made the code substitution per your request and I am now getting a error message in the macro.
    Please remember that I am using the .RTF format only in this macro.

    Perhaps I have missed something else in the code.

    Thanks once again for the feedback.
    Please see the attached screenshot with error message.

    -J
    Attached Images Attached Images

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to remove graphics from RTF files (MS Word 2003, XP SP2)

    1) Yes, there is only one place where you specify that you want to use .RTF files.
    2) Make sure that you use straight quotes around string values - yours look slightly different than the ones I see in the Visual Basic Editor. Try typing the quotes anew in the Visual Basic Editor.
    3) There is no line With ... so you don't need a line End With either. Each End With must belong to a corresponding With ...

  7. #7
    3 Star Lounger
    Join Date
    Jan 2007
    Location
    Massachusetts, USA
    Posts
    272
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Macro to remove graphics from RTF files (MS Word 2003, XP SP2)

    Hello again Hans,

    I ended up copy and pasting in Macro code from an older (txt) file I had and that seem to fix the quote problem.
    However, there is another problem in the code.
    The macro ran fro a while and then stopped on an error. The error window had a debug option, so I clicked on it
    and the error I received is in the attached screenshot (automatically highlighted).
    Any ideas?

    -J

    Everything else seems to be working okay in the macro now. Thanks once again.
    Attached Images Attached Images

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to remove graphics from RTF files (MS Word 2003, XP SP2)

    What was the error message?

  9. #9
    3 Star Lounger
    Join Date
    Jan 2007
    Location
    Massachusetts, USA
    Posts
    272
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Macro to remove graphics from RTF files (MS Word 2003, XP SP2)

    Hello Hans,

    The macro did go through and remove a lot of the graphics that were linked into the RTF files, but it did not remove
    any of the graphics that were embedded (no field code associated with graphic). The error message I received is attached here and
    the debug results of that error message are in the previous screen shot.

    Thanks once again for your assistance.

    -J

    By the way, can a macro be created that will remove word art placed on top of photos in MS Word, such as: rectangular callouts and arrows, or will this macro
    take care of those items, when it works?
    Attached Images Attached Images

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to remove graphics from RTF files (MS Word 2003, XP SP2)

    Was the document in which the error occurred protected? Otherwise I wouldn't know why you got a "permission denied" error.

    The code also removes autoshapes such as arrows etc. - read the code, please.

  11. #11
    3 Star Lounger
    Join Date
    Jan 2007
    Location
    Massachusetts, USA
    Posts
    272
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Macro to remove graphics from RTF files (MS Word 2003, XP SP2)

    Hello Hans,

    Some of my legacy word files, when converted to RTF format, got as large as 500 meg. I think this was why the macro
    had trouble on those files.

    All in all the macro worked on some files and not on others. Why? I am not sure.
    But, it removed the bulk of the graphics from the files - and for that I am grateful.

    Thanks again for your patience and suggestions.

    Regards,

    Jim

Posting Permissions

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