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?
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?