1

Problem: Word sometimes doesn't choose "Single File Web Page" format automatically for .mht files.

Description: When opening files in Word application there is an option to select file conversion format:

Convert File Word UI prompt

Convert File Word UI prompt

For the .mht files to be correctly decoded/viewed i noticed that selecting format "Single File Web Page" works perfect.

Is it possible to achieve this programmatically? Lets say I would like to open the .mht file in word application, and use word's converter to treat it as a "Single File Web Page" file.

So far I have found that Documents.Open method (https://learn.microsoft.com/en-us/office/vba/api/word.documents.open) accepts parameter "Format". But it seem like it doesn't have the format I need. The closest I see is wdOpenFormatWebPages(7), but it is not the same as "Single File Web Page"

https://learn.microsoft.com/en-us/office/vba/api/word.wdopenformat

VB:

Documents.Open FileName:="C:\test.mht", format:=7

C#:

Application app = new Application();
Document document = app.Documents.Open(FileName: @"C:\test.mht", Format: 7);
Mar124715
  • 11
  • 3
  • 1
    Try recording a macro when opening the file manually and see what Word records. – Cindy Meister Dec 16 '19 at 19:45
  • Thanks for the idea. However, recording always shows format 'wdOpenFormatAuto', no matter what i choose in the prompt. Documents.Open FileName:="test.mht", ConfirmConversions:=True, ReadOnly:= _ False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:= _ "", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _ Format:=wdOpenFormatAuto, XMLTransform:="" – Mar124715 Dec 17 '19 at 06:41
  • Back at a "real machine"... I don't find an equivalent, either. (BTW that's C# code, not VBA.) You can look at the documentation for the `FileConverter` object - according to that, the list in the conversion prompt comes from those, although a quick inspection here only brings up the last four, I think the others are built-in so there's no separate converter. If you turn off the option is the *.mht file opened correctly? IOW does Word choose the Single Web Page option automatically (reflecting wdOpenFormatAuto)? It's not clear from the problem description in the question... – Cindy Meister Dec 18 '19 at 21:11
  • Yes, the problem is that Word sometimes doesn't choose "Single File Web Page" automatically. It sets 'Plain text' for some of the files and those are not being opened correctly. Therefore I had an idea to force it choose "Single File Web Page" format. – Mar124715 Dec 19 '19 at 07:02
  • Ah, understandable, then. The only other possibility that occurs to me is to use SendKeys with the conversion dialog box - but that's not really ideal! In your place, I might also look very closely at the first few lines of code in the mht files to see if there's a difference between those Word is recognizing and those it opens as plain text. There may be a tag or attribute that's missing in the latter that you could add before opening... – Cindy Meister Dec 19 '19 at 13:29

1 Answers1

0

Good day community, Microsoft support have already document this .mht issue and offer a simple solution. I use it to get quick 'highly detail QA and training document in Word format' from PSR.exe recording (also provide in all Windows terminal since Win7). I have a conversion script to do it in batch for my tester/trainer at my compagny. With the right title and the command 'dir /b', it also build a table of content to help research for newly employés :

Ref : https://support.microsoft.com/en-us/topic/the-confirmconversions-property-in-macro-changes-the-confirm-conversion-at-open-option-in-word-6f16c1db-4cb8-2727-dc4f-fdf6ef112ff5

Sub MyDocumentOpenMacro()

   Dim x As Integer

   ' Set x equal to the current setting of the Confirm conversion at Open
   ' option before opening your file.
   x = Application.Options.ConfirmConversions

   ' Open your file.
   Documents.Open "C:\My Documents\Address.txt", ConfirmConversions:=False

   ' Use a conditional statement to set the Confirm conversion at Open
   ' option back to its setting (value of x) before opening your file.
   If x = "0" Then
      Application.Options.ConfirmConversions = False
   Else
      Application.Options.ConfirmConversions = True
   End If

End Sub