+ Reply to Thread
Results 1 to 27 of 27

Find and replace in word document

Hybrid View

  1. #1
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: Find and replace in word document

    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.

  2. #2
    Registered User
    Join Date
    05-14-2013
    Location
    California
    MS-Off Ver
    Excel 2013
    Posts
    78

    Re: Find and replace in word document

    thanks so much, when i run it now it gives me a new error at
    With wdApp.Selection.find
    It says object variable or With block variable not set.

    This is my full code
    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
    Truly appreciate your help with this

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1