none
Find an variable length text and assign it to a variable RRS feed

  • Question

  • Hello,

    I have a word document where I have a number of file names that I need to find and then process.  Each text occurance is on its own line.  These text occurances vary in length.  

    Example 1:  Tiny Pic.jpg

    Example 2:  Very Large Pic.jpg

    Example 3:  Medium_Size_Pic.jpg

    Example 4:  Image_Average_Size_Pic.jpg

    The text always ends with the four characters ".jpg"

    I could easily format the text so that it begins with the text "Image_" like in Example 4 and ensure that there are no embedded blanks, if that will make this search easier.

    I've tried the Selection.MoveRight method, but I can't get it going.  I don't know how to process varialbe length strings.

    For intCurrentItem = 1 To intTotalItems

        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = "image_"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute
        Selection.MoveRight Unit:=wdWord, Extend:=wdExtend
        Let varImageFile = Selection.Text

        ....(Process the file, which works fine)

        End With

        Selection Collapse

    Next intCurrentItem

    I have more experience with Excel Macro's. 

    Thanks for looking at this.

    Tuesday, April 17, 2012 1:42 AM

Answers

  • Assuming each filename is on its own line, the macro below should work for you.

    Sub PickUpFileNames()
        Dim done As Boolean
        Dim fName As String
        Dim doc As Word.Document
        Set doc = Word.ActiveDocument
        Word.Selection.HomeKey unit:=Word.WdUnits.wdStory
        Do Until done
            With Word.Selection.Find
                .ClearFormatting
                .Text = ".jpg"
                .Wrap = wdFindStop
                .Forward = True
                .Execute
            End With
            If Word.Selection.Find.Found Then
                Word.Selection.StartOf Word.WdUnits.wdLine, Extend:=Word.wdExtend
                fName = Word.Selection.Text
                Debug.Print fName
                Word.Selection.Collapse Word.WdCollapseDirection.wdCollapseEnd
            Else
                done = True
            End If
        Loop
    End Sub


    Kind Regards, Rich ... http://greatcirclelearning.com

    Tuesday, April 17, 2012 11:25 AM

All replies

  • Assuming each filename is on its own line, the macro below should work for you.

    Sub PickUpFileNames()
        Dim done As Boolean
        Dim fName As String
        Dim doc As Word.Document
        Set doc = Word.ActiveDocument
        Word.Selection.HomeKey unit:=Word.WdUnits.wdStory
        Do Until done
            With Word.Selection.Find
                .ClearFormatting
                .Text = ".jpg"
                .Wrap = wdFindStop
                .Forward = True
                .Execute
            End With
            If Word.Selection.Find.Found Then
                Word.Selection.StartOf Word.WdUnits.wdLine, Extend:=Word.wdExtend
                fName = Word.Selection.Text
                Debug.Print fName
                Word.Selection.Collapse Word.WdCollapseDirection.wdCollapseEnd
            Else
                done = True
            End If
        Loop
    End Sub


    Kind Regards, Rich ... http://greatcirclelearning.com

    Tuesday, April 17, 2012 11:25 AM
  • Hi Rich,

    Very elegant solution. 

    I implemented it and it works well.

    Thanks for all your help.

    Phil

    Tuesday, April 17, 2012 3:11 PM