Frage

I'm working on Word 2010, and been using the equation editor for some time now.

I want to write a macro that saves the selected content in the document, as image. I want to use it, to extract equations that I write in the document, as picture, and than upload them to some of the free images sites to publish.

but it will be even more useful if I would be able to extract large parts of the document, instead of just the equations (even though, extracting the equations would satisfy me for now).

In Excel there is a built in option to copy content as images, and then you can paste it anywhere you want. but for some reason, Word doesn't have that capability.

I'm looking for a way to do it anyway, even if this would involve some outer non-built-in-vba code. but it should be integrated with vba so I could work on those images, and do other stuff with them (like I said, for example - uploading them to some images site automatically).

I did some research before posting this question, and I couldn't find any working solution to the problem.

would appreciate your help!

War es hilfreich?

Lösung

Basically you need to use VBA to select the item, execute "Copy", then use the Clipboard functions to retrieve the data and save it to a file. Unfortunately I cannot post full code because it doesn't belong to me.

The Sequence is:

  • OpenClipboard
  • GetClipboardData (return value depends on clipboard format.)
    • CF_ENHMETAFILE it is a handle to a GDI MetaFile object. You can use CopyMetaFile to save it to a file. You don't have to use GlobalLock in this case.
    • CF_TEXT it is an HGLOBAL
    • CF_DIB it is an HGLOBLA containing a BITMAPINFO - can be copied to a file
  • CopyEnhMetafile or GlobalSize,GlobalLock,CopyMemory,GlobalUnlock.
  • CloseClipboard

    Private Declare Function Win32GetClipboardData Lib "Kernel32" Alias "GetClipboardData" (ByVal lClipboardFormat As Long) As Long

Where it returns an HGLOBAL, you can then use GlobalSize, GlobalLock, CopyMemory to copy it to a BSTR or Byte() object, then use ADODB.Stream to save the data to a file. , then and GlobalUnlock to cleanup. I think that it belongs to the clipboard, so you shouldn't use GlobalFree.

Seems like a lot of work I know! But once done you will have a simple function like this:

Sub SaveClipboardData(ByVal lFormat, ByVal sFileName)

And you will be able to say:

  • Select the object
  • Copy
  • SaveClipboardData CF_ENHMETAFILE, "path\to\file.emf"

Ta-Dah!

You will be able to save as BMP (CF_DIB) or EMF very easily and ImageMagick can convert them to anything else you want. I would recommend using EMF as it is a vector format, and converting to PNG for best resolution.

I have written code to do this at least twice - it's not that hard but you do have to get the function signatures right. See here for some help:

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top