|
楼主 |
发表于 2014-6-19 20:21
|
显示全部楼层
请“守柔”老师帮忙!
本帖最后由 gwfzh 于 2014-6-20 11:01 编辑
请各位老师帮帮忙,能否用下面VBA增加邮件合并中合并域的数目?谢谢了!!!
Sub Document_Open()
Dim strExcel As String
Dim strSheet As String
Dim strDataSource As String
Dim strConnection As String
Dim strQuery As String
Dim tempPath As String
'p1 = Documents(ActiveDocument.Name).Path '取得主文档绝对路径
tempPath = Me.Path '取得主文档绝对路径
'strExcel = "疫情年分析表" '定义Excel数据源工作簿文件名
strSheet = "H" '定义Excel数据源工作簿中工作表的名称
' set this to be the file name of your data source
strDataSource = tempPath & "\" & strExcel & ".xlsm" '计算出Excel数据源工作簿绝对路径,
'strDataSource = ThisWorkbook.Path & "\" & 疫情年分析表 & ".xls"
' set this to be the connection string for your data source
strConnection = ""
strQuery = "select * from " & strDataSource 'MMerge.TXT"
'以下是将Excel数据源载入主文档,通过录制取得
With ActiveDocument
With MailMerge
.OpenDataSource Name:= _
strDataSource, _
ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:= _
"Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=" & strDataSource & ";Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet OLEDB:RegistryPath="""";Jet OLEDB:Database Password", SQLStatement:="SELECT * FROM `" & strSheet & "$`", SQLStatement1:="", SubType:= _
wdMergeSubTypeAccess
End With
End With
End Sub |
|