CSV datasource ends Word Mail Merge after 2 records

M

Miles

Hello,

Can anybody shed any light on this? I'm having a problem when using a
Mergefield inside an IncludePicture, but only when using a tab- or
comma-separated (CSV) datasource. Other types of datasources (e.g. Excel,
SQL, etc) work fine, but I must use CSV in this application.

The symptom is that the merge works normally for the first two records, then
stops normally. No errors are thrown, and this always occurs after exactly 2
records no matter the datasource's length or the template doc's length.

The catch is that I'm using a MailMergeBeforeRecordMerge event like this:
---------
Private Sub WordApp_MailMergeBeforeRecordMerge(ByVal Doc As Document, Cancel
As Boolean)
Dim shp As Word.InlineShape
Dim counter As Integer
counter = 0
For Each shp In Doc.InlineShapes
Set flds = shp.Field.Code.Fields
'!!!! Statement below causes only 2 pages to merge. Comment out next
line & everything works OK
counter = counter + flds.Count
Next shp
End Sub
---------
As the comment says, accessing "flds.Count" is what causes the problem.
Removing the reference to flds.Count gets rid of the problem, but this is
just a stripped-down example to demo the problem. In the real macro, I must
access the Count field and do a bunch of other stuff in the same vein that
cause the same problem, so I can't workaround by just not referring to the
shape's fields.

I've tried this as both a VBA macro and as a real COM Word add-in with
exactly the same results. I've tested with both Word XP & 03 with all Service
Packs, same result; don't know about other Word versions.

