|
我测试了一遍没问题啊,另外我改了个直接引用固定工作表的代码,使用的时候,把中间三个提示框MsgBox删掉就好了。
Sub 宏2()
Dim p As String
Dim p1 As String
p1 = Documents(ActiveDocument.Name).Path '取得WORD文档的绝对路径
MsgBox "本文件的路径为:" & p1
Dim qgrmdtj()
myPath = p1 '把文件路径定义给变量
n = 1
myFile = Dir(myPath & "\*.xls") '获取excel文件名
If myFile = "" Then
myFile = Dir(myPath & "\*.xlsx") '当没有xls文件时,改查xlsx文件,获取excel文件名
Else
End If
MsgBox "数据源的名字为:" & myFile
p = p1 & "\" & myFile ''计算出名Excel数据源绝对路径
MsgBox "本文件的绝对路径为:" & p
ActiveDocument.MailMerge.OpenDataSource Name:= _
p, 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=P;Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=37;Jet OLEDB:Database L" _
, SQLStatement:="SELECT * FROM `引用$`", SQLStatement1:="", SubType:= _
wdMergeSubTypeAccess
ActiveDocument.MailMerge.ViewMailMergeFieldCodes = wdToggle '点击预览结果按钮
End Sub
另外有没有高手能把这段代码精简一下,格式必须是这么怪异吗?
ActiveDocument.MailMerge.OpenDataSource Name:= _
p, 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=P;Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=37;Jet OLEDB:Database L" _
, SQLStatement:="SELECT * FROM `引用$`", SQLStatement1:="", SubType:= _
wdMergeSubTypeAccess
|
|