Inserting a picture into a word document

I’m trying to insert a user-provided picture (might be .png or .jpeg or whatever) as a header into a word document. Christian’s WordMBS class provides two ways of doing this, but I can’t get either of them to work.

  1. Use WordFileMBS.addMediaFile(name as string, content as string)
    This would mean I’d have to mess with the internal xml files, which I’ve tried to do without much success.
  2. Insert a placeholder Image and replace its content via
    // load image
    Dim ImageFile As FolderItem = SpecialFolder.Desktop.Child(“new.jpg”)
    Dim ImageStream As BinaryStream = BinaryStream.Open(ImageFile)
    Dim ImageData As String = ImageStream.Read(ImageStream.Length)
    word.MediaFile(name) = ImageData

(code is straight from Monkeybread Xojo plugin - WordFileMBS methods)
This works, but the problem is that the inserted picture will always be the same dimensions as the placeholder picture. And my placeholder is a .png, not sure what happens if I try to replace it with a .jpeg. Anyway, as I can’t anticipate what dimensions the user provided picture will have I can’t seem to go this way either. Any ideas?

Well, you may just go and get the Word Plugin from @Björn_Eiríksson to add pictures.

Ah, hmm. Not sure I’m willing to spend 200 bucks on this. Will try to call a python script. Thanks!

String is unlikely to be able to hold Binary data from an image. Try using a memory block instead.

Please, when sharing code, use the icons to tell the forum software the selected text is code ad in:

Click in this icon:
image

// load image
Dim ImageFile As FolderItem = SpecialFolder.Desktop.Child("new.jpg")
Dim ImageStream As BinaryStream = BinaryStream.Open(ImageFile)
Dim ImageData As String = ImageStream.Read(ImageStream.Length)
word.MediaFile(name) = ImageData

Understand the difference ?

And while I am at it, when you got an answer, click in the CheckBox, so readers will know.

Yeah, I was wondering about that too, but as I said, I just copied the code from Christians documentation. Anyway, this doesn’t solve the fixed picture size problem I mentioned.

Where’d you get that idea? I have only ever used MemoryBlock when working with declares. You can treat String as a bag o’ bytes.

2 Likes

Often with other languages special codes like 0 cause massive problems with strings. Images are pretty likely to contain null bytes. I’m aware that Xojo string do not use null terminated strings.

With UTF-8 being a “default” encoding for strings you also, in theory, are producing bad encoding values. Relying on “implementation details” seems like bad practice.

1 Like

Nothing important, but my thoughts include

  1. String is a bag o’ bytes, this is what I was taught and why Text datatype was a thing at one point.
  2. String and MemoryBlock are interchangeable as far as I know, you can convert between them with an assignment (var s as String = mb || var mb as MemoryBlock = s)
  3. Picture.FromData and Picture.ToData return MemoryBlocks

So like, nothing useful to add, just some interesting bits. :slight_smile:

Text was dropped for a reason. String is taking over from what Text was. You only have to look at String iterators to see that “Characters” are now something in strings. I’ve nothing against strings but they seem to be conceptually a bad fit for binary data.

Characters are only a thing if you define the encoding. Otherwise, it’s just bytes. Text was an interesting idea, but they reverted back to String.

1 Like