locked
Convert Word document in HTML via Excel RRS feed

  • Question

  • I have a problem, maybe someone can help me.
    From Excel, through VBA I want open a Word document (docx) and convert and save to HTML.
    I tried a method but has given a disappointing result. I attach.
    I recorded a macro in Word and I pasted in Excel.



    Public objWord As Object

    Sub deschidWord()
    Dim texta(10) As String
    Dim ws As Worksheet

            Set objWord = CreateObject("Word.Application")
            objWord.Visible = True
            loca = d.Cells(1, 1) ' my word file name
            objWord.Documents.Open loca


        objWord.ActiveDocument.SaveAs2 Filename:="D:\proba.htm", FileFormat:= _
            wdFormatFilteredHTML, LockComments:=False, Password:="", AddToRecentFiles _
            :=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
            :=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
            SaveAsAOCELetter:=False, CompatibilityMode:=0

    End Sub

    Sub inchidWord()
            objWord.ActiveDocument.Close
          objWord.Quit
    End Sub


    A better idea?
    Wednesday, June 11, 2014 2:55 PM

Answers

  • Hi csaba114,

    I just tested according to your description. I think it's the problem with the FileFormat. You need to add reference to the Word Object Library into the Excel document. VBA Editor>>Tools>>Reference>>Check "Microsoft Word 15.0 Object Library" (I'm using Win8.1 X64 with Office 2013).

    After you add reference to this library, you'll get the correct file.

    This is my code:

    Public Sub test()
        Dim objWord As Object
    
        Set objWord = CreateObject("Word.Application")
        objWord.Visible = True
        objWord.Documents.Open "D:\OfficeDev\Test\SaveAsHtml.docx"
    
        objWord.ActiveDocument.SaveAs2 Filename:="D:\OfficeDev\Test\SaveAsHtml.htm", FileFormat:= _
            wdFormatFilteredHTML, LockComments:=False, Password:="", AddToRecentFiles _
            :=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
            :=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
            SaveAsAOCELetter:=False, CompatibilityMode:=0
        
        objWord.ActiveDocument.Close
        objWord.Quit
    End Sub


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, June 16, 2014 8:07 AM

