A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Word » Mailmerge
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Word/Excel VBA mail merge fails with filtered results



 
 
Thread Tools Display Modes
  #1  
Old July 9th, 2009, 02:50 PM posted to microsoft.public.word.mailmerge.fields
jm_dunning
external usenet poster
 
Posts: 1
Default Word/Excel VBA mail merge fails with filtered results

I have a VBA macro that copies uses an Excel file to do a mail merge
in Word. Everything works properly, except if the Excel file has
filtering, in which case I get

Run-time error '5640':
Word could not re-establish a DDE connection to Microsoft Excel to
complete the current task

The code I'm using is as follows:

With wordDoc.MailMerge
.OpenDataSource Name:=strSourcePath, Connection:="Entire
Spreadsheet", SQLStatement:=strSQL, SubType:=wdMergeSubTypeWord2000 '
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With


Is there another argument I could use for "Connection:", eg., "Visible
Range"

Any tips would be appreciated.

TIA, John
  #2  
Old July 9th, 2009, 07:07 PM posted to microsoft.public.word.mailmerge.fields
Peter Jamieson
external usenet poster
 
Posts: 4,550
Default Word/Excel VBA mail merge fails with filtered results

You can try

"_FilterDatabase"

instead of

"Entire Spreadsheet"

However, you will then see blank rows where Excel has hidden the rows it
has filtered out, so you would need to change the query. How to do that
may depend on the version of Office - I'm not sure you can set
SQLStatement in the opendatasource, so you might need to leave the
SQLStatement parameter out of your opendatasource statement and put
something like the following code after the opendatasource

..DataSource.Querystring = "SELECT * FROM " & strSourcePath & "WHERE
(mycolumn is not NULL)

But, if you need further assistance...
a. do you really need to use DDE?
b. which version of Word/Excel?

Peter Jamieson

http://tips.pjmsn.me.uk

jm_dunning wrote:
I have a VBA macro that copies uses an Excel file to do a mail merge
in Word. Everything works properly, except if the Excel file has
filtering, in which case I get

Run-time error '5640':
Word could not re-establish a DDE connection to Microsoft Excel to
complete the current task

The code I'm using is as follows:

With wordDoc.MailMerge
.OpenDataSource Name:=strSourcePath, Connection:="Entire
Spreadsheet", SQLStatement:=strSQL, SubType:=wdMergeSubTypeWord2000 '
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With


Is there another argument I could use for "Connection:", eg., "Visible
Range"

Any tips would be appreciated.

TIA, John

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 09:04 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.