VBA WORD: Problem with DocumentBeforeSave in macro to save .docx also in .htm

Lauro Colasanti 121 Reputation points
2023-04-28T11:41:49.3733333+00:00

Hi,

I need to save always a word document also in HTM format beside the normal DOCX.

I have difficulties in interceping all commands the user can use to save/close a document. The Major problem is intercepting the X Close Window application in the right upper corner.

I was trying to use DocumentBeforeSave event:

  • class Module EventClassModule:
Public WithEvents appWord As Word.Application

Private Sub appWord_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
   If Len(ActiveDocument.path) > 0 Then
         MsgBox "I got you", vbOK
         SalvaActiveDocumentDocxAndHTM
         Cancel = True
   Else
      Cancel = False
   End If
End Sub

  • Module1:
Public Sub SalvaActiveDocumentDocxAndHTM()

Dim sNomeFile As String, sPath As String
Dim vTipo As WdViewType
Dim ObjWordDoc As Word.Document, objWordActDoc As Word.Document

      Set objWordActDoc = Application.ActiveDocument
      With objWordActDoc
         .Save
            With Application
               .ScreenUpdating = False
               vTipo = .ActiveWindow.View.Type
            End With
            sPath = ActiveDocument.path
            sNomeFile = sPath + Application.PathSeparator + RemoveExtensionFromFileName(.Name)
            .SaveAs2 fileName:=sNomeFile + ".htm", FileFormat:=wdFormatHTML, AddToRecentFiles:=False
            'open again file docx
           Set ObjWordDoc = Documents.Open(sNomeFile & ".docx")
           .Close wdDoNotSaveChanges
            With Application
               .ActiveWindow.View.Type = vTipo
               .ScreenUpdating = True
            End With
      End With
      Set ObjWordDoc = Nothing
      Set objWordActDoc = Nothing

End Sub

The problem is avoiding the infinite loop of saving. I tried with a boolean inserted before and after the call of SalvaActiveDocumentDocxAndHtm but without success.

Do you have any suggestions?

Thanks, Lauro


Microsoft 365 and Office | Development | Other
Microsoft 365 and Office | Word | For business | Windows
Developer technologies | VB
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. John Korchok 6,416 Reputation points Volunteer Moderator
    2023-04-28T15:28:16.9366667+00:00

    You haven't actually stated what problem is caused by your existing code. Any error messages? A debug button that highlights a particular line? What connection does any of this have to the button that closes the window?

    Usually, to create an alternate to the standard Save command, you would create a macro that uses the name of a built-in command. In your case, that would be FileSave. Here is Microsoft's page on how to modify the built-in commands. Modifying a Word Command

    1 person found this answer helpful.
    0 comments No comments

  2. John Korchok 6,416 Reputation points Volunteer Moderator
    2023-04-30T00:31:26.53+00:00

    The FileSave dialog should return -2 if the Close button is clicked. Then you can provide a message to the user.

    Here's a page on working with built-in dialogs that are uncooperative: Returning a value after .Show-ing a built-in Word dialog box

    Please add a comment to reply, instead of posting an answer.

    1 person found this answer helpful.
    0 comments No comments

  3. Lauro Colasanti 121 Reputation points
    2023-04-28T16:04:43.1766667+00:00

    Thanks for your answer!

    The error arises in routine ActiveDocumentDocxAndHtm at

    .SaveAs2 fileName:=sNomeFile + ".htm", FileFormat:=wdFormatHTML, AddToRecentFiles:=False
    

    The files are correctly saved in the two formats, but then the ActiveDocumentDocxAndHtm is called again (WHY?) [the MsgBox is fired twice ] and Word cannot save the open file (but it should be closed already!?) with the same name.

    I can repourposing the FileSave command, but I'm unable to do the same if user click the Close X Window and ask if a save of the closing file is wanted.

    P.S.: I posted this also in Stackoverflow just because my browser seemed unable to publish the post. Sorry

    0 comments No comments

  4. Lauro Colasanti 121 Reputation points
    2023-05-01T08:06:06.5666667+00:00

    Hi John, thanks for the interest.

    I'm not repurpousing the FileSave command, so I'm not calling the FileSave Dlg, and neither seeing what value it is reporting.

    I was trying to tell to the Word Application to do something every time before saving a document.

    The problem arises from the fact that in the something I ask to do is a saving operation, falling in a recursive loop. I need a way to escape from that. In my

    Private Sub appWord_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
    

    I need to tell Word to keep doing the normal saving (without calling my double saving SalvaActiveDocumentDocxAndHTM ) in the following circumstances:

    1. If the Word document is not based on myTemplate
    2. If the Word document was not saved before
    3. If the Save is called from inside my SalvaActiveDocumentDocxAndHTM() routine

    While the first two are easy to accomplish the last one is giving me hard times

    Thanks, again

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.