Consulting

Results 1 to 8 of 8

Thread: Word Mail Merge - CSV Datafile - change fieldnames before merge?

  1. #1
    VBAX Regular jeeves's Avatar
    Joined
    Jan 2005
    Location
    Canada
    Posts
    14
    Location

    Question Word Mail Merge - CSV Datafile - change fieldnames before merge?

    Backround info to question:
    So I get to customize our current setup at work to accomodate an automated mail merge in an upgraded version of one of our apps.
    This application is a contact database that exports contact data to a csv file to be used as the merge data field for Word. We have this functioning properly for our current version of the application, but it's due for an "upgrade" this year, and the base user population will be getting a web version of the app, with what has turned out to be limited functionality. The version in production allowed us to edit what fields were exported, in what order, and how they were named.

    So basically the upgraded application's exported fields do not match the existing fields in our automated merge set up. After much discussion with the vendor, it turns out that this is the way their app is coded and they will not change it for us. Well, they may develop a new function altogether, but for a price.

    Any tips... I need to change the fieldnames in the CSV file before running the merge in Word. I'm hoping to address this as part of the existing code in Word, rather than within Excel in the CSV since a new CSV file will be created every time the contact information is exported. Most of the fieldnames are close, i.e. "addr_line_3" instead of "address_3".

    Ideas?
    Note: it is not feasible for me to change the fields in our template suite to match those in the exported data file.

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Can you post an attachment with the file you want to change?

  3. #3
    VBAX Regular jeeves's Avatar
    Joined
    Jan 2005
    Location
    Canada
    Posts
    14
    Location
    Quote Originally Posted by DRJ
    Can you post an attachment with the file you want to change?
    I can't, unfortunately. The two templates that I'm editing are vendor produced, paid for software. I'm the in-house developer that gets to put it all together to make it work.
    When I'm in the office tomorrow, I'll see if I can copy in specific parts of the code...

    thanks for your response...

  4. #4
    VBAX Regular jeeves's Avatar
    Joined
    Jan 2005
    Location
    Canada
    Posts
    14
    Location
    OK... I'm back after a hellish week @ work. It's Friday night, so no gorey details of sitting in meeting after meeting and getting no work done.

    Anyway, I think I have a better grasp of what I'm trying to do here.

    The 3rd party contact management app exports the data to a CSV in the same format/layout each time, but the field (column) names do not match the fields in my existing merge templates.

    I need to either execute a find and replace, or just rename certain fields

    i.e.
    Field: 9
    fieldname: "JobTitle"
    I need to rename it to "Job Title" so it matches the existing autotext entry in my merge templates. I have to change the names of approximately 20 fields. They will always be in the same order when exported.

    I've figured out that I can open the csv from Word and i'm assuming that i can make changes to it programmatically before executing the merge.

    [VBA]ActiveDocument.MailMerge.OpenDataSource path + fName
    [/VBA]
    Then what?
    Any suggestions, tips are appreciated. I'm on my own for this one, and brand new to VBA.

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Jeeves,
    You need to add your old/new field names into the arrays replacing my sample code, then run the following
    MD

    [VBA]
    Option Explicit
    Option Compare Text
    Sub ChangeHeader()

    Dim ToFind
    Dim ReplaceWith
    Dim TF
    Dim i As Integer

    ActiveDocument.MailMerge.EditDataSource

    ToFind = Array("Add1", "Add2", "Add5")
    ReplaceWith = Array("Add_1", "Add_2", "Add_5")
    i = -1
    For Each TF In ToFind
    i = i + 1
    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = ToFind(i)
    .Replacement.Text = ReplaceWith(i)
    .Forward = True
    .MatchWholeWord = True
    End With
    Selection.Find.Execute Replace:=wdReplaceOne
    Next

    End Sub


    [/VBA]
    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'

  6. #6
    VBAX Regular jeeves's Avatar
    Joined
    Jan 2005
    Location
    Canada
    Posts
    14
    Location
    Thanks very much! I'm working with your sample code now.
    I've gotten it to work once, and I'm trying to convince it to work again consistently!

  7. #7
    VBAX Regular jeeves's Avatar
    Joined
    Jan 2005
    Location
    Canada
    Posts
    14
    Location
    thanks very much! the code you provided, with a bit of tweaking, did the trick.

    The only thing is, it opens the CSV on the desktop - is there a command I can provide to get have the CSV opened and edited so the user will not see it? It's not just the window placement, and the prompt to save and close, I can't add any extra steps to the existing process.

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Jeeves,
    Modified to suit
    MD

    [VBA] Option Explicit
    Option Compare Text
    Sub ChangeHeader()

    Dim ToFind, ReplaceWith
    Dim MyDS As String
    Dim TF
    Dim i As Integer

    Application.ScreenUpdating = False
    MyDS = ActiveDocument.MailMerge.DataSource.Name
    ActiveDocument.MailMerge.EditDataSource
    ToFind = Array("Add1", "Add2", "Add5")
    ReplaceWith = Array("Add_1", "Add_2", "Add_5")
    i = -1
    For Each TF In ToFind
    i = i + 1
    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = ToFind(i)
    .Replacement.Text = ReplaceWith(i)
    .Forward = True
    .MatchWholeWord = True
    End With
    Selection.Find.Execute Replace:=wdReplaceOne
    Next
    Documents(MyDS).Close True
    Application.ScreenUpdating = True
    End Sub
    [/VBA]
    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'

Posting Permissions

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