|
Question : Word/Excel Mail Merge with variable worksheet name
|
|
I'm trying to write a subroutine in vba to add to Word for a mail merge. The workbook name of the data source is always the same, but the worksheet name will be variable based on the date, for instance if I did it today, the sheet name I would use as the data source would be "110602" Getting the value of the variable won't be a problem, I can handle that OK. I recorded a macro to get an idea what Word does, but I don't know how to put a variable worksheet name in the procedure, in other words replace the literal. I also don't understand why the SQL statements are empty. Of course, I want empty rows skipped. Can I add something within to check a single field that I know should have data and if there is no data ("") then skip that record? Thanks! (Here is the test macro I recorded:
ActiveDocument.MailMerge.OpenDataSource Name:= _ "C:\Documents and Settings\My Documents\testing\testmerge.xls" _ , ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _ AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _ WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _ Format:=wdOpenFormatAuto, Connection:= _ "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=C:\Documents and Settings\My Documents\testing\testmerge.xls;Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLED" _ , SQLStatement:="SELECT * FROM `Sheet3$`", SQLStatement1:="", SubType:= _ wdMergeSubTypeAccess
|
|
Answer : Word/Excel Mail Merge with variable worksheet name
|
|
Here is what i get when recording macro: querying a XL workbook: -source file is E:\Test Import.xls -source data is named range NamedRangeName
SQL: "SELECT Data.FieldName1, Data.FieldName2, Data.FieldName3 " & _ "FROM `E:\test import`.NamedRangeName Data " & _ "WHERE (Data.FieldName1 Is Not Null) ORDER BY Data.FieldName1 "
Replace -FieldName1,2,3 -"E:\test import" -NamedRangeName
Naming the range of data in XL could be a big help to the engine to localise the data (and mainly data boundaries) in the SHeet.
Sébastien
|
|
|
|