Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to put a JPG file into a Excel Cell? 1

Status
Not open for further replies.

ReportMan

Programmer
Aug 13, 2001
40
0
0
US
I have a JPG file that I am try to put into a Excel cell. I am having no luck with this. Anyone ever done this before? It sounds easy enough but doesn't work for me.

This is the code I am using.

toSheet.Range("C28").Value = TempSig.JPG

Any help would be greatly appreciated.
 
Try this:

toSheet.Range('C28').Select
toSheet.Pictures.Insert("TempSig.JPG")

Hope it helps

-- AirCon --
 
AirCon,

I didn't know you can place a picture in an cell. If so, starting what version you can do this?

Your code does place a picture on the worksheet (with its upper left corner at the selected cell), but then it is easily moved around. But it does not put it in a cell. Is it possible at all?

Stella
 
Stella,

I only have Office2000. Up to this version, there is no capability to put a picture inside the cell. We can't even do it from Excel manually (I don't know for the newer version). We can only insert a picture in the worksheet as an OLE

You are right. The thread title was asking to put a pic inside the cell. But I assumed that ReportMan was actually asked to insert in the worksheet with the selected cell as the Top-Left coordinate.

So to ReportMan,
I am sorry if I was wrong for making those assumption, and please ignore my posted.

Regards

-- AirCon --
 
I haven't had a chance to really test this logic yet but had planned on doing so this afternoon. What this JPG file contains is a signature that needs to be placed in a certain location on the worksheet. This worksheet is a duplicate of a printed report which has the signature graphic printed on it.

If I understand what you are saying, is that this is not possible?
 
I need to clarify this first.

What is the step that you need if you do it directly from Excel ? Is that "Insert -> Picture -> From File" ?

-- AirCon --
 
Yes, I just did that with this JPG file and it worked perfectly but when I execute the code in FoxPro it bombs. It sure is so strange doings...

Got any ideas??
 
In that case you can try the code I posted.

Or are you saying you did tried my code and it gave you an error ?

-- AirCon --
 
It doesn't bomb... It just doesn't place anything into the cell. Here is the code being used...

* Update Sheet 14 (Declaration)
SELECT .aPagePrintOptions[tcFileId, PPO_RESULT_CURSOR]
GO TOP
toWorkbook.Sheets("Declaration").Select
toSheet = toWorkbook.ActiveSheet
toSheet.Range("A4").Value = oFormSet.cFormVal( "NOTES" )
toSheet.Range("A28").Value = dCurrentDate()

COPY TO tempsig FIELDS userinfo1.sigfile NEXT 1

*** execute the following code, assuming the file name for the output JPG image is "TestSig.JPG":
*** Open two files, "UserInfo1.fpt" contains the physical JPG data.
*** and Create a file handle for the output file "TestSig.JPG"
handlein = FOPEN("TempSig.fpt")
handleout = FCREATE("TempSig.jpg")

*** To determine the length of the Input file "TempSig.fpt"
gnEnd = FSEEK(handlein, 0, 2)
gnTop = FSEEK(handlein, 0)

*** Store the whole file into a string "str1"
str1 = FREAD(handlein, gnEnd)

*** Offset 599 bytes from Str1 and save it to "str2"
str2 = RIGHT(str1,LEN(str1)-599)

*** Write "Str2" into the JPG file "TempSig.JPG"
n = FWRITE(handleout,str2)

*** Close both opened files
=FCLOSE(handlein)
=FCLOSE(handleout)

toSheet.Range('C28').Select
toSheet.Pictures.Insert("TempSig.JPG")
toSheet.Range("H28").Value = oFormSet.lSignatorTitle()
 
AirCon

I think also you may onto something where if you put a picture in an Excel sheet and save it, and e-mail it, will the picture follow?

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
I just ran a test where I put the JPG signature into a newly created Excel workbook and e-mailed it to myself. The e-mail attachment is just fine. The Excel file is OK. Don't really know what that proves but it does work.
 
AirCon,

I currently use Office2000. If "Insert -> Picture -> From File" is what ReportMan needed, then it is exactly what your code does, though I tested it not from VFP, but from Excel VB and substitutes apostrophies for quotations around the range ("C28" instead of 'C28'). Should work from VFP, too, then. If I have time to try, I will let you know.

 
ReportMan, AirCon,

This is the test I ran from VFP6. Works perfectly with .GIF and BMP, but returns me the Automation Server OLE error 1429 with .JPG. The text is: "Dispatch exception code 0 from Microsoft Excel: Unable to get the Insert property of the pictures class". Why is that?

toExcel = Createobject("Excel.Application")
toExcel.Visible = .T.
toExcel.Workbooks.Add()
toSheet = toExcel.ActiveSheet
toSheet.Range("C28").Select
toSheet.Pictures.Insert("MyPicture.???")
 
Looks like you need the full path of the file even if it is in the search path of VFP.

e.g.

toSheet.Pictures.Insert("C:\data\test\B_pic1.jpg")

Brian
 
Brian,

I do use full path when run it. It works with .GIF and .BMP, but not with .JPG. All are located in the same folder.

Stella
 
I just tried it with the full path using a JPG file and it worked just fine. I appreciate all the help that was given.
 
I finally found some Microsoft documentation that actually explains why you need the path.... Wish I had found it sooner...

Adding pictures
Pictures are added to the Shapes collection with the AddPicture method. The syntax is as follows:
oSheet.Shapes.AddPicture(cFileName, lLinkToFile, lSaveWithDocument,
nLeft, nTop, nWidth, nHeight)
cFileName Character The fully pathed filename of the picture.
lLinkToFile Logical Indicates whether to link the picture to the file from which it was created or to create a copy inside the document.
lSaveWithDocument Logical Indicates whether to save the linked picture with the file or to store only the link information in the document. This argument must be True if lLinkToFile is False.
nLeft, nTop Numeric The position (in points) of the upper-left corner of the picture, measured from the upper-left corner of the spreadsheet.
nWidth, nHeight Numeric The width and height of the picture, in points.
The following example adds the TasTrade logo to the worksheet. Be sure to fully qualify the path, as Excel does not know anything about VFP’s defaults.
LogoFile = _SAMPLES + "TasTrade\Bitmaps\TTradeSm.bmp"
oSheet.Shapes.AddPicture(LogoFile, .T., .T., 32, 32, 72, 72)
A little playing is required when you’re trying to determine the width and height of the graphic. None of the parameters are optional, so you need to know the width and height of any graphic you’re adding.
 
And I found out why my test example didn't work with .JPG, even with full path. The file was corrupted, didn't open and in Windows Explorer showed 0 Kb file size.

When I used another file, it was working OK, with both

toSheet.Pictures.Insert(...)
and
toSheet.Shapes.AddPicture(...)
 
To ReportMan (and anyone) that interesting on this thread.

Please accept my apology. Last night, all of sudden my house was flooded. I was panic and unplugged the electricity. So I can't replied anymore.

I already test it with JPG. It works if the picture is taken directly from a real JPG one and not from VFP memo file. I also ran into error if I insert the JPG that created from memo file.

I haven't got a clue about this. I'll try to find out later when I finish cleaning up the house

Once again, I really am sorry :(
Regards


-- AirCon --
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top