Perry Choy


How to convert MS Excel to jpg or gif and paste/insert to MS Word thank you very much

 

 




Re: Once copied, can we export as a GIF file?

duck thing


I assume you're talking about simply taking a screenshot of the viewable range of the MS Excel document for insertion into a MS Word document. Do you need to do this programatically If not, just hit PrtScr on your keyboard, paste it into MS Paint, save it, and insert it into the Word document.

If you need to do this in a program, you've got two obstacles to overcome as I see it:

1.) VBA doesn't provide a "native" method for taking screenshots (as far as I know). I believe you'll need to access the API to do this. If this does turn out to be the case, I can help you out with code.

2.) VBA has no support for converting bitmap images (which is the format your screenshot would be in) into .JPG or .GIF files. You'd need to use external libraries.

If you can provide a bit more information on what you need to do and why, I can give you much more specific advice on how to solve the problem.







Re: Once copied, can we export as a GIF file?

Perry Choy

I want copy excel selected range convert to jpg, gif and paste to MS outlook mail. Thank you




Re: Once copied, can we export as a GIF file?

Derek Smyth

I doubt very much you can do that, or if you can it's a long difficult way to do a simple thing.

If your looking to email parts of a spreadsheet your best bet is to format the cells into html/text and display them in the email message. How you go about putting the cells as html/text into an email message is another matter, if you co with copy and paste then the user needs to manually do the paste in Outlook, on the other hand if your sending the email through VBA interop with Outlook objects then all you need to do is create the email and set it's message body and address info.






Re: Once copied, can we export as a GIF file?

duck thing

Perry Choy wrote:
I want copy excel selected range convert to jpg, gif and paste to MS outlook mail. Thank you

I'll repeat my previous question: do you NEED to do this programatically Why not just take a screenshot manually, convert it using an imaging program, and paste the resulting JPG/GIF file into the message






Re: Once copied, can we export as a GIF file?

Perry Choy

Yes, i need to do this programatically. thanks



Re: Once copied, can we export as a GIF file?

duck thing

This a very complex problem. I'd be happy to look at whatever code you might have, but this is far beyond the sort of thing VBA is intended for.






Re: Once copied, can we export as a GIF file?

Francisco Parrilla

Sorry, my english es basic

XL97: Como crear un archivo GIF de un Grafico de Microsoft Excel

Esto es cuando ya esta creada imagen dentro de la hoja.
Sub creargif()
Dim mychart As Chart
Set mychart = ActiveSheet.ChartObjects(1).Chart
mychart.Export Filename:="c:\Mychart.gif", FilterName:="GIF"
End Sub





Re: Once copied, can we export as a GIF file?

GoHogsar

If I understand your question, here is how I would go about it:

1. Highlight the portion of the Excel sheet you want to copy

2. Hold down Shift and click the Edit menu.

3. Click Copy Picture (us two defaults selected)

4. Paste into Word

Hope this helps.





Re: Once copied, can we export as a GIF file?

stefen

hi friend

i have seen in one of the thread , u have suggested using api excel sheet can be converted to jpg gif image.

could u pls kindly provide me the code

thanks and regards

durairaj





Re: Once copied, can we export as a GIF file?

Jon Peltier

I don't know why everyone thinks it's so hard to copy an image of a region and paste it elsewhere.

To copy a chart as a picture:

ActiveChart.CopyPicture _
  Appearance:=xlScreen, Size:=xlScreen, Format:=xlBitmap

To copy a range as a picture:

ActiveSheet.Range("B1:C6").CopyPicture Appearance:=xlScreen, Format:=xlBitmap

These copy the selections as bitmaps, but you can also copy as a metafile (Format:=xlPicture), and you can copy an Excel range and paste it into Word as a table.

You can get this or similar syntax by simply turning on the macro recorder while you manually do what you want your program to do. Follow up the commands above with pasting of your copied object into the target location or application. I cover similar topics, including the interaction of Excel with other applications, on this web page:

http://peltiertech.com/Excel/XL_PPT.html

You can also export an Excel chart as an image file:

ActiveChart.Export "C:\My Documents\MyChart.gif", "GIF"

Various image file formats are available, but you would do well to avoid JPG and stick to GIF or PNG.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______





Re: Once copied, can we export as a GIF file?

duck thing

Jon Peltier wrote:

I don't know why everyone thinks it's so hard to copy an image of a region and paste it elsewhere.

I guess I both misunderstood his problem and what the CopyPicture function is capable of. I was under the impression that he wanted to copy an image of an entire document, rather than just a single region -- simple enough, but I was also working under the misconception that CopyPicture wouldn't be able to grab a screenshot of an area bigger than what could be displayed (don't know why I thought this...) You're absolutely right!






Re: Once copied, can we export as a GIF file?

Quadra950

I had no idea we could copy a range and paste as a graphic... I do the chart gif thing all the time for userforms (load the .GIF in to an image frame or the background of multipage, etc.). Copying a range as graphic and saving is as GIF and then using the image would reduce so, so much code writing. I see many great uses for this!!!

So --- once we copy the range, can we:

1) export as a graphic file and save to the hard drive (like we can with GIF export for the charts)

2) control the file fomat (aka - .GIF, .JPEG, etc)

I did follow the link the gentleman's website and all his code shows pasting the copied range into Excel or other apps. I didn't see an example where he exported the copied range and saved it as a GIF file.

I'm going to experiment with a couple ideas and if anything works, I'll post it later.

Thanks, John.





Re: Once copied, can we export as a GIF file?

Quadra950

Not my cleanest code... the only way I can see doing this is by "tricking" Excel into thinking the copied range is a chart. The attached is a quick & dirty run to see if this would work or not. It does. When I use this in a real application I'll create a new workbook, perform the chart, etc., in that work and then delete everything. The current application I'm working on could use variants of this code 100+ times per session, so I'm very worried about the source workbook(s) becoming corrupted due to the constant adding and deleting of sheets & charts. We're using Office 2003 here, does XP have an easier way to do this

Sub COPY_TEXT_TEST()

'PROOF OF CONCEPT

Dim FName As String
Dim Temporary_Worksheet As Worksheet
Dim Temporary_Chart As Chart
Dim Temporary_Picture As Picture

'Adding a temporary worksheet
Set Temporary_Worksheet = Worksheets.Add

'Adding a chart, this is just a holding area for the copied range (eventually)
Charts.Add

ActiveChart.Location Where:=xlLocationAsObject, Name:=Temporary_Worksheet.Name

Set Temporary_Chart = ActiveChart

'Copying my range
ThisWorkbook.Sheets("BSCs").Range("B7:G21").CopyPicture Appearance:=xlScreen, Format:=xlPicture

'Pasting it on top of chart
Temporary_Chart.Paste

Set Temporary_Picture = Selection

'Placing some extra space around the image to keep things clean
With Temporary_Chart.Parent
.Width = Temporary_Picture.Width + 5
.Height = Temporary_Picture.Height + 5
End With

FName = ThisWorkbook.Path & "\Cache\NEW_PICTURE.gif"

Temporary_Chart.Export Filename:=FName, FilterName:="gif"

End Sub





Re: Once copied, can we export as a GIF file?

duck thing

That seems like a straight-forward way to do it, and it seems like it should work fine. However, it's going to be awfully inefficient. Can't you just grab the bitmap data from the clipboard as a DIB and write it to a file, then run external conversion software for the BMP->GIF conversion I'll see if I can come up with a way to do this.