简体   繁体   English

(MS Word / VBA)打开文档时执行邮件合并

[英](MS Word/VBA) Executing Mail Merge when document opens

I've been trying my Macro for testing but somehow something is not working. 我一直在尝试使用Macro进行测试,但是某种方式无法正常工作。 I tried recording a Macro and compared the code to mine. 我尝试录制宏并将代码与我的代码进行比较。 The recorded Macro worked. 录制的宏工作正常。 But as soon as I started modifying it, nothing would happen if I run the Macro. 但是,一旦我开始对其进行修改,运行宏就不会发生任何事情。

I have Word Object Library 我有Word对象库

Working Macro: 工作宏:

Sub DistrictMailMerge()
On Error GoTo NoKTOAccess

ActiveDocument.MailMerge.Destination = wdSendToNewDocument
RunMMKTO
Exit Sub

NoKTOAccess:
    If Err.Number = 5174 Then
        RunMMPEO
    End If
End Sub



Sub RunMMKTO()
'Wrong Filename for testing purpose'
'-----------------------------------
    ActiveDocument.MailMerge.OpenDataSource _
    Name:="\\Astc-ls-001\new_admin\File Sharing\001. KLN 1\Caseworkers\Herman\Masterlist One-Stop Portal.xlsm2", _
    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=\\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
    ActiveDocument.MailMerge.ViewMailMergeFieldCodes = wdToggle
End Sub



Sub RunMMPEO()
    ActiveDocument.MailMerge.OpenDataSource Name:= _
        "\\192.168.9.190\new_admin\File Sharing\Caseworkers\Herman\ISS OSP\Masterlist One-Stop Portal.xlsm" _
        , 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=\\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
    ActiveDocument.MailMerge.ViewMailMergeFieldCodes = wdToggle
End Sub

Macro that does nothing (very rarely error as shown below): 不执行任何操作的宏(很少会出现如下所示的错误):

Error of Run-time error '5852': Requested object is not available 运行时错误“ 5852”的错误:请求的对象不可用

Sub DistrictMailMerge()
On Error GoTo NoKTOAccess

ActiveDocument.MailMerge.Destination = wdSendToNewDocument
RunMMKTO
Exit Sub

NoKTOAccess:
    If Err.Number = 5174 Then
        RunMMPEO
    End If
End Sub


Sub RunMMKTO()
'Wrong Filename for testing purpose'
'-----------------------------------
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.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
.Execute
End With
End Sub



Sub RunMMPEO()
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.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
.Execute
End With
End Sub

There are two possible approaches to your problem. 有两种可能的方法来解决您的问题。

  1. Find out what your sub DistricMailMerge actually does. 找出您的子DistricMailMerge实际做什么。 Put a break point in its first line and press F5. 在其第一行中放置一个断点,然后按F5。 When it hits the stop continue pressing F8 and watch which part of it actually executes. 当它到达停止点时,继续按F8并观察它实际执行的部分。
  2. I am not familiar with MailMerge. 我对MailMerge不熟悉。 Therefore I start here:- https://msdn.microsoft.com/en-us/vba/word-vba/articles/mailmerge-object-word . 因此,我从这里开始: -https : //msdn.microsoft.com/zh-cn/vba/word-vba/articles/mailmerge-object-word I find that your code is quite different from what MS suggests. 我发现您的代码与MS建议的完全不同。 I am intrigued by your line ActiveDocument.MailMerge.Destination = wdSendToNewDocument which seems to specify only that the merge is to be sent to a document without specifying which one, nor how to merge. 我对您的行ActiveDocument.MailMerge.Destination = wdSendToNewDocument感兴趣,该行似乎仅指定将合并发送到文档而不指定哪个文档,也不指定如何合并。 MS suggested code resolves that issue. MS建议的代码解决了该问题。 Without studying further, I find the question of why one of your two versions actually works much more powerful than the one why the other doesn't. 在不做进一步研究的情况下,我发现了一个问题,为什么您的两个版本之一实际上比另一个版本为什么功能更强大?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM