Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 863
  • Last Modified:

search and replace text boxes (and other story ranges) words document

I just found out that when searching and replacing via vb word doesn't automatically search text boxes and other "story ranges"

I found this code and have tried to apply but it seems to be ignoring it. What am I missing? (see 2 public sub at bottom of code
Case ".doc", ".RTF", ".dot", ".docx", ".txt", ".csv"
 
' Beginning of Word section
 
'MsgBox "this is a Word document!" & strFileName & " suffix is" & strSuffix
 
 
    Set wrd = CreateObject("Word.Application")
      wrd.Documents.Open strFilePath & strFileName
                    'need to specify main form
                    If Me.ActivateTrackChanges = True Then
                    wrd.ActiveDocument.TrackRevisions = True
                    Else
                    wrd.ActiveDocument.TrackRevisions = False
                    End If
      wrd.Options.DefaultHighlightColorIndex = strHighlightColor
      wrd.Selection.Find.Replacement.ClearFormatting
      If Forms![frmReplacementProject]!HighLight = True Then
      wrd.Selection.Find.Replacement.HighLight = True
      End If
          
    'start replacing list of words in glossary subform
    With [sbfrmProjectGlossary].Form.RecordsetClone
    .MoveFirst
    Do While .EOF = False
        
    strSearchText = .TargetLanguageTerm
    If Forms![frmReplacementProject]!ReplaceOptionID = 1 Then
    strReplaceText = .TargetLanguageCorrectedTerm
    Else
    strReplaceText = strSearchText & " " & strBeforeTag & .TargetLanguageCorrectedTerm & strAfterTag
    End If
    
    wrd.Selection.Find.Execute FindText:=strSearchText, _
       ReplaceWith:=strReplaceText, _
        Format:=True, Replace:=2, Wrap:=wdFindContinue, Forward:=True
        
    'Now search all other stories using Ranges
    Call FindReplaceAnywhere
    
        
       .MoveNext
    Loop
    End With
      
      wrd.ActiveDocument.SaveAs IncrementIfExists(strFilePath & strPrefix & strSaveExtension & strSuffix)
      DoEvents
      wrd.ActiveDocument.Close (True)
      DoEvents
      
      wrd.Quit
' end of Word section
 
 
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 = strSearchText
  pReplaceTxt = strReplaceText
    
  '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
            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)
 
  With rngStory.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = strSearch
    .Replacement.Text = strReplace
    .Wrap = wdFindContinue
    .Execute Replace:=wdReplaceAll
  End With
 
End Sub

Open in new window

0
Shawn
Asked:
Shawn
  • 2
  • 2
1 Solution
 
irudykCommented:
Did you declare the strSearchText and strReplaceText as Public variables?  If not, then pFindTxt and pReplaceTxt will be set as "" since the sub FindReplaceAnywhere won't know what these values are.
Instead of creating Public variable, you could pass them to the FindReplaceAnywhere sub
For example,
Call FindReplaceAnywhere(strSearchText, strReplaceText)
and then change the Sub to start with
Public Sub FindReplaceAnywhere(pFindTxt As String, pReplaceTxt As String)
Try the revised code below:
 

Case ".doc", ".RTF", ".dot", ".docx", ".txt", ".csv"
 
' Beginning of Word section
 
'MsgBox "this is a Word document!" & strFileName & " suffix is" & strSuffix
 
 
    Set wrd = CreateObject("Word.Application")
      wrd.Documents.Open strFilePath & strFileName
                    'need to specify main form
                    If Me.ActivateTrackChanges = True Then
                    wrd.ActiveDocument.TrackRevisions = True
                    Else
                    wrd.ActiveDocument.TrackRevisions = False
                    End If
      wrd.Options.DefaultHighlightColorIndex = strHighlightColor
      wrd.Selection.Find.Replacement.ClearFormatting
      If Forms![frmReplacementProject]!HighLight = True Then
      wrd.Selection.Find.Replacement.HighLight = True
      End If
          
    'start replacing list of words in glossary subform
    With [sbfrmProjectGlossary].Form.RecordsetClone
    .MoveFirst
    Do While .EOF = False
        
    strSearchText = .TargetLanguageTerm
    If Forms![frmReplacementProject]!ReplaceOptionID = 1 Then
    strReplaceText = .TargetLanguageCorrectedTerm
    Else
    strReplaceText = strSearchText & " " & strBeforeTag & .TargetLanguageCorrectedTerm & strAfterTag
    End If
    
    wrd.Selection.Find.Execute FindText:=strSearchText, _
       ReplaceWith:=strReplaceText, _
        Format:=True, Replace:=2, Wrap:=wdFindContinue, Forward:=True
        
    'Now search all other stories using Ranges
    Call FindReplaceAnywhere(strSearchText, strReplaceText)
    
        
       .MoveNext
    Loop
    End With
      
      wrd.ActiveDocument.SaveAs IncrementIfExists(strFilePath & strPrefix & strSaveExtension & strSuffix)
      DoEvents
      wrd.ActiveDocument.Close (True)
      DoEvents
      
      wrd.Quit
' end of Word section
 
 
Public Sub FindReplaceAnywhere(pFindTxt As String, pReplaceTxt As String)
 
  Dim rngStory As Word.Range
  Dim lngJunk As Long
  Dim oShp As Shape
    
  '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
            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)
 
  With rngStory.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = strSearch
    .Replacement.Text = strReplace
    .Wrap = wdFindContinue
    .Execute Replace:=wdReplaceAll
  End With
 
End Sub

Open in new window

0
 
ShawnAuthor Commented:
that would do it!  thanks.

I have a very similar problem here:
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_24124781.html
if you could have a quick peek that would be great.
0
 
puppydogbuddyCommented:
Hi Shawn,
Try changing this line:
          Set wrd = CreateObject("Word.Application")
To This:
          Set wrd = New Word.Application
Due to the confusion over the approriate object qualifiers when using late binding, I find that when using early binding, my code suddenly starts working.                    
0
 
puppydogbuddyCommented:
Shawn,
did no know post was closed, but you may want to try my suggestion on your other post if all else fails.
0
 
ShawnAuthor Commented:
Hi puppydogbuddy,

It does work now but your input is appreciated. I've seen quite a bit early and late binding recently and will look into it a bit more. Still not sure of the differences pros/cons. I really need cross version compatability and thought there might be issues with early binding. Maybe I should put up a question to get a better grasp of it all.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now