PDA

View Full Version : Merging TExt using VBA



aze_kool
07-11-2004, 10:22 AM
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

Kelly
07-16-2004, 02:37 PM
>
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 (http://www.vbaexpress.com/forum/showthread.php?t=486)


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

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

On Error Resume Next
FileToSave = Trim(Documents(MyDocWithFields).MailMerge.DataSource.DataFields("Location_To_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_Save").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


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 (http://kellyjones.netfirms.com)