请帮助解决这个问题,因为我在最后阶段试图在以下代码中正确定义我的变量。
我想按顺序执行以下操作:
我遇到的问题在于3和4。
运行时错误' 5174':抱歉,我们无法找到您的文件。是吗 移动,重命名或删除?
我尝试了什么: 我考虑过为我的嵌入式文档创建Document Open事件,但它不会起作用。文档仅作为临时文档打开,脚本不会保存。因此,我不能只在开放事件期间执行DistrictMailMerge函数而不使用Excel来代替它。
我认为问题在于这些打开的文件的性质。他们真的不存在'在某种程度上,正常的Word文档会。我想知道是否有人可以帮助我。
这是子程序:
Private Sub CommandButton1_Click()
Sheets("Resource Bank").Select
ActiveSheet.Shapes("CR_MMFormTest").Select
Selection.Verb xlVerbOpen
Call DistrictMailMerge
End Sub
这是我打算用于打开的文档的函数:
Function DistrictMailMerge()
Application.ScreenUpdating = False
On Error GoTo NoKTOAccess
Application.Documents.Add ActiveDocument.FullName
Close_All_Except_Active_Document
RunMMKTO
Exit Function
NoKTOAccess:
If Err.Number = 5174 Then
RunMMPEO
End If
Application.ScreenUpdating = True
End Function
Sub Close_All_Except_Active_Document()
Dim i As Integer
Dim KeepOpen As String
KeepOpen = ActiveDocument.Name
For i = Documents.Count To 1 Step -1
If Documents(i).Name <> KeepOpen Then Documents(i).Close Savechanges:=wdDoNotSaveChanges
Next i
End Sub
Sub RunMMKTO()
With ActiveDocument.MailMerge
.OpenDataSource _
Name:="\\Astc-ls-001\new_admin\File Sharing\001. KLN 1\Caseworkers\Herman\Masterlist One-Stop Portal.xlsm", _
Format:=wdOpenFormatAuto, _
Connection:="Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=\\Astc-ls-001\new_admin\File Sharing\001. KLN 1\Caseworkers\Herman\Masterlist One-Stop Portal.xlsm;Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet OLEDB:Registry ", _
SQLStatement:="SELECT * FROM [CR Step 2 - Mail Merge List$] WHERE [ISS No#] LIKE '%-%'", _
SQLStatement1:="", SubType:=wdMergeSubTypeAccess
.ViewMailMergeFieldCodes = wdToggle
End With
End Sub
Sub RunMMPEO()
With ActiveDocument.MailMerge
.OpenDataSource _
Name:="\\192.168.9.190\new_admin\File Sharing\Caseworkers\Herman\ISS OSP\Masterlist One-Stop Portal.xlsm", _
Format:=wdOpenFormatAuto, _
Connection:="Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=\\Astc-ls-001\new_admin\File Sharing\001. KLN 1\Caseworkers\Herman\Masterlist One-Stop Portal.xlsm;Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet OLEDB:Registry ", _
SQLStatement:="SELECT * FROM [CR Step 2 - Mail Merge List$] WHERE [ISS No#] LIKE '%-%'", _
SQLStatement1:="", SubType:=wdMergeSubTypeAccess
.ViewMailMergeFieldCodes = wdToggle
End With
End Sub