问题
I'm using a macro, launched from a word document, which opens, executes, and closes a series of other documents, each of which contains a macro that executes a mail merge. The code works fine, but each time the new document is opened, I am given an SQL prompt, asking if I want to put the data into my document from the datasource.
I can just say 'Yes' to all of these dialog boxes, but
- a) I would like to automate this to make it easier
- b) Other users might also use this document, and I don't want to risk them breaking things by selecting 'No' because they are confused
Using the following thread I managed to put code in that selects the 'default' option for all message boxes, BUT it doesn't work because, for me, the default option for the SQL prompt is 'No'.
excel VBA to Automatically select Yes when prompted during mail merge
(I know you can also disable the prompt by going into the registry but that's not an option for me as I'll need it to work on many computers and I can't edit the registry on all of them for various reasons).
Here's the code at the moment (file paths redacted):
Sub castingbookmaster()
'
' castingbookmaster Macro
'
'
Dim tmp as Long
tmp = Documents.Application.Displayalerts
Documents.Application.Displayalerts = 0
ChangeFileOpenDirectory "Y:\zzz\"
Documents.Open FileName:= _
"Y:\zzz\Mail Merge - All Active Scripts, Alphabetical.docm" _
, ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto, XMLTransform:=""
Application.Run MacroName:="CastingBook1"
ActiveWindow.Close SaveChanges:=wdDoNotSaveChanges
ChangeFileOpenDirectory "Y:\zzz\"
Documents.Open FileName:= _
"Y:\zzz\Mail Merge - Theatre, Active.docm", _
ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto, XMLTransform:=""
Application.Run MacroName:="theatre"
ActiveWindow.Close SaveChanges:=wdDoNotSaveChanges
ChangeFileOpenDirectory "Y:\zzz\"
Documents.Open FileName:= _
"Y:\zzz\Mail Merge - UK Casting Directors.docm", _
ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto, XMLTransform:=""
Application.Run MacroName:="UKcastingdirectors"
ActiveWindow.Close SaveChanges:=wdDoNotSaveChanges
ChangeFileOpenDirectory "Y:\zzz\"
Documents.Open FileName:= _
"Y:\zzz\Mail Merge - US Casting.docm", _
ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto, XMLTransform:=""
Application.Run MacroName:="UScasting"
ActiveWindow.Close SaveChanges:=wdDoNotSaveChanges
Documents.Application.DisplayAlerts = tmp
MsgBox "All Casting Books have been updated."
End Sub
So, I either need
- a) a way of setting the default value of the SQL prompt to 'Yes', then allowing DisplayAlerts = 0 to take care of it
- b) a way of automatically selecting 'Yes' to the SQL prompt
If anyone could help it would be much appreciated.
回答1:
save each of your documents without the data source attached, but modify the VBA code in each document to do the appropriate OpenDataSource
来源:https://stackoverflow.com/questions/33801623/how-to-use-vba-to-say-yes-to-any-sql-prompts-in-word