• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Code Compiles in 2010 but not 2007

betsig250

New Member
Can someone explain why this works in 2010 but not 2007? When my user tries to either run the Insert_Picture or compile it the yellow line is at this line.
Private Sub InsertPicture(Pict As String, PictCell As Range)

The error I am getting is "Compile Error in Hidden Module - Code" (name of my module)

The original line of code that my user used was the following
ActiveSheet.Pictures.Insert(Pict).Select

I modified it to the below so the picture is now an object and therefore I have the ability to resize the picture to the cells dimensions.
Thanks!

Code:
Private Sub Insert_Picture()
Dim Pict As String
Dim ImgFileFormat As String
Dim PictCell As Range
Dim Ans As Integer

Call Unprotect


'First file format listed is the default.
ImgFileFormat = "jpg (*.jpg),*.jpg, bmp (*.bmp),others, tif (*.tif),*.tif, pdf (*.pdf), *.pdf"

Pict = Application.GetOpenFilename(ImgFileFormat)


'If user clicks cancel on the insert image box.
: GetPict
If Pict = "" Then ActiveSheet.Protect Password:="M-CS-2014"


Ans = MsgBox("Open : " & Pict, vbYesNo, "Insert Picture")
If Ans = vbNo Then GoTo GetPict


'Grabbing the cell that has the name range "Picture
GetCell:
Application.GoTo Reference:="Picture"
Set PictCell = Range(ActiveCell.Address)


'Calling the routine to insert the picture.
InsertPicture Pict, PictCell

Call Protect
End Sub


Private Sub InsertPicture(Pict As String, PictCell As Range)
Dim p As Object
Dim t, l, w, h As Double


'Inserting the picture in full resolution. Grabbing the dimmension of the picture and then resizing it in a locked ratio.
'This will fill the cell as much as it can but keep it from distorting.
Set p = ActiveSheet.Pictures.Insert(Pict)
With PictCell
t = .Top
l = .Left
w = .Offset(0, .Columns.Count).Left - .Left
h = .Offset(.Rows.Count, 0).Top - .Top
End With

With p
.Top = t
.Left = l
.Width = w
.Height = h
End With
Set p = Nothing
End Sub
 
You can add a section like:
Code:
Select Case Application.Version
  Case 12
  ' code for Excel 2007
  Case 14
  ' code for Excel 2010
  Case 15
  ' code for Excel 2010
End Select

to allow for various changes between versions[/code]
 
Thanks.. I was hoping not to have different versioning. :)

But I do have some code from my older days that I know will work with 2007. So will try that..
 
Hi ,

I ran the code you have posted in Excel 2007 , and I did not get any error.

Can you say where this line of code was placed ?

ActiveSheet.Pictures.Insert(Pict).Select

Narayan
 
Back
Top