Home > Back-end >  How do I save a docx as html?
How do I save a docx as html?

Time:01-06

I want to save a docx (Word 2007) as a html file with the accompanying files in a subfolder.

To see how to do that, I have just done that in Word 2007 and recorded a macro.

It recorded everything except the saving:

Sub Makro3()

    With ActiveDocument.WebOptions
        .RelyOnCSS = True
        .OptimizeForBrowser = True
        .OrganizeInFolder = True
        .UseLongFileNames = True
        .RelyOnVML = False
        .AllowPNG = True
        .ScreenSize = msoScreenSize800x600
        .PixelsPerInch = 96
        .Encoding = 65001
    End With
    With Application.DefaultWebOptions
        .UpdateLinksOnSave = True
        .CheckIfOfficeIsHTMLEditor = False
        .CheckIfWordIsDefaultHTMLEditor = False
        .AlwaysSaveInDefaultEncoding = False
        .SaveNewWebPagesAsWebArchives = True
    End With
End Sub

As I want to do the same in VB6, I re-wrote the code replacing the enum as it was unavailable in VB6 and added a SaveAs line.

However, this saved the docx obviously as a docx again, just with a different extension (.html).

What am I doing wrong?

Public Sub pCreateHtml(ByVal uPath As String)

    Dim oWord As New Word.Application
    Set oWord = New Word.Application

    Dim oDoc As Word.Document
    Set oDoc = oWord.Documents.Open(uPath, True, True)
    
    With oDoc.WebOptions
        .RelyOnCSS = True
        .OptimizeForBrowser = True
        .OrganizeInFolder = True
        .UseLongFileNames = True
        .RelyOnVML = False
        .AllowPNG = True
        .ScreenSize = 3 'msoScreenSize800x600
        .PixelsPerInch = 96
        .encoding = 65001
    End With
    With oDoc.Application.DefaultWebOptions
        .UpdateLinksOnSave = True
        .CheckIfOfficeIsHTMLEditor = False
        .CheckIfWordIsDefaultHTMLEditor = False
        .AlwaysSaveInDefaultEncoding = False
        .SaveNewWebPagesAsWebArchives = True
    End With
    
    oDoc.SaveAs Replace(uPath, ".docx", ".html")
    
    oDoc.Saved = True
    oDoc.Close
    
    oWord.Quit
    Set oWord = Nothing
    
End Sub

CodePudding user response:

oDoc.SaveAs2 FileName:=Replace(uPath, ".docx", ".html"), FileFormat:=wdFormatHTML

//value of wdFormatHTML is 8
  • Related