When working with Excel and Word, you always have to specify WHICH Range you're referring to.
![]()
Dim myStoryRange As Word.Range
When working with Excel and Word, you always have to specify WHICH Range you're referring to.
![]()
Dim myStoryRange As Word.Range
David
(*) Reputation points appreciated.
thanks so much, when i run it now it gives me a new error atIt says object variable or With block variable not set.![]()
With wdApp.Selection.find
This is my full codeTruly appreciate your help with this![]()
Sub FasterFindAndReplaceAllStoriesHopefully() Dim myStoryRange As Word.Range Dim xlApp As Excel.Application Dim xlWs As Excel.Worksheet Dim wdApp As Word.Application Dim lastrow As Long lastrow = Cells.SpecialCells(xlCellTypeLastCell).Row 'this version of last row looks in any column for the furthest down cell with data in it Dim wdDoc As Word.Document Dim pfindtext As String Dim preplacetext As String Dim lngjunk As Long Dim oShp As Word.Shape Dim newfn$ newfn = Application.GetOpenFilename(Title:="Please select a file") If newfn = "False" Then Exit Sub 'determine if there are more procedures than word pages to fill in Dim ival As Integer ' count # of procedures listed (just to see if any were added) ival = Excel.Application.WorksheetFunction.CountIf(Range("A1:A" & lastrow), "%%Location*%%") If ival > 3 Then Warning = MsgBox("Warning, more than 3 procedures!") Warning = Warning & vbNewLine Warning = Warning & MsgBox("Have you added extra pages in the word doc?", vbYesNo) If Warning = vbNo Then Exit Sub End If 'loop through cells and replace in word For i = 2 To lastrow pfindtext = Excel.Application.ActiveWorkbook.ActiveSheet.Cells(i, 1).Value preplacetext = Excel.Application.ActiveWorkbook.ActiveSheet.Cells(i, 2).Value 'wdCommentsStory 4, wdEndnotesStory 3, wdEvenPagesFooterStory 8, wdEvenPagesHeaderStory 6, 'wdFirstPageFooterStory 11, wdFirstPageHeaderStory 10, wdFootnotesStory 2, wdMainTextStory 1, 'wdPrimaryFooterStory 9, wdPrimaryHeaderStory 7, and wdTextFrameStory 5 'First search the main document using the Selection With wdApp.Selection.Find .text = pfindtext .Replacement.text = preplacetext .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute Replace:=wdReplaceAll End With 'Now search all other stories using Ranges For Each myStoryRange In Word.Application.ActiveDocument.StoryRanges Select Case myStoryRange.StoryType Case 5, 6, 7, 8, 9 With myStoryRange.Find .text = pfindtext .Replacement.text = preplacetext .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With Do While Not (myStoryRange.NextStoryRange Is Nothing) Set myStoryRange = myStoryRange.NextStoryRange With myStoryRange.Find .text = pfindtext .Replacement.text = preplacetext .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With Loop End Select Next myStoryRange Next i End Sub
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks