Shawn
asked on
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi Shawn,
Try changing this line:
Set wrd = CreateObject("Word.Applica tion")
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.
Try changing this line:
Set wrd = CreateObject("Word.Applica
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.
Shawn,
did no know post was closed, but you may want to try my suggestion on your other post if all else fails.
did no know post was closed, but you may want to try my suggestion on your other post if all else fails.
ASKER
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.
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.
ASKER
I have a very similar problem here:
https://www.experts-exchange.com/questions/24124781/powerpoint-not-saving-as-in-function-search-and-replace-in-VB.html
if you could have a quick peek that would be great.