简体   繁体   English

如何使用代码禁用MS Word“无宏文档”警告?

[英]How to disable the MS Word “macro-free document” warning with code?

I have an application that dynamically insert VBA code to help building a MS word document with proper bookmarks. 我有一个应用程序,动态插入VBA代码,以帮助构建具有适当书签的MS Word文档。 The VBA code doesn't need to be saved with the document itself. 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. 以下内容无法保存在无宏文档中:VBA项目要使用这些功能保存文件,请单击“否”返回“另存为”对话框,然后在“文件类型”下拉列表中选择启用宏的文件类型。 Continue saving as a macro-free document? 继续保存为无宏文档? buttons: [Yes][No][Help] 按钮:[是] [否] [帮助]

** **

One idea is to change the document's SaveFormat to an older format in order to prevent this warning from popping up. 一个想法是将文档的SaveFormat更改为旧格式,以防止弹出此警告。 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). 但我不确定此更改是否会影响文档的行为,以及是否甚至可以在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). 下面的代码将打开一个单词(假设存在c:\\ work \\ test.docx,这可能只是一个空白单词doc)。 If you hit the Save button on the word doc, the warning message will show up. 如果单击文档doc上的“保存”按钮,将显示警告消息。 BTW, I'm using Office 2010. 顺便说一下,我正在使用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. 一旦DocumentBeforeSave被连接起来,我希望以下代码将禁用该警告。 Maybe the DisplayAlerts need to be set as soon as the document is opened? 也许在文档打开后需要设置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

Like this? 像这样?

Application.DisplayAlerts = wdAlertsNone
'~~> Your Save Code
Application.DisplayAlerts = wdAlertsAll

FOLLOWUP 跟进

You are doing it in vb.net. 你是在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,但下面的示例将为您设置正确的路径

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 :) 希望你能根据需要调整它:)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM