Results 1 to 2 of 2

Loop through Excel file to replace wording in Word (VBA in excel)

Threaded View

  1. #1
    Registered User
    Join Date
    06-12-2013
    Location
    USA
    MS-Off Ver
    excel 2007
    Posts
    9

    Loop through Excel file to replace wording in Word (VBA in excel)

    I am trying to replace wording in a word file using an excel file. Based on if a user selects yes or no to a question, there will either be a few pages of wording or no wording at all. What I have done is set up placeholders in the word file (%placeholder1%, %placeholder2%, etc) and if the user answers no, the placeholders are replaced by "". If the user selects yes, %placeholder1% is replaced by cell A1, %placeholder% by cell A2, etc.

    I got the loop macro to work when I was bringing up the file and saving it all in that one macro, but I need to do this for several questions in the same file. So I need to have the test macro call a few loop macros. Not sure how to get it working with opening the word file in the main macro instead of the loop one. Here is my code:

    Sub test()
    '
    ' test Macro
    '
    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document
    
    Set wdApp = New Word.Application
    Set wdDoc = wdApp.Documents.Open("omitted")
    
    Worksheets("Fee").Range("A1").Select
    With wdDoc
    Call loop(wdDoc)
    
    wdApp.ActiveDocument.SaveAs Filename:="new.doc"
    wdApp.ActiveDocument.Close
    End With
    
    Set wdDoc = Nothing
    Set wdApp = Nothing
    End Sub
    Sub loop(wdDoc As Word.Document)
    '
    ' loop Macro
    '
        
    Dim sSplit As String
    Dim sLeft As String
    Dim sSearch As String
    Dim sReplace As String
    Dim sLarge As Boolean
    
    sSplit = "<Å*Å>"
    
    Dim k As Integer
    k = 1
    
    Worksheets("Fee").Range("A1").Select
    
    With wdDoc
    ''Loops until reaches an empty cell''
    Do Until IsEmpty(ActiveCell)
    
    sSearch = "%placeholder" & k & "%"
    sReplace = ActiveCell
    sLarge = True
    
    '''Loops until replacement string is empty
    Do Until sLarge = False
    If Len(sReplace) > 250 Then
    '''adds unique chars to end of what replaces the placeholder
    sLeft = Left(sReplace, 250) & sSplit
    '''takes the 250 chars we already replaced out of the replacement string
    sReplace = Right(sReplace, Len(sReplace) - 250)
    
    '''replace sSearch with sLeft the first time, replace the unique chars subsequently
    With .Content.Find
    .Text = sSearch
    .Replacement.Text = sLeft
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
    End With
    
    Else
    '''Replace the unique chars with the rest of the replacement string
    With .Content.Find
    .Text = sSearch
    .Replacement.Text = sReplace
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
    End With
    
    ''' set sLarge to false to stop looping
    sLarge = False
    End If
    
    '''After first replace, now the unique chars at end will be found and replaced
    sSearch = sSplit
    
    Loop
    
    '''loop through the k's
    k = k + 1
    ActiveCell.Offset(1, 0).Select
    
    Loop
    
    End With
    
    End Sub
    I am running into a problem in the test macro here: Worksheets("EngineeringFee").Range("A1").Select

    EDIT: The error is "select method of Range class failed"

    In the test macro, I am going to need to be able to call loop, loop2(another macro), loop3, etc. Therefore I think I need to set the active cell each time to where it needs to be for the loop macro as that is how I iterate through cells. I'm not sure if I need to change how I iterate through. I plan on making a search macro to cut down on code length, and I'm sure there are other ways I could make the code more efficient but I just need the damn thing to work!

    Any help would be greatly appreciated.
    Last edited by frongi; 06-13-2013 at 11:22 AM.

Thread Information

Users Browsing this Thread

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

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