All replies

  • Could you please be more specific? Did you try to debug the code?
    Wednesday, June 11, 2014 3:00 PM
  • The code works, but the generated HTML file is not HTML, is malformed.

    Is something like

    "PK!A¨.fT[Content_Types].xml ¢( ´”Ënƒ0E÷•úÈÛ œtQUUH},ÛHM?À±âÖ/ÙÎëï;@‚ª(©I6H0sï=xF“VÉ |Öäd˜ H†[!M™“¯Ù[úH’™LY9ÙB “ñíÍh¶uT›“EŒî‰ÒÀ YȬƒ•ÂzÍ"Þú’:ÆX ô~0x Üš&¦±ò ãÑ l©bòºÁÇ É·ƒ’$ÏMc••©+ƒº@j<¨p aÎ)ÉYÄ:]q@–î¨2TÖ=a!]¸Ã†Ž„ªÒ°Ó}àëôR@2e>¾3]tm½ Âò¥FevÚæ§- É¡ÕWnÎ[!àwÒ*k+šI³çïäq« \ž¢ñ폇Qp €s/ÂæŸW£øcÞ R`îŒÍ\£µî…ˆxj¡¹Ïæ¨mNEbçÔ[p øŒ½?²•:Łø(Oÿum"ZŸ=TÛ@€8’Më8þÿÿPK!‘·ïN _rels/.rels ¢( ¬’ÁjÃ0 @ïƒýƒÑ½QÚÁ£N/cÐÛÙ[ILÛØj×þý<ØØ]éaGËÒÓ“ÐzsœFuà”]ð–U Š½ Öù^Ã[û¼x•…¼¥1xÖpâ ›æöfýÊ#I)ʃ‹YŠÏ‘øˆ˜ÍÀå*Döå§ i")ÏÔc$³£žqU×÷˜~3 ™1ÕÖjH[{ª=E¾†ºÎ~ f?±—3-ÂÞ²]ÄTꓸ2j)õ,l0/%œ‘b¬ ð¼Ñêz£¿§Å‰…, ¡ ‰/û|f\ZþçŠæ?6ï!Y´_áoœ]AóÿÿPK!ý°ä/Xword/_rels/document.xml.rels ¢( ¬”MNÃ0F÷HÜÁòž8)PjÒ uÁÂÜd’˜úOö@šÛcZJRQ"YÎgÍ›g[öb¹U’|€óÂè”&QL è”B×)}Í/n)ñÈuɥѐÒ<]fçg‹gC“o„õ$P´OiƒhïóEŠûÈXÐa¥2Nq ¥«™åņ×Àfq<gnè ù“¬ê”ºuyiiþyøût•(àþï 4žÁzx¿�bØœxîjÀ”Â();-r3¥†^èvå="">LÆfS:ø_'qHÆ’I°“0ØÕcãçSŽ¯ŒÆœ¯åà&~¢1‰ë)%š@rRèM/ñmÛ6RΛ #g«O¦ ƒ¶Nó? ¯¦4*¼ØÞNA)ø>L¢7 õ—;ú²OÿÿPK!ÎÛ45 8Šword/document.xmlì]msÚ:þ¾3û4ììÌÝ™`üŠ^r›ÒönÛd éÝoa Qc,¯l祷?¥ŸöwlïÿZI`ÇŒ$¤ng0–¥££ç<::Çί¿ÝMpca·]Q$¹l×ÂCäŽÛ•Ë‹Ój£üºCè`×nWîm¿òÛá_ÿòëmkˆ­pb» "\¿uëYíÊUx­ZÍ·®ì ô¥ ²öñ(,<©áÑYví“aM•™ó¶lߧýu {ýŠ7IKÞíÒ‹#L&0 §d\›@rzU*݃ ÷T</gnè ù“¬ê”ºuyiiþyøût•(àþï>

    ......

    Wednesday, June 11, 2014 3:14 PM
  • Why do you need to use the filtered HTML format? Did you try to use the wdFormatHTML value instead?

    Here is what MSDN states for the FileFormat parameter of the SaveAs method:

    The format in which the document is saved. Can be any WdSaveFormat constant. To save a document in another format, specify the appropriate value for the SaveFormat property of the FileConverter object.

    Wednesday, June 11, 2014 5:51 PM
  • I tried converting both HTML and filtered HTML format. Same wrong result.
    But boths works as macro in Word.

    The problem is with the insertion into Excel macro.

    Friday, June 13, 2014 2:03 PM
  • Hi csaba114,

    I just tested according to your description. I think it's the problem with the FileFormat. You need to add reference to the Word Object Library into the Excel document. VBA Editor>>Tools>>Reference>>Check "Microsoft Word 15.0 Object Library" (I'm using Win8.1 X64 with Office 2013).

    After you add reference to this library, you'll get the correct file.

    This is my code:

    Public Sub test()
        Dim objWord As Object
    
        Set objWord = CreateObject("Word.Application")
        objWord.Visible = True
        objWord.Documents.Open "D:\OfficeDev\Test\SaveAsHtml.docx"
    
        objWord.ActiveDocument.SaveAs2 Filename:="D:\OfficeDev\Test\SaveAsHtml.htm", FileFormat:= _
            wdFormatFilteredHTML, LockComments:=False, Password:="", AddToRecentFiles _
            :=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
            :=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
            SaveAsAOCELetter:=False, CompatibilityMode:=0
        
        objWord.ActiveDocument.Close
        objWord.Quit
    End Sub


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, June 16, 2014 8:07 AM
  • Thanks! It's works fine!
    Friday, June 27, 2014 3:34 PM