How to disable the MS Word "macro-free document" warning with code?
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
Open Word and insert a module and then paste this code
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 :)
Admin
Updated on June 04, 2022Comments
-
Admin almost 2 years
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.
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?
**
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]
**
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).
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.
<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?
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