问题
I'd like the system to be as automated for my users as possible. Right now, I have code that runs when the user clicks a button. The code takes data with the intention of applying it to a word document via mail merge.
Everything works as intended except there's always a message that pops up saying
Opening this document will run the following SQL command:
Select * FROM 'TAGS$'
Data from your database will be placed in the document. Do you want to continue?
I need to keep this as simple as possible without risking users selecting "No" because they're confused. How can VBA automatically proceed and accept the data placement, as it would had they selected "Yes"?
I tried just using the following code to block alerts in hopes it would default "Yes" and proceed, but it didn't work.
Application.DisplayAlerts = False
This is what I have
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
回答1:
Try setting the DisplayAlerts property in Word (if that's where the alert is coming from):
Dim tmp as Long
tmp = wdDoc.Application.DisplayAlerts
wdDoc.Application.DisplayAlerts = wdAlertsNone
'do the action which causes the prompt
wdDoc.Application.DisplayAlerts = tmp
回答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\Software\Microsoft\Office\12.0\Word\Options
"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\Software\Microsoft\Office\11.0\Word\Options
"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\Software\Microsoft\Office\10.0\Word\Options
"SQLSecurityCheck"=dword:00000000
To do this, follow these steps:
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.
来源:https://stackoverflow.com/questions/11301716/excel-vba-to-automatically-select-yes-when-prompted-during-mail-merge