PDA

View Full Version : Solved: Inserting a photo



ABabeNChrist
07-16-2011, 11:32 PM
I am trying to make some changes from excel 2007 on my PC to MAC 2011
I was using this piece below to insert photos on my PC and want to know if this will also be compatible with MAC

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Range("D7"), Target) Is Nothing Then
Cancel = True ' Don't perform the standard action
' Your code here; Target is the cell being double-clicked
'This will insert photos into merged cells
Dim Pic As Excel.Picture
Dim PicLocation As String
Dim MyRange As Range
PicLocation = Application.GetOpenFilename(FileFilter:="Pic Files (*.jpg;*.bmp), *.jpg;*.bmp", Title:="Browse to select a picture")
If PicLocation = "False" Then Exit Sub
Set Pic = Me.Pictures.Insert(PicLocation)
With Pic.ShapeRange
.Left = Target.Left
.Top = Target.Top
.LockAspectRatio = msoFalse
.ZOrder msoBringForward
If .Width > .Height Then
.Width = Target.Width
If .Height > Target.Height Then .Height = Target.Height
Else
.Height = Target.Height
If .Width > Target.Width Then .Width = Target.Width
End If
End With
With Pic
.Placement = xlMoveAndSize
.PrintObject = True
End With
End If
End Sub

mikerickson
07-24-2011, 12:29 PM
The FileFilter arguments are handled differently on a Mac. If I want a file filter I use AppleScript rather than VBA.

#If Mac Then
PicLocation = "False"
On Error Resume Next
PicLocation = MacScript("choose file of type ""public.jpeg"" with prompt ""Browse to select a picture"" ")
On Error GoTo 0
#Else
PicLocation = Application.GetOpenFilename(FileFilter:="Pic Files (*.jpg;*.bmp), *.jpg;*.bmp", Title:="Browse to select a picture")
#End If

ABabeNChrist
07-24-2011, 01:14 PM
Will both codes work or will i need to make changes as you suggested

mikerickson
07-24-2011, 02:29 PM
The format of the fileformat argument in post#1 will cause an error when run on a Mac. Using conditional compiling (as in post #2) will prevent that by causing the Window's style code not to be run or even compiled.

although on closer examination this edit should be made
PicLocation = MacScript("choose file of type {""public.jpeg"", ""public.bmp""} with prompt ...)