PC Review Forums Newsgroups Microsoft Excel Microsoft Excel Discussion VBA insert a photo

Reply

VBA insert a photo

 
Thread Tools Rate Thread
Old 15-03-2006, 07:49 PM   #1
azidrane
Guest
 
Posts: n/a
Default VBA insert a photo


I am inserting a photo into a worksheet and sizing it to a specific
range when a name is chosen from a list validated cell. I am using the
following code:

*********************************
Dim RepPhoto As Picture
Dim CurrentWorksheet As Worksheet
Dim RepPhotoFileName As Variant
Dim strFirstName As Variant
Dim strLastName As Variant

Set CurrentWorksheet = ActiveSheet

'Get the FIRST NAME of the full name stored in the cell H5
strFirstName = Trim(Right(Range("H5"), Len(Range("H5")) - InStr(1,
Range("H5"), ",", vbTextCompare) - 1))

''Get the LAST NAME of the full name stored in the cell H5
strLastName = Trim(Left(Range("H5"), InStr(1, Range("H5"), ",",
vbTextCompare) - 1))

'Store this as a file name
RepPhotoFileName = "\\Networkdrive\photos\" & strFirstName & " " &
strLastName & ".jpg"

'Insert the photo into the worksheet
Set RepPhoto = CurrentWorksheet.Pictures.Insert(RepPhotoFileName)

'Resize the photo to this merged cell (B4:C10)
With Range("B4:C10")
RepPhoto.Top = .Top
RepPhoto.Width = .Width
RepPhoto.Height = .Height
RepPhoto.Left = .Left
RepPhoto.Placement = xlMoveAndSize
End With

*********************************

This works, UNTIL I share the workbook....
When it shared we get this error:
"Runtime error: 1004"
"Unable to get the Insert property of the Picture Class"

Cannot figure it out...


Also, does anyone know how to delete the photo from the work sheet when
you choose a different name?

There are no other pic's on the sheet so maybe a with statement?

Cheers!

  Reply With Quote
Old 15-03-2006, 08:20 PM   #2
Dave Peterson
Guest
 
Posts: n/a
Default Re: VBA insert a photo

If you look at excel's help for "Features that are unavailable in shared
workbooks", you'll see that inserting pictures is something you can't do.

Maybe you could put all the picutures on a sheet (hide that sheet) and then copy
it to the location you want.

Or just put all the pictures in that location and hide all of them--then use
code to just show the one you want.

J.E. McGimpsey has a routine that hides/unhides pictures based on the value of a
cell.
http://www.mcgimpsey.com/excel/lookuppics.html

You might be able to use some of it.

azidrane wrote:
>
> I am inserting a photo into a worksheet and sizing it to a specific
> range when a name is chosen from a list validated cell. I am using the
> following code:
>
> *********************************
> Dim RepPhoto As Picture
> Dim CurrentWorksheet As Worksheet
> Dim RepPhotoFileName As Variant
> Dim strFirstName As Variant
> Dim strLastName As Variant
>
> Set CurrentWorksheet = ActiveSheet
>
> 'Get the FIRST NAME of the full name stored in the cell H5
> strFirstName = Trim(Right(Range("H5"), Len(Range("H5")) - InStr(1,
> Range("H5"), ",", vbTextCompare) - 1))
>
> ''Get the LAST NAME of the full name stored in the cell H5
> strLastName = Trim(Left(Range("H5"), InStr(1, Range("H5"), ",",
> vbTextCompare) - 1))
>
> 'Store this as a file name
> RepPhotoFileName = "\\Networkdrive\photos\" & strFirstName & " " &
> strLastName & ".jpg"
>
> 'Insert the photo into the worksheet
> Set RepPhoto = CurrentWorksheet.Pictures.Insert(RepPhotoFileName)
>
> 'Resize the photo to this merged cell (B4:C10)
> With Range("B4:C10")
> RepPhoto.Top = .Top
> RepPhoto.Width = .Width
> RepPhoto.Height = .Height
> RepPhoto.Left = .Left
> RepPhoto.Placement = xlMoveAndSize
> End With
>
> *********************************
>
> This works, UNTIL I share the workbook....
> When it shared we get this error:
> "Runtime error: 1004"
> "Unable to get the Insert property of the Picture Class"
>
> Cannot figure it out...
>
> Also, does anyone know how to delete the photo from the work sheet when
> you choose a different name?
>
> There are no other pic's on the sheet so maybe a with statement?
>
> Cheers!


--

Dave Peterson
  Reply With Quote
Old 15-03-2006, 08:33 PM   #3
azidrane
Guest
 
Posts: n/a
Default Re: VBA insert a photo

Crap. Thanks dave. The thing is we have 194 images, all quite large.
That would really bulk up the size of the file.

Thanks for the info though.

Cheers!

  Reply With Quote
Old 15-03-2006, 08:52 PM   #4
Dave Peterson
Guest
 
Posts: n/a
Default Re: VBA insert a photo

Sharing a workbook isn't for wimps <vbg>.

That's one reason why most people don't share workbooks too often <vvbg>.

azidrane wrote:
>
> Crap. Thanks dave. The thing is we have 194 images, all quite large.
> That would really bulk up the size of the file.
>
> Thanks for the info though.
>
> Cheers!


--

Dave Peterson
  Reply With Quote
Old 15-03-2006, 08:58 PM   #5
azidrane
Guest
 
Posts: n/a
Default Re: VBA insert a photo

Shared workbooks have become quite useful in our office.

  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off