Consulting

Results 1 to 4 of 4

Thread: VBA in Excel to Word: Searching and Replacing

  1. #1
    VBAX Newbie
    Joined
    Nov 2011
    Posts
    1
    Location

    VBA in Excel to Word: Searching and Replacing

    Hi,

    I have a list of words maintained in Excel that will be used for searching and replacing in Word. I have code that originates in Excel, loads the list of words from the Excel file into an array, and then opens a Word file to search and replace for each word in that array. The code seems to work for the most part, however, the "replaceall" isn't working once in Word. Stepping through the code, everything seems to be working as intended until the last line.

    Here is the code I am working with. Any help would be greatly appreciated as I am stuck!

    [VBA]Sub SpellCheck()

    Dim replace_text As String
    Dim verbTemplateWord As Variant
    Dim this_index As Variant, this_word As Variant, last_word As Variant
    Dim DataList As Range, word_list As Variant

    Set DataList = Sheets("Sheet2").Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    word_list = DataList.Value ' this is where it loads the values in column A

    replace_text = "XXXXXX" 'replace items in word_list with this

    'now need to switch to word
    ' this opens Word (object named wrdApp)
    On Error Resume Next
    Set wrdApp = GetObject("Word.Application")
    If Err.Number <> 0 Then 'Word isn't already running
    Set wrdApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0


    ' COMMENT OUT (for debugging only)


    wrdApp.DisplayAlerts = True
    wrdApp.Visible = True ' can set this to true (and ScreenUpdating) for debugging
    wrdApp.ScreenUpdating = True

    ' Open Word template
    verbTemplateWord = "f:\home\kristin\reference\vba_test\SpellCheck Test.docx"
    Set wrdDoc = wrdApp.documents.Open(verbTemplateWord)

    'this block searches, finds but doesn't replace. for some reason, it highlights instead?
    last_word = UBound(word_list)
    For this_index = 1 To last_word ' the main loop through the word list
    this_word = word_list(this_index, 1)

    wrdApp.Selection.Find.ClearFormatting
    wrdApp.Selection.Find.Replacement.ClearFormatting

    With wrdApp.Selection.Find
    .Text = this_word
    .Replacement.Text = replace_text
    .Forward = True
    .Wrap = wdFindcontinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    wrdApp.Selection.Find.Execute Replace:=wdReplaceAll

    Next this_index


    End Sub[/VBA]

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Welcome to VBAX
    I see what you mean and can't think of a reason.
    As a workaround, I would try creating the macro in Word, then pass the variables to that macro to run.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    wellcome to vbax


    pls refer to crossposting rules...

    http://www.mrexcel.com/forum/showthread.php?t=592190
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    hi kmh,

    Try:
    [vba]Sub SpellCheck()
    Dim replace_text As String
    Dim verbTemplateWord As Variant
    Dim this_index As Variant, this_word As Variant, last_word As Variant
    Dim DataList As Range, word_list As Variant
    Dim wrdApp As Word.Application, wrdDoc As Word.Document

    Set DataList = Sheets("Sheet2").Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    word_list = DataList.Value ' this is where it loads the values in column A

    replace_text = "XXXXXX" 'replace items in word_list with this

    'now need to switch to word
    ' this opens Word (object named wrdApp)
    On Error Resume Next
    Set wrdApp = GetObject("Word.Application")
    If Err.Number <> 0 Then 'Word isn't already running
    Set wrdApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    With wrdApp
    .DisplayAlerts = True
    .Visible = True ' can set this to true (and ScreenUpdating) for debugging
    .ScreenUpdating = False

    ' Open Word template
    verbTemplateWord = "f:\home\kristin\reference\vba_test\SpellCheck Test.docx"
    Set wrdDoc = .Documents.Open(verbTemplateWord)
    'this block searches, finds but doesn't replace. for some reason, it highlights instead?
    last_word = UBound(word_list)
    For this_index = 1 To last_word ' the main loop through the word list
    this_word = word_list(this_index, 1)
    With wrdDoc.Content.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = this_word
    .Replacement.Text = replace_text
    .Forward = True
    .Wrap = wdFindcontinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
    End With
    Next this_index
    .ScreenUpdating = True
    End With
    End Sub[/vba]
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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