Home > Blockchain >  Does DisplayAlerts work in Word and PowerPoint when using automation?
Does DisplayAlerts work in Word and PowerPoint when using automation?

Time:09-14

I have an app that processes Word, PowerPoint and Excel documents. After processing each document, I call the SaveAs method (or SaveAs2). For word, I set the DisplayAlerts to wdAlertsAll. I never see the dialog asking if I want to overwrite the document. I added code to get the alsert level right before calling SaveAs2 and it is wdAlertsAll. But, I see no message box. If I run Word and open the same file and do SaveAs to the same location as my app, the dialog appears.

I have the same issue with PowerPoint where I set the level to ppAlertsAll.

I do the same thing for Excel and ... it works just fine. I am prompted before doing the SaveAs.

In all three cases my app starts the Office apps and make sure they are visible on the desktop. I was thinking that perhaps running in the background was an issue but apparently not.

CodePudding user response:

There's some MS documentation about DisplayAlerts in PowerPoint here: https://docs.microsoft.com/en-us/office/vba/api/powerpoint.application.displayalerts?f1url=?appId=Dev11IDEF1&l=en-US&k=k(vbapp10.chm502050);k(TargetFrameworkMoniker-Office.Version=v16)&rd=true

The writer should be complimented for his/her vivid imagination. It's all a fantasy, probably copy/pasted from the Excel docs. DisplayAlerts is part of the PPT object model but it's never worked.

For example, try this in VBA:

Sub BeAlert()
    Application.DisplayAlerts = ppAlertsNone
    Debug.Print MsgBox("Testing", vbAbortRetryIgnore, "ArfArf")
    Application.DisplayAlerts = ppAlertsAll
    Debug.Print MsgBox("Testing", vbAbortRetryIgnore, "ArfArf")
End Sub

Both MsgBoxes appear, though the documentation suggests that only the second one will.

I'm not sure there'd be any difference between behavior in C# vs VBA, but in the latter, overwriting a file doesn't produce a message. It simply overwrites, no questions asked.

If you close a file that hasn't been saved since it was last changed, you may get a message; if you want to avoid that, set the presentation's .Saved property to True.

CodePudding user response:

If you deal with open XML documents it makes sense to process documents without MS Office involved, see Welcome to the Open XML SDK 2.5 for Office for more information.

Otherwise, you can use the Application.DisplayAlerts in PowerPoint. The value of the DisplayAlerts property is not reset once a macro stops running; it is maintained throughout a session. It's not stored across sessions, so when PowerPoint begins, it is reset to ppAlertsNone.

Sub SetAlert
    Application.DisplayAlerts = ppAlertsNone
End Sub
  • Related