HTMLProjectItem.LoadFromFile on a Word document causes mht save fo

S

ssissa

Hi,

It seems that when I call
Document.HTMLProject.HTMLProjectItems.LoadFromFile() on a Word document
using Word 2003, the default save format of the file is changed to mht.

I have a COM add-in with the following basic workflow:
1. Extract an HTML representation of an open Word document using
document.HTMLProject.HTMLProjectItems.Text
2. Manipulate the HTML to make some desired changes to the document
3. Apply the changes to the document using
Document.HTMLProject.HTMLProjectItems.LoadFromFile()

Following this, when the user requests a Save or Save As on the document,
Word proposes MHT as the file type by default. I want that Word proposes DOC
by default. Without the calls to LoadFromFile(), Word does propose DOC. How
can I get the default save format for the document back to DOC?

Thanks,
Bret
 
J

Jialiang Ge [MSFT]

Hello Bret,

According to the description, you want to reset the Word's default save
format to "Doc" after it is changed to "Mht" by the call of
"Document.HTMLProject.HTMLProjectItems.LoadFromFile()". If I'm off base,
please feel free to let me know.

To reset the default save format, we can call
Application.DefaultSaveFormat = "DOC"
after the call of LoadFromFile()

For more information DefaultSaveFormat property, see:
http://msdn2.microsoft.com/en-us/library/microsoft.office.interop.word._appl
ication.defaultsaveformat(VS.80).aspx

Please have a try and let me know the result. If you have any other
concerns or questions, feel free to let me know.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

ssissa

Hi Jialiang,

Thanks for your suggestion, however it does not help. If I set
Application.DefaultSaveFormat = "DOC" after
Document.HTMLProject.HTMLProjectItems.LoadFromFile(), then ask Word to
Save the document, it still proposes MHT. Moreover, if I check the value of
Application.DefaultSaveFormat after LoadFromFile(), it is already "DOC". I
don't think the problem is the default save format of Word, it is something
specific to the manipulated document which causes Word to proposes MHT for
that specific document.

Thanks,
Bret
 
J

Jialiang Ge [MSFT]

Hello Bret,

I am sorry for misleading. Application.DefaultFileFormat does not work in
my side either.

However, please try the following workaround:

1. register the Application.DocumentBeforeSave event with code:
app.DocumentBeforeSave += new
Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeSaveEventHand
ler(app_DocumentBeforeSave);
2. In the event handler:
static void
app_DocumentBeforeSave(Microsoft.Office.Interop.Word.Document Doc, ref bool
SaveAsUI, ref bool Cancel)
{
if (SaveAsUI)
{
Microsoft.Office.Core.FileDialog fileSaveDlg =
Doc.Application.get_FileDialog(MsoFileDialogType.msoFileDialogSaveAs);
fileSaveDlg.InitialFileName = "Document1.doc";
if (fileSaveDlg.Show() == -1)
{
fileSaveDlg.Execute();

SaveAsUI = false;
}
Cancel = true;
}
}

I have carefully tested the workaround in my side. Please have a try and
let me know the result.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jialiang Ge [MSFT]

Hello Bret,

Would you mind letting me know the result of the suggestions? If you need
further assistance, feel free to let me know. I will be more than happy to
be of assistance.

Have a great day!

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

ssissa

Hi Jialiang,

I am hesitant to take your suggestion.

First, it seems odd to interrupt the Application.DocumentBeforeSave event
for this purpose, especially when the user is not actively using my plugin.
For example, this means I should keep track of which documents I have
modified so as to not make the change when the uesr is saving some other
document.

Second, there seems to be other changed behaviors that come along with Word
recognizing the document as MHT, such as addition of web-page functions to
the toolbar. Your solution does not address these issues. I would really
prefer a solution that tells Word that the document is DOC not an MHT,
immediatly after the modifications, and not when the only when the user tries
to to save the document.

Thanks,
Bret
 
J

Jialiang Ge [MSFT]

Hello Bret,

Firstly, please allow me to explain the logic of Word's save action when
users start to save a Word document:

