How do I keep the original Word document open while saving a copy in HTML?

The problem I am facing is when I run my macro to save the current Word document as HTML type, the document remains open, but not in its original .docx format, it is in .htm format.

If I needed to edit the document after running the macro, it won't stay in its original .docx format later.

I would appreciate some feedback on how to stay in the original format when also saving a copy in a different format. Thank you.

Here is my docx code for html in VBA

Sub DocToHTML()

    Dim slice As String
    Dim strDocName As String
    Dim PathOrg As String

    On Error Resume Next

    strDocName = ActiveDocument.Name
    slice = Left(strDocName, InStrRev(strDocName, ".") - 1)
    strDocName = ActiveDocument.Path + "\" + slice
    ActiveDocument.SaveAs2 FileName:=strDocName, FileFormat:=wdFormatHTML

End Sub

      

+3


source to share


1 answer


Before writing the code to do something like this stop and think about how you would do it in a UI without code. Any code you write will simply automate this process.

So what would you do in the user interface?

  • Save the original document to keep the changes you made.
  • Save a copy as html.
  • Reopen the original document.
  • Possibly close html version.


Thus, your code can be rewritten as follows:

Sub DocToHTML()

    Dim origName As String
    Dim saveName As String
    Dim docHTML As Document

    If Not ActiveDocument.Saved Then ActiveDocument.Save
    origName = ActiveDocument.FullName
    saveName = Left(origName, InStrRev(origName, ".") - 1)
    ActiveDocument.SaveAs2 FileName:=saveName, FileFormat:=wdFormatHTML

    Set docHTML = ActiveDocument
    Documents.Open origName
    docHTML.Close wdDoNotSaveChanges

End Sub

      

+2


source







All Articles