View Single Post
 
Old 07-13-2015, 12:11 PM
BrotherDude BrotherDude is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Jul 2015
Posts: 3
BrotherDude is on a distinguished road
Default

Hello,

I am trying to loop through all the headers in a document searching for a text string. If that string is found I would like to select it and the connected text to the left and pass the text value to a string variable so I can split and name the documents by these string values.

I know this is a few parts but if you can help with any of the modules it would be much appreciated.

1. Loop through all headers
2 Search for text string, select entire word, save value to string
3. Export Page and name by string variable

Any help is much appreciated. I found several sources for search and replace but the structure, ranges and object naming seems more complicated than necessary. I can provide some code that has yielded some results but seems off track.

-Jeff

Code:
Public Sub FindReplaceAnywhere()
    Dim rngStory As Word.Range
    Dim pFindTxt As String
    Dim pReplaceTxt As String
    Dim lngJunk As Long
    Dim oShp As Shape
 
     pFindTxt = "-IN"
     'pReplaceTxt = "-IN**"  'From Sorce Code
 
 
    'Fix the skipped blank Header/Footer problem
    lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
 
    'Iterate through all story types in the current document
    For Each rngStory In ActiveDocument.StoryRanges
 
        'Iterate through all linked stories
        Do
            SearchAndReplaceInStory rngStory, pFindTxt, pReplaceTxt
            On Error Resume Next
            Select Case rngStory.StoryType
                Case 6, 7, 8, 9, 10, 11
                    If rngStory.ShapeRange.Count > 0 Then
                        For Each oShp In rngStory.ShapeRange
                            If oShp.TextFrame.HasText Then
                                SearchAndReplaceInStory oShp.TextFrame.TextRange, pFindTxt, pReplaceTxt
                                'FindIt oShp.TextFrame.TextRange, pFindTxt, pReplaceTxt
                            End If
                        Next
                    End If
                Case Else
                    'Do Nothing
                End Select
                On Error GoTo 0
 
                'Get next linked story (if any)
                Set rngStory = rngStory.NextStoryRange
            Loop Until rngStory Is Nothing
        Next
End Sub
 
 
Public Sub SearchAndReplaceInStory(ByVal rngStory As Word.Range, ByVal strSearch As String, ByVal strReplace As String)
Dim Invoice As String
Dim rngInvoice As Range
Dim strStart As Long
Dim strEnd As Variant
 
'Best I could do, Prints the range of the area I want. Seems like I should be using .moveleft ?
With rngStory
    If .Find.Execute(strSearch) Then Debug.Print .Start - 7 & " " & .End
End With
 
End Sub

I found this which seems like it should be used to select the range but I'm struggling to name the header range and actually select the text I want

Code:
With Selection 
 Set MyRange = .GoTo(wdGoToField, wdGoToPrevious) 
 .MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend 
 If Selection.Fields.Count = 1 Then Selection.Fields(1).Update 
End With
IDK pretty lost when it comes to Word and VBA. Any help is muchh appreciated!!
Reply With Quote