When users click on "Save" or Ctrl+S, [action No.1]: the first action of
Word is to detect if it is a new document. If it is a document that has
never been saved, -> [action No.2]: Word will determine the best file
format for it. Word creates a score for each file type: mht, doc, rtf,
html, etc, and choose the format with the highest score, namely the fittest
format, to be the default save format shown in the "Save As" dialog. If
multiple file formats have the same high score, Word will take the
DefaultSaveFormat setting (Application.DefaultSaveFormat) into
consideration. -> [action No.3]: Word triggers the DocumentBeforeSave event
as is shown in my last reply, and indicate if a SaveAs dialog needs to be
shown (the SaveAsUI parameter). -> [action No.4]: If SaveAsUI is true, Word
will show the SaveAs dialog and initiate it with the best file format
determined in action No.2.

In our case, Word finds that mht is the best file format. The score of mht
is higher than that of doc, so Application.DefaultSaveFormat = "doc" does
not take effect. In order to reset the save format, we need to intervene in
the action No.3, since it is the only interface Word exposes to us.

Word decides the save format only when it needs to be saved. I do not find
a way to force word's save format immediately after the modification. A
better workaround may be to set a flag (e.g Doc.Variables.Add
Name:="DocIsLoadedfromHtmlByOurAddin", Value:= true) to indicate that this
document is loaded from a html by our add-in. Then in the
DocumentBeforeSave event, we check the flag. If it is true, we take our
action, otherwise, let Word take its default action.

I am sorry that I do not understand "there seems to be other changed
behaviors that come along with Word recognizing the document as MHT, such
as addition of web-page functions to the toolbar." Would you send a sample
loaded html to my mailbox (e-mail address removed)? I will check the "changed
behavior" and follow up you as soon as possible.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jialiang Ge [MSFT]

Hello Bret,

I am writing to see if there is anything else I can do for you in this
issue. Would you mind letting me know the result of the suggestions? In
addition, I have not received the sample of "there seems to be other
changed behaviors that come along with Word recognizing the document as
MHT, such as addition of web-page functions to the toolbar" yet. My mailbox
is "(e-mail address removed)". If you have any other concern or need anything
else, please feel free to let me know.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

ssissa

Hi Jialiang,

Find below simple .NET code to reproduce the problem. To test it, I open
Word manually, leaving the default new document open. I then run this code as
a console program. After running the program, the "New" button on the
"Standard" toolbar changes it's icon and becomes "New Web Page" and when
saving the document, MHT is the suggested format.

Application application =
(Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
object ONE = 1;
Document document = application.Documents.get_Item(ref ONE);
string[] items = new string[document.HTMLProject.HTMLProjectItems.Count];
for(int i = 1; i <= document.HTMLProject.HTMLProjectItems.Count; i++)
{
object I = i;
items[i-1] = document.HTMLProject.HTMLProjectItems.Item(ref I).Text;
}
for(int i = 1; i <= items.Length; i++)
{
string tmpFile = System.IO.Path.GetTempFileName();
System.IO.FileStream fs = System.IO.File.OpenWrite(tmpFile);
byte[] data = System.Text.Encoding.UTF8.GetBytes(items[i-1]);
fs.Write(data, 0, data.Length);
fs.Close();
object I = i;
document.HTMLProject.HTMLProjectItems.Item(ref I).LoadFromFile(tmpFile);
System.IO.File.Delete(tmpFile);

}
document.HTMLProject.RefreshDocument(true);
 
J

Jialiang Ge [MSFT]

Hello Bret,

I have tested the sample code and see what you meant by "the addition of
web-page functions to the toolbar". This appears to be a "by design"
behavior of HtmlProject. Word switches from the normal "doc mode" to this
"html mode" also when we save a *.doc document as a web page (menu:
File->Save As Webpage). I am consulting the Word product team for more
information, and will get back to you as soon as possible. Thanks for your
patience.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jialiang Ge [MSFT]

Hello,

According to the response of Word product team, it is a "by design"
behavior of HtmlProject. However, Word object model does not expose any
interface allowing us to interfere. I am sorry for the inconveniences.
Please feel free to submit your feedback at:
http://connect.microsoft.com/Main/content/content.aspx?ContentID=2220,

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top