+ Reply to Thread
Results 1 to 5 of 5

Mailmerge Search & Replace

Hybrid View

PooH Mailmerge Search & Replace 07-19-2010, 01:16 PM
PooH re: Mailmerge Search & Replace 07-20-2010, 04:07 AM
snb re: Mailmerge Search & Replace 07-20-2010, 04:28 AM
PooH re: Mailmerge Search & Replace 07-21-2010, 06:04 AM
Leith Ross re: Mailmerge Search & Replace 07-19-2010, 07:13 PM
  1. #1
    Registered User
    Join Date
    12-22-2009
    Location
    Pune, India
    MS-Off Ver
    Excel 2003
    Posts
    30

    Mailmerge Search & Replace

    HI,
    I am using the macro to mailmerge data from Excel to word. When merge is done I am finding it difficult to find a specific word and replace it with other word. Below is the code i am using. Fins and replace string is not working in this code.

    Please can any one help me on this one.

    Function MMGA()
    With Session
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    With objWord
    Set objMMMD = objWord.Documents.Open("c:\Doc trial2.doc ")
    objMMMD.Activate
    objMMMD.MailMerge.OpenDataSource _
    name:="C:\ABC.xls", _
    SQLStatement:="SELECT * FROM [Sheet3$]"
    With objMMMD.MailMerge
    .Destination = wdSendToNewDocument
    .SuppressBlankLines = True
    With .DataSource
    .FirstRecord = wdDefaultFirstRecord
    .LastRecord = wdDefaultFirstRecord
    End With
    .Execute
    
    With .Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "Ind"
        .Replacement.Text = "India"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = bWild
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
    End With
    
    End With
    End With
    objMMMD.Close SaveChanges:=wdDoNotSaveChanges
    Set objMMMD = Nothing
    If bCreatedWordInstance Then
    objWord.Quit
    End If
    Set objWord = Nothing
    ' open excel and delete data
     Set Xl = GetObject("C:\ABC.xls")
        Xl.Application.Visible = True
        Xl.Parent.Windows(1).Visible = True
        Xl.activesheet.Unprotect "abc12345"
        Xl.activesheet.Cells.Clear
        Xl.activesheet.Protect "abc12345"
        Xl.Application.Workbooks("ABC.xls").save
        Xl.Application.Visible = True
        Xl.Parent.Windows(1).Visible = True
        Xl.Application.Workbooks("lpm.xls").Close
    
    End With
    End Function
    Last edited by PooH; 07-19-2010 at 01:27 PM. Reason: Added Code tags

  2. #2
    Registered User
    Join Date
    12-22-2009
    Location
    Pune, India
    MS-Off Ver
    Excel 2003
    Posts
    30

    re: Mailmerge Search & Replace

    @ Leith - Thanks for the reply, I tried changing the code but still am not able to get it done.
    I am not sure what i am missing..

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    re: Mailmerge Search & Replace

    Sub snb()
       with GetObject("c:\Doc trial2.doc")
          with .MailMerge
            .OpenDataSource "C:\ABC.xls",SQLStatement:="SELECT * FROM [Sheet3]"
            .Destination = wdSendToNewDocument
            .SuppressBlankLines = True
            With .DataSource
               .FirstRecord = wdDefaultFirstRecord
               .LastRecord = wdDefaultFirstRecord
            End With
            .Execute
         end with       
         with .application.activedocument
            .content.find.execute " Ind", , , , , , , , , " India", 2
            .saveas "C:\test.doc"
            .close 0
         end with
         .Close
      End With
    End Sub
    Last edited by snb; 07-20-2010 at 04:37 AM.

  4. #4
    Registered User
    Join Date
    12-22-2009
    Location
    Pune, India
    MS-Off Ver
    Excel 2003
    Posts
    30

    re: Mailmerge Search & Replace

    Yeap this worked. Thanks snb

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    re: Mailmerge Search & Replace

    Hello Pooh,

    The reason for the code not working is simple. You can not use predefined object constants when using Late Binding. You will need to replace all the Word constants with their actual values.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ Reply to Thread

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