Consulting

Results 1 to 5 of 5

Thread: Extracting Specific text within a word document

  1. #1
    VBAX Newbie
    Joined
    May 2005
    Posts
    3
    Location

    Extracting Specific text within a word document

    Hello all and thanks in advance for the help.

    I am trying to write a macro which will search through a word document and locate certain pieces of text.

    For instance:

    I want it to find any HTML IMG tags and just return the ALT info.

    I want it to find:
    <IMG SRC="thisimg.gif" ALT="This is an Image">

    and Replace it with:
    This is an Image

    Am I making sense?

    I look forward to any help you are able to give!

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I'm sure there is a more efficient way to do this, but this might give you a start. Recorded with Word macro recorder.
    [VBA] Option Explicit
    Sub Find_Replace()

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = "<IMG SRC=""thisimg.gif"" ALT=""This is an Image"">"
    .Replacement.Text = "This is an Image"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.Find.Execute Replace:=wdReplaceAll
    End Sub

    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Newbie
    Joined
    May 2005
    Posts
    3
    Location
    Hey Lucas. Thanks for the reply.

    The problem is that I will never know what text is in either the SRC or the ALT.

    I want the macro to search the document for an <IMG and if found, extract the text in the ALT tag to replace the tag with.

    So it needs to be dynamic. The plus side, it will be well-formed with " " where they need to be, so maybe something along the lines of:

    <IMG SRC="ThisImg.gif" ALT="Some TExt here">
    Search for <IMG.

    If <IMG is found Select from <IMG until the third " is found.
    <IMG SRC="ThisImg.gif" ALT="Some TExt here">

    Delete.
    Leaving:
    Some TExt here">

    From that point, find the next " and select from there until > and delete

    Some TExt here">

    Leaving:Some TExt here

    Is that even possible within VBA?

    Thanks again and I look forward to more help.

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Yes, this is very possible. I will re post with something in about an hour.

  5. #5
    You don't really need a macro. You can process the whole document with a single wildcard Replace All. In the Replace dialog, clikc the More button and check the Use Wildcards option. Then in the Find What box enter

    [VBA] \<IMG SRC*ALT="(*)"\>[/VBA]

    and in the Replace With box enter

    [VBA] \1[/VBA]

    and click Replace All.

    If you want to make a macro of this so it's easy to repeat, use lucas's code but replace the .Text and .Replacement.Text expressions with these two, and set .MatchWildcards = True.

    Jay

Posting Permissions

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