Here are the formal steps to reproduce (a copy of these files is available
at http://onmerge.com/csvbug.zip)

- Create a csv file like this:

Beatle,Recipient
John,Polly
Paul,Lisa
George,Freddi
Ringo,Diana

- Create a doc, press Alt-F9 and enter these nested fields:
{ INCLUDEPICTURE "{ MERGEFIELD "Beatle" }.gif" \*MERGEFORMAT \d }
Press Alt-F9 again, then Ctrl-A, F9 to update the fields into a graphic

- Load up the csv file as the merge datasource

- Open up VBE, create a Class Module named WordApp in the doc like this:
'-----------------
Public WithEvents WordApp As Word.Application

Private Sub WordApp_MailMergeBeforeRecordMerge(ByVal Doc As Document, Cancel
As Boolean)
Dim shp As Word.InlineShape
Dim counter As Integer
counter = 0
For Each shp In Doc.InlineShapes
Set flds = shp.Field.Code.Fields
'!!!! Statement below causes only 2 pages to merge.
'!!!! Comment out next line & everything works OK
counter = counter + flds.Count
Next shp
End Sub
'---------------------

- Still in VBE, create an ordinary Module named Init like this:
'------------
Dim X As New WordApp
Sub Register_Event_Handler()
Set X.WordApp = Word.Application
End Sub
'------------------

- Still in VBE, hit Run, select Register_Event_Handler to run, OK.

- Back in the document, do a Merge to New Document. The merge will seem to
work normally, but will only have 2 records.

Thoughts, anyone?
 
M

macropod

Hi Miles,

Is it possible your first two records don't have any in-line shapes?

I take it you're trying to get a count of the fields in each in-line shape, so perhaps something like:

Private Sub WordApp_MailMergeBeforeRecordMerge(ByVal Doc As Document, Cancel As Boolean)
Dim shp As Word.InlineShape
Dim counter As Integer
counter = 0
For Each shp In Doc.InlineShapes
counter = counter + shp.Range.Fields.Count
Next shp
End Sub
 
M

Miles

Hi Macropod --

I tried your suggestion -- no change. I have verified that all datasource
records are valid and resolve to proper inline shapes.

Note that, as I was saying, this happens only on CSV files. I can take the
CSV file, import into Excel, save as-is to an .xls file, then use the .xls as
a datasource. I then get all records & everything works perfectly! Too bad I
can't just use an .xls file in my app -- only a CSV.

Any other thoughts on this? I'd suspect an obscure bug in Word's CSV
datasource logic, but this problem happens as far back as Word 2002, and it's
a little hard to believe that nobody's noticed in 5 years. Obviously,
searching turns up no hints.

macropod said:
Hi Miles,

Is it possible your first two records don't have any in-line shapes?

I take it you're trying to get a count of the fields in each in-line shape, so perhaps something like:

Private Sub WordApp_MailMergeBeforeRecordMerge(ByVal Doc As Document, Cancel As Boolean)
Dim shp As Word.InlineShape
Dim counter As Integer
counter = 0
For Each shp In Doc.InlineShapes
counter = counter + shp.Range.Fields.Count
Next shp
End Sub

--
Cheers
--
macropod
[MVP - Microsoft Word]


Miles said:
Hello,

Can anybody shed any light on this? I'm having a problem when using a
Mergefield inside an IncludePicture, but only when using a tab- or
comma-separated (CSV) datasource. Other types of datasources (e.g. Excel,
SQL, etc) work fine, but I must use CSV in this application.

The symptom is that the merge works normally for the first two records, then
stops normally. No errors are thrown, and this always occurs after exactly 2
records no matter the datasource's length or the template doc's length.

The catch is that I'm using a MailMergeBeforeRecordMerge event like this:
---------
Private Sub WordApp_MailMergeBeforeRecordMerge(ByVal Doc As Document, Cancel
As Boolean)
Dim shp As Word.InlineShape
Dim counter As Integer
counter = 0
For Each shp In Doc.InlineShapes
Set flds = shp.Field.Code.Fields
'!!!! Statement below causes only 2 pages to merge. Comment out next
line & everything works OK
counter = counter + flds.Count
Next shp
End Sub
---------
As the comment says, accessing "flds.Count" is what causes the problem.
Removing the reference to flds.Count gets rid of the problem, but this is
just a stripped-down example to demo the problem. In the real macro, I must
access the Count field and do a bunch of other stuff in the same vein that
cause the same problem, so I can't workaround by just not referring to the
shape's fields.

I've tried this as both a VBA macro and as a real COM Word add-in with
exactly the same results. I've tested with both Word XP & 03 with all Service
Packs, same result; don't know about other Word versions.

Here are the formal steps to reproduce (a copy of these files is available
at http://onmerge.com/csvbug.zip)

- Create a csv file like this:

Beatle,Recipient
John,Polly
Paul,Lisa
George,Freddi
Ringo,Diana

- Create a doc, press Alt-F9 and enter these nested fields:
{ INCLUDEPICTURE "{ MERGEFIELD "Beatle" }.gif" \*MERGEFORMAT \d }
Press Alt-F9 again, then Ctrl-A, F9 to update the fields into a graphic

- Load up the csv file as the merge datasource

- Open up VBE, create a Class Module named WordApp in the doc like this:
'-----------------
Public WithEvents WordApp As Word.Application

Private Sub WordApp_MailMergeBeforeRecordMerge(ByVal Doc As Document, Cancel
As Boolean)
Dim shp As Word.InlineShape
Dim counter As Integer
counter = 0
For Each shp In Doc.InlineShapes
Set flds = shp.Field.Code.Fields
'!!!! Statement below causes only 2 pages to merge.
'!!!! Comment out next line & everything works OK
counter = counter + flds.Count
Next shp
End Sub
'---------------------

- Still in VBE, create an ordinary Module named Init like this:
'------------
Dim X As New WordApp
Sub Register_Event_Handler()
Set X.WordApp = Word.Application
End Sub
'------------------

- Still in VBE, hit Run, select Register_Event_Handler to run, OK.

- Back in the document, do a Merge to New Document. The merge will seem to
work normally, but will only have 2 records.

Thoughts, anyone?
 
M

macropod

Hi Miles,

At this stage the only other thing I can suggest is to use your mailmerge sub to open an Excel session, which then opens & converts
to csv to xls, and merge from the xls file (which you could close & delete programmatically afterwards).

--
Cheers
macropod
[MVP - Microsoft Word]


Miles said:
Hi Macropod --

I tried your suggestion -- no change. I have verified that all datasource
records are valid and resolve to proper inline shapes.

Note that, as I was saying, this happens only on CSV files. I can take the
CSV file, import into Excel, save as-is to an .xls file, then use the .xls as
a datasource. I then get all records & everything works perfectly! Too bad I
can't just use an .xls file in my app -- only a CSV.

Any other thoughts on this? I'd suspect an obscure bug in Word's CSV
datasource logic, but this problem happens as far back as Word 2002, and it's
a little hard to believe that nobody's noticed in 5 years. Obviously,
searching turns up no hints.

macropod said:
Hi Miles,

Is it possible your first two records don't have any in-line shapes?

I take it you're trying to get a count of the fields in each in-line shape, so perhaps something like:

Private Sub WordApp_MailMergeBeforeRecordMerge(ByVal Doc As Document, Cancel As Boolean)
Dim shp As Word.InlineShape
Dim counter As Integer
counter = 0
For Each shp In Doc.InlineShapes
counter = counter + shp.Range.Fields.Count
Next shp
End Sub

--
Cheers
--
macropod
[MVP - Microsoft Word]


Miles said:
Hello,

Can anybody shed any light on this? I'm having a problem when using a
Mergefield inside an IncludePicture, but only when using a tab- or
comma-separated (CSV) datasource. Other types of datasources (e.g. Excel,
SQL, etc) work fine, but I must use CSV in this application.

The symptom is that the merge works normally for the first two records, then
stops normally. No errors are thrown, and this always occurs after exactly 2
records no matter the datasource's length or the template doc's length.

The catch is that I'm using a MailMergeBeforeRecordMerge event like this:
---------
Private Sub WordApp_MailMergeBeforeRecordMerge(ByVal Doc As Document, Cancel
As Boolean)
Dim shp As Word.InlineShape
Dim counter As Integer
counter = 0
For Each shp In Doc.InlineShapes
Set flds = shp.Field.Code.Fields
'!!!! Statement below causes only 2 pages to merge. Comment out next
line & everything works OK
counter = counter + flds.Count
Next shp
End Sub
---------
As the comment says, accessing "flds.Count" is what causes the problem.
Removing the reference to flds.Count gets rid of the problem, but this is
just a stripped-down example to demo the problem. In the real macro, I must
access the Count field and do a bunch of other stuff in the same vein that
cause the same problem, so I can't workaround by just not referring to the
shape's fields.

I've tried this as both a VBA macro and as a real COM Word add-in with
exactly the same results. I've tested with both Word XP & 03 with all Service
Packs, same result; don't know about other Word versions.

Here are the formal steps to reproduce (a copy of these files is available
at http://onmerge.com/csvbug.zip)

- Create a csv file like this:

Beatle,Recipient
John,Polly
Paul,Lisa
George,Freddi
Ringo,Diana

- Create a doc, press Alt-F9 and enter these nested fields:
{ INCLUDEPICTURE "{ MERGEFIELD "Beatle" }.gif" \*MERGEFORMAT \d }
Press Alt-F9 again, then Ctrl-A, F9 to update the fields into a graphic

- Load up the csv file as the merge datasource

- Open up VBE, create a Class Module named WordApp in the doc like this:
'-----------------
Public WithEvents WordApp As Word.Application

Private Sub WordApp_MailMergeBeforeRecordMerge(ByVal Doc As Document, Cancel
As Boolean)
Dim shp As Word.InlineShape
Dim counter As Integer
counter = 0
For Each shp In Doc.InlineShapes
Set flds = shp.Field.Code.Fields
'!!!! Statement below causes only 2 pages to merge.
'!!!! Comment out next line & everything works OK
counter = counter + flds.Count
Next shp
End Sub
'---------------------

- Still in VBE, create an ordinary Module named Init like this:
'------------
Dim X As New WordApp
Sub Register_Event_Handler()
Set X.WordApp = Word.Application
End Sub
'------------------

- Still in VBE, hit Run, select Register_Event_Handler to run, OK.

- Back in the document, do a Merge to New Document. The merge will seem to
work normally, but will only have 2 records.

Thoughts, anyone?
 

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