excel VBA在邮件合并期间出现提示时自动选​​择“是”

时间:2012-07-02 21:54:41

标签: excel vba alert blocking mailmerge

我希望系统尽可能为我的用户自动化。现在,我有代码在用户单击按钮时运行。该代码获取数据的目的是通过邮件合并将其应用于word文档。

一切都按预期工作,除非总是弹出一条消息说

  

打开此文档将运行以下SQL命令:

     

选择* FROM'TAGS $'

     

数据库中的数据将放在文档中。你想要_____吗   继续?

我需要保持尽可能简单,不要让用户选择“否”,因为他们感到困惑。 VBA如何自动继续并接受数据放置,就像他们选择“是”一样?

我尝试使用以下代码阻止警报,希望它默认为“是”并继续,但它不起作用。

Application.DisplayAlerts = False

这就是我所拥有的

Sub RunMailMerge()

    Application.ScreenUpdating = False

    Dim wdOutputName, wdInputName As String
    wdOutputName = ThisWorkbook.Path & "\nametags - " _
        & Format(Date, "d mmm yyyy")
    wdInputName = ThisWorkbook.Path & "\nametags.docx"

    ' open the mail merge layout file
    Dim wdDoc As Object
    Set wdDoc = GetObject(wdInputName, "Word.document")
    wdDoc.Application.Visible = True

    With wdDoc.MailMerge
         .MainDocumentType = wdFormLetters
         .Destination = wdSendToNewDocument
         .SuppressBlankLines = True
         .Execute Pause:=False
    End With

    'Application.ScreenUpdating = True

    'show and save output file
    wdDoc.Application.Visible = True
    wdDoc.Application.ActiveDocument.SaveAs wdOutputName

    ' cleanup
    wdDoc.Close SaveChanges:=False
    'activedoc.Close
    Set wdDoc = Nothing

End Sub

2 个答案:

答案 0 :(得分:2)

尝试在Word中设置DisplayAlerts属性(如果是警报的来源):

Dim tmp as Long

tmp = wdDoc.Application.DisplayAlerts 

wdDoc.Application.DisplayAlerts = wdAlertsNone
'do the action which causes the prompt
wdDoc.Application.DisplayAlerts = tmp

答案 1 :(得分:2)

http://support.microsoft.com/kb/825765

  

Word 2013

HKEY_CURRENT_USER \ Software \ Microsoft \ Office \ 15.0 \ Word \ Options

" SQLSecurityCheck" = DWORD:00000000

Start Registry Editor.
Locate and then click the following registry key:

HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Word\Options
On the Edit menu, point to New, and then click DWORD Value.
Under Name, type:

SQLSecurityCheck
Double-click SQLSecurityCheck.
In the Value data box, type:

00000000
Click OK.
  

Word 2010

HKEY_CURRENT_USER \ Software \ Microsoft \ Office \ 14.0 \ Word \ Options

" SQLSecurityCheck" = DWORD:00000000

Start Registry Editor.
Locate and then click the following registry key:
HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Word\Options
On the Edit menu, point to New, and then click DWORD Value.
Under Name, type:
SQLSecurityCheck
Double-click SQLSecurityCheck.
In the Value data box, type:
00000000
Click OK.
  

Word 2007

HKEY_CURRENT_USER \软件\微软\办公室\ 12.0 \ Word中\选项

" SQLSecurityCheck" = DWORD:00000000

Start Registry Editor.
Locate and then click the following registry key:
HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Word\Options
On the Edit menu, point to New, and then click DWORD Value.
Under Name, type:
SQLSecurityCheck
Double-click SQLSecurityCheck.
In the Value data box, type:
00000000
Click OK.
  

Word 2003

HKEY_CURRENT_USER \软件\微软\办公室\ 11.0 \ Word中\选项

" SQLSecurityCheck" = DWORD:00000000

Start Registry Editor.
Locate and then click the following registry key:
HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Word\Options
Click Edit, point to New, and then click DWORD Value.
Under Name, type:
SQLSecurityCheck
Double-click SQLSecurityCheck.
In the Value data box, type:
00000000
Click OK.
  

Word 2002 Service Pack 3

HKEY_CURRENT_USER \软件\微软\办公室\ 10.0 \ Word中\选项

" SQLSecurityCheck" = DWORD:00000000

为此,请按照下列步骤操作:

Start Registry Editor.
Locate and then click the following registry key:
HKEY_CURRENT_USER\Software\Microsoft\Office\10.0\Word\Options
Click Edit, point to New, and then click DWORD Value.
Under Name, type:
SQLSecurityCheck
Double-click SQLSecurityCheck.
In the Value data box, type:
00000000
Click OK.