clipborad- saving word table as an image

R

Rama

Hi All,
Can anyone tell whether I'm asking for moon or is it possible to
copy word table as an image and save it back to filesystem using VBA and .Net?

I know I can convert table into metafilepict but I have no clue what I've to
do afterwards!!!

Dim data As New DataObject
data = Clipboard.GetDataObject()
ActDocument.Bookmarks("simple").Range.Tables(1).Range.CopyAsPicture()
data.GetData("MetaFilePict", True)
 
J

Jean-Guy Marcil

Rama was telling us:
Rama nous racontait que :
Hi All,
Can anyone tell whether I'm asking for moon or is it possible
to
copy word table as an image and save it back to filesystem using VBA
and .Net?

I know I can convert table into metafilepict but I have no clue what
I've to do afterwards!!!

Dim data As New DataObject
data = Clipboard.GetDataObject()
ActDocument.Bookmarks("simple").Range.Tables(1).Range.CopyAsPicture()
data.GetData("MetaFilePict", True)

Sort of a dirty way would be:
Copy and then paste your table as a picture (enhanced metafile) in a brand
new temporary document in a known location.
Then save that file as HTML. When you do so, a sub folder named after your
document with the suffix "_files" (Document1_Files for example) is created.
This sub folder will contain all the images from your html file.
Since that html file will have only one table, you can safely delete all
files except the jpg one.
Then, copy that jpg file to the folder you want.
Finally, delete the html file and its sub folder.
All this can be done in VBA.

If you use Word XP and later you can work with invisible documents.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

Rama

Hi Jean,
Thanks for the same. I'll try and let you know. Meanwhile I
found another solution in the net which is as follows. But I never worked
with handles(system.intptr) and i dont know which one is the good solution.

Public Class GetGraph
Private Declare Function OpenClipboard Lib "user32" (ByVal hWnd As IntPtr)
As Boolean
Private Declare Function CloseClipboard Lib "user32" () As Boolean
Private Declare Function GetClipboardData Lib "user32" (ByVal uFormat As
Integer) As IntPtr
Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal
uFormat As Integer) As Short

Public Function Grab(ByVal Handle As IntPtr) As Imaging.Metafile
Dim wmf As Imaging.Metafile
Try
If OpenClipboard(Handle) Then
If IsClipboardFormatAvailable(14) <> 0 Then
wmf = New Imaging.Metafile(GetClipboardData(14), True)
End If
CloseClipboard()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return wmf
End Function
End Class
 
R

Rama

Hi Tony,
Thanks for the immediate response. You mean to say the solution
given by jean also doesnt work? Unfortunately my code is already in
production so i've very less time to play. alternatively I can get the cell
information and stored it in the database if you say I cant retrive the table
borders etc etc.

My requiremnt is quite simple they want to have the table as an image in
HTML email so I've to copy the table as an image or just get each and every
cell's information and build the table.
 
R

Rama

Hi Tony,
Thanks for the immediate response. You mean to say the solution
given by jean also doesnt work? Unfortunately my code is already in
production so i've very less time to play. alternatively I can get the cell
information and stored it in the database if you say I cant retrive the table
borders etc etc.

My requiremnt is quite simple they want to have the table as an image in
HTML email so I've to copy the table as an image or just get each and every
cell's information and build the table.
 
T

Tony Jollans

Hi Rama,

I was going to post the code I linked to (although it looks like the
newsreader has not reproduced it properly as a link) but thought I'd confirm
it worked with a table first. it was then that I found that the borders were
not reproduced. It may be something in my set up but I don't know what it
might be. Presumably, if your code is in production, you have tested it -
does it work?

Jean-Guy's solution, as the others, is based on the enhanced metafile so
either all work fully or none do.
 
R

Rama

Hi Tony,
Thanks for reply. But how can I Use inline shape when it is
atable. excuse my ignorance.

ActiveDocument.InlineShapes(1).Range.EnhMetaFileBits
 
T

Tony Jollans

It was because my original was for a shape that I thought it prudent to
check it worked for a table before posting :)

Use ActiveDocument.Tables(1).Range.EnhMetaFileBits, or, more generally, ..

<table_ref>.Range.EnhMetaFileBits

But, as I say, it has the same limitation as the other methods.
 
J

Jean-Guy Marcil

Tony Jollans was telling us:
Tony Jollans nous racontait que :
As far as I can tell, the enhanced metafile version of a table does
not include the borders, so all these methods are doomed to failure.
If any of them worked then the VBA method described here would as well

It does, but it can be unpredictable.
I think that it has to do with screen resolution... if some borders are too
thin, they get lost in the gif or jpg.

So, one way around that, is to select the table, cut it, paste it special as
an enhanced metafile.
Work in Normal view.
Then, select it and blow it up 200 or 300%. If the lines are real thin (¼
point), then you would probably have to blow it up to the maximum Word
allows (352% with the table I had, which was about 6 inches wide in the
document before I generated the image by doing the "Paste Special").

Then generate the HTML.

Finally, when getting the image back into the next application, just reduce
it accordingly.

The few tests I have just performed were all successful on Word 2003.

Also, it is probably better to apply a White background colour to the table,
even if it appears white in Word before creating the HTML.


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
T

Tony Jollans

I don't really know what an enhanced metafile is or how word generates one
but I am now thoroughly confused.

I played with this a little and found the zoom made no difference. The only
factor that I could see that did make a difference was having something
following the table - I tested with a table in a new blank document and
thinking about it now (I'm on a different machine with Win and Word 2K so
can't check quickly) the table was at the start of the document so I had
nothing before it and I don't know whether that wouldbe significant. Anyway
I got gridlines when nothing followed the table and didn't when something
did follow it.

I'm going to look into this a bit more if I can but would be interested in
anybody else's comments.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top