Consulting

Results 1 to 2 of 2

Thread: Merging TExt using VBA

  1. #1
    VBAX Newbie
    Joined
    Jul 2004
    Posts
    1
    Location

    Merging TExt using VBA

    Hi ALL
    I have a word document with some Mail merge Fields connected to the datasource(a text file).
    some Mergefield's values are actullay the path of some other word document in a subfolder.
    the mergefields starting with the "Inc" are those fields.


    I need to write a macro that get the path from such field.value and get the content and update the Field with the text of this word doc.



    In my Main Document there are some Fields one of them is <<Inc_ABC >>

    Public Sub GetTheText()
    Dim strFilename As String
    For Each afield In ActiveDocument.MailMerge.DataSource.DataFields

    If InStr(1, afield.Name, "Inc", 1) Then

    If afield.Value <> "" Then
    Set wDoc = Documents.Open(afield.Value)
    Set rng = wDoc.Content
    ThisDocument.Content = rng
    wDoc.Close
    End If
    End If
    Next afield


    End Sub

    in My Datasource text file there are following entries:
    "Inc_ABC"; (The MergeField itself)

    "E:\work\SubDocuments\supplier-01.doc";(The Value of Merge Field)



    I want to get the Text from the word file stored in the above path and update the merge field with the text.
    I tried to do this with the above macro but i dont really know how to update the Merge field in MAcro with the text.


    So the Filepath I will get on the fly and i have to update the correspoding field with the content given in the word doc saved in th path.



    Please help me out

  2. #2
    VBAX Regular Kelly's Avatar
    Joined
    Jun 2004
    Location
    Los Angeles, California
    Posts
    84
    Quote Originally Posted by aze_kool
    >
    Hi ALL
    I have a word document with some Mail merge Fields connected to the datasource(a text file).
    some Mergefield's values are actullay the path of some other word document in a subfolder.
    the mergefields starting with the "Inc" are those fields.

    I need to write a macro that get the path from such field.value and get the content and update the Field with the text of this word doc.

    Please help me out
    Hello!

    I believe I may have what you are looking for.

    Please check out a similar mail merge thread.

    Please click here to read through the mail merge thread started by Baillieston


    Aze_cool, I think you are mainly looking for something like the following, correct?

    [vba] '*****RETRIEVE SAVE-TO LOCATION AND NAME FROM THE MERGE FIELDS***********

    On Error Resume Next
    FileToSave = Trim(Documents(MyDocWithFields).MailMerge.DataSource.DataFields("Location_T o_Save").Value)
    If Err.Number <> 0 Then ErrorHandle Err.Number, "Location_To_Save", Name_of_Source
    On Error Goto 0

    If Right(FileToSave, 1) <> "\" And Right(FileToSave, 1) <> "/" Then
    FileToSave = FileToSave & "\"
    End If

    PathOfFileToSave = FileToSave

    On Error Resume Next
    NameToSave = Trim(Documents(MyDocWithFields).MailMerge.DataSource.DataFields("Name_To_Sa ve").Value)
    If Err.Number <> 0 Then ErrorHandle Err.Number, "Name_To_Save", Name_of_Source
    On Error Goto 0

    FileToSave = FileToSave & NameToSave & ".doc"


    '*******PERFORM THE MERGE AND SAVE NEW DOC**************

    Application.DisplayAlerts = wdAlertsNone

    With ActiveDocument.MailMerge
    .Destination = wdSendToNewDocument
    With .DataSource
    .FirstRecord = wdDefaultFirstRecord
    .LastRecord = wdDefaultLastRecord
    End With
    .Execute Pause:=True
    End With

    Application.DisplayAlerts = wdAlertsAll

    On Error Resume Next

    ActiveDocument.SaveAs FileName:=FileToSave, FileFormat:=wdFormatDocument [/vba]


    The above code is BUT A SMALL PIECE of a larger macro. you can see the entire macro (along with the Error Handler mentioned in this code snippet) at the VBA for mail merge that began on 7-13-2004.

    Please let me know if this helps. You might try adapting it for your particular situation, and then if it still doesn't work, post your new code here in this thread and we can look at it.

    Also, you may wish to download and run the complete macro from the other thread to see if that macro perhaps does the job for you.

    Best wishes!
    -Kelly
    _____________________
    Kelly's Humble Home Page

Posting Permissions

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