Office plugin copy and paste picture

Hi,
I try to open a word document in a desktop app by office plugin. All it is ok. I copy an inlineshape and try to paste in a canvas. The PictureAvailable is True but the canvas stay empty. Here the code:

Var NomeFile As String
Var f As FolderItem

NomeFile = “c:\tmp\example.doc”
f = New FolderItem(NomeFile)

Dim Word As New WordApplication
Dim doc As WordDocument

Word.Documents.Open(f.NativePath)
doc=Word.ActiveDocument

Var i As Integer
Var sh As WordInlineShape

Var cb As New Clipboard
For i=1 To doc.InlineShapes.Count
sh = doc.InlineShapes(i)
sh.Range.CopyAsPicture

If cb.PictureAvailable Then
Canvas1.Backdrop=cb.Picture
End If

Exit For
Next i
doc.Close False

If I try to paste in mspaint I have the image of a shape.
If I do copy of an image from mspaint and paste in canvas with the same code:

Var cb As New Clipboard
If cb.PictureAvailable Then
Canvas1.Backdrop=cb.Picture
End If

I have the image correctly.
Can you help me to understand where I doing wrong?
Thank you
Oscar

Hello Oscar,

This looks like its an old bug with Windows and the clipboard. You are correct, the picture from Microsoft Word is on the clipboard, and can be used by other programs.

Xojo doesn’t seem to see a picture although one clearly exists. I used code at Clipboard Documentation

…And there was no picture in the ClipPic picture:
XojoClipboardBug

…And the clipboard printed ‘This code has run’ even though there is no picture in the clipboard:
Clipboard-NoPicture

I am not sure what to tell you. Maybe someone from Xojo can shed some light on this?

Maybe the picture is a vector image, not a bitmap?

Hi Eugene,
I see an old post with an issue similar, I think like you.
I hope someone from Xojo can help me.
Thank you
Oscar

Hi Jeff,
I think the image is a bitmap, I prepare the word document with copy and paste from mspaint.
If I copy from Xojo and paste in mspaint it is ok.
If I copy in mspaint and paste in Xojo it is ok.
If I copy from Xojo and paste in Xojo it is not work.

Thank you,
Oscar

In fact, you Copy from the Office Plug-in.

Check what you copy…

How you copy something into cb in your code ?


Var cb As New Clipboard
For i=1 To doc.InlineShapes.Count
sh = doc.InlineShapes(i)
sh.Range.CopyAsPicture

If cb.PictureAvailable Then
Canvas1.Backdrop=cb.Picture
End If

The code above may work only once (the New Clipboard declaration is poutside of the Loop)… explorate with the debugger how it really behave.

Hi Emile,
sorry, yes in Xojo I copy by the office Plug-in with sh.Range.CopyAsPicture.
But if I paste in mspaint after this instruction I see the image correctly.
By the debugger I see always picture like nil exactly like Eugene show.

Thank you
Oscar

I can explain why Copy / Paste wroeks here and not there. But It will takes me time and I needed to iuse Google Translate…

To make the long story short, what you copy is not in a Xojo known format.

Search an application that returns the Clipboard contents (in Windows) as types… and you will start to have a clue.

Think: using Firefox, copy an image; it may or may not be pasted somewhere but can always be pasted elsewhere ! Once I checked what was in the Clipboard, I understand the trouble. I all cases, it was an image, but the trouble was the kind of image.

So, in the debugger, check the Clipboard contents once you copied the object from the Office document: if it is a Picture, you can see it in the debugger (click in Contents): if not, you get your answer.

PictureAvailable returns True because there is a Picture; apparently, that Picture cannot be displayed in Xojo.

BTW: What are the Xojo and Windows (+ Office) versions you’re using ? You may fall into an old and corrected bug…

Hi Emile,
now I check what kind of object is in the clipboard.
I use Xojo 2022R2 and relative office plugin.

Thank you
Oscar

Hi,
I use InsideClipboard, and this the mspaint copy content
Schermata 2022-08-09 alle 11.52.43
and this is the content of the Xojo office plugin content
Schermata 2022-08-09 alle 11.53.59
I think I need to select the correct object.
Have you an idea about it to suggest?

Thank you
Oscar

This page may help you understanding Clipboard on Windows:

It appears in english (Google), but French in a tab.

I see the entry you just send, I think you have to use the PNGH entry, but how… I will look at the Clipboard entry in the Xojo Documentation.

You now “know” why it does not works: the default type is wrong and so does not appear at “paste” time.

I was right. Look there:

https://documentation.xojo.com/api/user_interface/clipboard.html#clipboard
and search the RAW entry, they talk about Windows.

And here you have an enum:

You may also want to read:
https://documentation.xojo.com/topics/office_automation/controlling_microsoft_office_from_your_app.html

I hope this will help you.

Finally I found a solution:

Var sh As WordInlineShape
Var cb As New Clipboard
Var mb As MemoryBlock

For i=1 To doc.InlineShapes.Count
sh = doc.InlineShapes(i)
SH.Range.CopyAsPicture
mb=cb.RawData(“PNG”)
Canvas1.Backdrop =Picture.FromData(mb)

Next i

Thanks to all,
Oscar

1 Like