Answered Creating Thumbnail for Word-document using 'EnhMetaFileBits'

Hi everybody,

Win8, 10, OE11.7 & 12.5

Found a small piece of code which I try to translate from C# to ABL.

C#:
var doc = new Microsoft.Office.Interop.Word.Application().Documents.Open(FileName: docxPath, Visible: false, ReadOnly: true);

byte[] bytes = doc.Range().EnhMetaFileBits;
Image page = Image.FromStream(new MemoryStream(bytes));
doc.Close(WdSaveOptions.wdDoNotSaveChanges);

I came up with this:
Code:
DEF VAR chWord        AS COM-HANDLE   NO-UNDO.
DEF VAR chDocument  AS COM-HANDLE   NO-UNDO.
DEF VAR bt             AS "System.Byte[]".

CREATE "Word.Application" chWord.
chDocument = chWord:Documents:OPEN("C:\Temp\Test.Docx",FALSE,,,,,,,,,,).

bt = CAST(chDocument:Range():EnhMetaFileBits, "System.Byte[]").

MESSAGE VALID-OBJECT(bt) VIEW-AS ALERT-BOX.

but, as the message shows, 'bt' is not valid.

Could anybody please point out what I'm doing wrong.

TIA, Wolf
 

Rob Fitzpatrick

ProgressTalk.com Sponsor
Code:
 // ABL Dojo
 OpenEdge V. 12.6.0
 Platform: Linux (x86_64) 
Invalid datatype specified: System.Byte[]. Specify a datatype such as 'character' or the name of a class. (5638)
** Main.p Could not understand line 3. (196)

The AVM doesn't understand "System.Byte[]" as a type or a class. If you want to read arbitrary data from an external source, you can use a memptr.

But that's just half of the problem. Then you need to know how to parse that data to produce some kind of image file. I had a look at the Microsoft docs for Microsoft.Office.Interop.Word.Application.EnhMetaFileBits and it didn't say much. I gathered that it's a byte array that can be transformed into an image in some image format. Perhaps a Windows metafile? I don't know.

Since this operation is foreign to the AVM and the C# code for doing it appears quite succinct, maybe you would be further ahead by creating an external executable to create the image, given some byte stream, and have your program shell out and run it. Or create a DLL to do the work and define it as an external procedure and call it from the ABL.
 
Thanks for your input, Rob !

It's not a real important thing for us, we do use some thumbnails, but if there are none, it's ok as well.

Just stumbled over this piece of code and thought it looks small and easy and I'll give it a try. OK, it's not so easy....

Wolf
 

Stefan

Well-Known Member
Code:
def var mp as memptr no-undo.

mp = chDocument:Range():EnhMetaFileBits.

copy-lob from mp to file 'c:\temp\preview.emf'.

Will create a file that can be opened by Windows, but it cannot be used on a Progress image widget since that does not support emf files and will throw error 2290.

You may be able to throw this onto some .Net control.
 
Top