Hello all,
Thanks for Tony's comment, as to why the EnhMetaFileBits property only
reflects but not equals to the image file size, I think I can give some
explanation. From the MSDN document
http://msdn.microsoft.com/en-us/library/aa172538.aspx, the EnhMetaFileBits
returns a variant that represents a picture representation of how a
selection or range of text appears. Thus, I think this property just
capture a picture of the range. It may be also used internally for
Range.CopyAsPicture() method. But, we cannot know which algorithm Word
adopts to generate the picture for us. It may omit some information to
compress it already.
To get a more accurate image size in Word, I think the approach is still
converting the document format to another type. The xml should be better
than html. The corresponding content of image in Office-2003-XML-format
Word document looks like:
<w:binData w:name="wordml://02000001.jpg">......(binary code of the image)
</w:binData>
Consequently, I use the following codes to get all image files' size:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.DocumentOpen += new
Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentOpenEventHandler(Ap
plication_DocumentOpen);
}
void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document Doc)
{
object visible = false;
Word.Document newDoc = this.Application.Documents.Add(ref missing, ref
missing, ref missing, ref visible);
newDoc.Content.FormattedText = Doc.Content.FormattedText;
object fileName = @"D:\temp.xml";
object fileFormat = Word.WdSaveFormat.wdFormatXML;
newDoc.SaveAs(ref fileName, ref fileFormat, ref missing, ref missing,
ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref
missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
newDoc.Close(ref missing, ref missing, ref missing);
String xmlContent = System.IO.File.ReadAllText(@"D:\temp.xml");
int startIndex = 1;
while (true)
{
int start = xmlContent.IndexOf(@"<w:binData w:name=" + '"' +
"wordml://", startIndex);
if (start == -1)
{
break;
}
else
{
startIndex = start;
int end = xmlContent.IndexOf(@"</w:binData>", startIndex);
startIndex = end;
MessageBox.Show("File size is about :" + ((end - start - 41) *
0.73).ToString() + " bytes");
}
}
}
I have tried it on my side. It works fine. Every time a document is opened,
the code will create an invisible document where we paste the opened
document's content. Then it saves the invisible document as xml format to
the disk. With searching in the saved xml content, we can find each
<w:binData></w:binData> mark and get the length of the binary content.
Of course, this approach is slower than the EnhMetaFileBits. So it depends
on your scenario which one to choose. Hope this helps. If you have any
future questions, please feel free to let me know.
Best regards,
Ji Zhou (
[email protected], remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
This posting is provided "AS IS" with no warranties, and confers no rights.