Fixing my merge file after field changes

S

Salad

Hi Word pros. I hope you can help me out on this.

I have a document that merges data from Access. The merge file is a
text file I create from the fields the user initially selected for the
document. The merge file might look something like this

"Fld1", "Fld2", "Fld3", "Fld4", "Fld5", "Fld6"
"F1","F2","F3","F4","F5","F6"

I recently changed the database layout and removed Fld3...Fld5. So my
merge text file now looks like this.
Fld1, Fld2, Fld6
"F1","F2",""F6"

I guess since I removed those fields I need to modify the document
somehow as incorrect data is now being merged. What do I need to do?

I'm providing the code I use to pass the merge file to the document if
that helps. The merge file created from MakeMergeCustomText() is OK,
the field names are there, but I guess those missing fields from the
initial creation of the document are still associated with the doc.

Function MergeCustomToWord(strDocName As String, lngCustomID As Long) As
Boolean

Dim WordApp As Object ' running instance of word
Dim WordDoc As Object ' one instance of a word doc
Dim strTemplate As String
Dim strMergeFile As String

'create/generate the data (csv) merge file for the document
strTemplate = MakeMergeCustomText(strDocName, lngCustomID)

If strTemplate <> "" Then
'open word and the document
On Error GoTo CreateWordApp
Set WordApp = GetObject(, "Word.Application")
On Error GoTo 0
On Error GoTo Err_MergeCustomToWord
Set WordDoc = WordApp.Documents.Open(strDocName)
WordDoc.MailMerge.MainDocumentType = 0 ' wdFormLetters = 0

'now display the letter with the merge field values
WordDoc.MailMerge.OpenDataSource _
Name:=strTemplate, _
ConfirmConversions:=False, _
ReadOnly:=False, LinkToSource:=True,
AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="",
WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, Format:=0, _
Connection:="", SQLStatement:="", SQLStatement1:=""
WordDoc.MailMerge.ViewMailMergeFieldCodes = False
WordDoc.MailMerge.ViewMailMergeFieldCodes = False
WordDoc.MailMerge.MainDocumentType = -1
WordDoc.Save

WordDoc.Close (False)

Set WordApp = Nothing
Set WordDoc = Nothing
End Function

TIA for any assistance.
 
D

Doug Robbins - Word MVP

In Word, you will need to open the document represented by strDocName and
remove the superfluous merge fields.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top