VBA激活Word但功能区无响应,加载项未加载

时间:2015-07-31 22:20:58

标签: excel vba ms-word office-2013

我在Windows 8.1上的Office 2013中有一个Excel VBA宏,提示用户选择Word文档,然后使用Excel中当前打开的电子表格在Word中驱动mailmerge,并通过激活Word完成,以便用户可以编辑合并后的文件。它完全按照我想要的方式工作。

但有两个问题:合并后的文档现在在Word中处于活动状态,但是a)单击Ribbon选项卡和控件时无法识别(即没有任何反应),以及b)未加载我的Word启动目录中的加载项。

这两个问题都可以通过快速Alt-tabbing到Excel并返回到Word来解决。但是我怎么能首先防止它呢?这是供知识有限的用户使用,并且必须解释alt-tab以及为什么他们需要这样做不会很漂亮。

Public Sub MergeMarkedPatients()
    Dim wd As Word.Application
    Dim doc As Word.Document
    Dim src As String, tmpl As String, sh As String
    Dim savepath As String, filename As String
    tmpl = SelectFile(Array("Bill Templates", "*.doc; *.docx"), gDocumentsDir)
    If tmpl = "" Then Exit Sub
    Set wd = New Word.Application
    wd.Visible = True
    Set doc = wd.Documents.Open(filename:=tmpl)
    src = ActiveWorkbook.FullName
    sh = ActiveWorkbook.Worksheets(1).Name & "$"
    savepath = wd.ActiveDocument.Path & Application.PathSeparator
    filename = Replace(GetBasename(wd.ActiveDocument.FullName), "!", "") & " " _
        & Left(ActiveWorkbook.Worksheets(1).Name, 9)
    With doc.MailMerge
        .OpenDataSource Name:=src, ConfirmConversions:=False, ReadOnly:=False, _
            LinkToSource:=True, AddToRecentFiles:=False, PasswordDocument:="", _
            PasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
            Connection:="Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=" _
            & src & ";Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:" _
            & "System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine " _
            &"Type=35;Jet OLEDB:Databa", SQLStatement:="SELECT * FROM `" & sh _
            & "` WHERE PRINT=""P""", SQLStatement1:="", SubType:=wdMergeSubTypeAccess
        .Destination = wdSendToNewDocument
        .MailAsAttachment = False
        .MailAddressFieldName = ""
        .MailSubject = ""
        .SuppressBlankLines = True
        With .DataSource
            .FirstRecord = wdDefaultFirstRecord
            .LastRecord = wdDefaultLastRecord
        End With
        .Execute Pause:=True
    End With
    doc.Close savechanges:=wdDoNotSaveChanges
    ProcessMerges wd.ActiveDocument
    wd.ActiveDocument.BuiltinDocumentProperties(wdPropertyTitle).Value = savepath _
        & filename
    wd.Activate
End Sub

1 个答案:

答案 0 :(得分:0)

在加载项问题上,我曾经使用自动化启动Excel时遇到同样的问题。修复是遍历加载项并卸载,然后重新加载应加载的加载项。我怀疑Word也是如此。见this SO answer for the code