vba 如何使用代码禁用 MS Word“无宏文档”警告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10821263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable the MS Word "macro-free document" warning with code?
提问by Michael
I have an application that dynamically insert VBA code to help building a MS word document with proper bookmarks. The VBA code doesn't need to be saved with the document itself.
我有一个动态插入 VBA 代码的应用程序,以帮助构建带有适当书签的 MS Word 文档。VBA 代码不需要与文档本身一起保存。
When saving the document, the following warning (unfortunately I can't post an image yet) will pop up that confuses the end users of the application. Is there a way to disable this within the DocumentBeforeSave event?
保存文档时,将弹出以下警告(不幸的是我还不能发布图像),这会使应用程序的最终用户感到困惑。有没有办法在 DocumentBeforeSave 事件中禁用它?
**
**
The following cannot be saved in a macro-free document: VBA project To save a file with these features, click No to return to the Save As dialog, and then choose a macro-enabled file type in the File Type drop-down. Continue saving as a macro-free document? buttons: [Yes][No][Help]
以下内容无法保存在无宏文档中: VBA 项目 要保存具有这些功能的文件,请单击否返回另存为对话框,然后在文件类型下拉列表中选择启用宏的文件类型。继续另存为无宏文档?按钮:[是][否][帮助]
**
**
One idea is to change the document's SaveFormat to an older format in order to prevent this warning from popping up. But I'm not sure if this change will affect how the document behaves going forward and if it's even possible to modify this property within the DocumentBeforeSave event (the property is a READONLY property).
一个想法是将文档的 SaveFormat 更改为旧格式,以防止弹出此警告。但我不确定此更改是否会影响文档的行为方式,以及是否可以在 DocumentBeforeSave 事件中修改此属性(该属性是 READONLY 属性)。
Thanks in advance for any help on this topic.
在此先感谢您对本主题的任何帮助。
The following code will open a word (assuming c:\work\test.docx exist, which can be just a blank word doc). If you hit the Save button on the word doc, the warning message will show up. BTW, I'm using Office 2010.
以下代码将打开一个单词(假设 c:\work\test.docx 存在,它可以只是一个空白单词 doc)。如果您点击 word doc 上的“保存”按钮,则会显示警告消息。顺便说一句,我正在使用 Office 2010。
<TestMethod()>
Public Sub testWord()
Dim wApp As New Word.Application()
Dim myDoc As Word.Document
Dim DataCodeModule As Object = Nothing
myDoc = wApp.Documents.Open("C:\Work\test.docx")
DataCodeModule = myDoc.VBProject.VBComponents(0).CodeModule
With DataCodeModule
.InsertLines(1, "Option Explicit")
.InsertLines(2, "Sub TestCode()")
.InsertLines(3, "Selection.InsertAfter ""test""")
.InsertLines(4, "End Sub")
End With
wApp.Visible = True
myDoc.Activate()
End Sub
And once the DocumentBeforeSave is hooked up, I was hoping the following code would disable the warning. Maybe the DisplayAlerts need to be set as soon as the document is opened?
一旦 DocumentBeforeSave 连接起来,我希望下面的代码可以禁用警告。可能需要在打开文档后立即设置 DisplayAlerts?
Public Sub App_DocumentBeforeSave(ByVal doc As Object, ByRef saveAsUI As Boolean, ByRef cancel As Boolean) Handles _officeHelper.DocumentBeforeSave
Dim WordApp As Object = this.WordApp()
'WordApp.DisplayAlerts = False
WordApp.DisplayAlerts = 0
End Sub
回答by Siddharth Rout
Like this?
像这样?
Application.DisplayAlerts = wdAlertsNone
'~~> Your Save Code
Application.DisplayAlerts = wdAlertsAll
FOLLOWUP
跟进
You are doing it in vb.net. I don't have access to VB.net at the moment but this example below will set you on the right path
你是在 vb.net 中做的。我目前无法访问 VB.net,但下面的这个例子会让你走上正确的道路
Open Word and insert a module and then paste this code
打开 Word 并插入一个模块,然后粘贴此代码
Option Explicit
Dim MyClass As New Class1
Sub Sample()
Set MyClass.App = Word.Application
End Sub
Now insert a Class Module and paste this code
现在插入一个类模块并粘贴此代码
Public WithEvents App As Word.Application
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, _
SaveAsUI As Boolean, Cancel As Boolean)
Application.DisplayAlerts = wdAlertsNone
ActiveDocument.Save
Application.DisplayAlerts = wdAlertsAll
End Sub
Now if you press the save button, you will notice that you won't get that alert any more. :)
现在,如果您按下保存按钮,您会注意到您将不会再收到该警报。:)
Hope you can adapt it as required :)
希望您可以根据需要进行调整:)
回答by user3747146
I simply done this code . It will prompt you to save and also help you in attaching it to email. It worked for me. Please try. I gave a command button on top of the sheet. Thanks
我只是完成了这段代码。它会提示您保存并帮助您将其附加到电子邮件中。它对我有用。请尝试。我在工作表顶部给出了一个命令按钮。谢谢
Private Sub CommandButton1_Click()
Dim whatfolder, whatfile As String
whatfolder = InputBox("Type folder name.. Don't type which drive")
whatfile = InputBox("Type file name you want to give")
ChangeFileOpenDirectory "C:\" & whatfolder
Application.DisplayAlerts = wdAlertsNone
ActiveDocument.SaveAs FileName:=whatfile & ".docx", FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:=False
Application.DisplayAlerts = wdAlertsNone
ActiveDocument.SendMail
End Sub