Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / October 2007

Tip: Looking for answers? Try searching our database.

Insert Image

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Cy Dodimead - 12 Oct 2007 18:00 GMT
I have a folder of images which are numerically named 1 thru 171. I want to
insert image 1 into C1, image 2 into C2 etc etc. I have in the past had to
go  Insert, Picture, from File. But after a while it become very boring and
tiring. Does any body know of a macro I could record/write to do this for
me? I have never written a macro so please be gently.

On the same note is there a way to get the image in the cell instead of
floating over the cells? Would be nice to size the cell around the image by
double clicking instead of dragging.
Dave Peterson - 12 Oct 2007 20:14 GMT
Images float over the worksheet--they aren't contained in the cell.

You could put the pictures over each cell or you could put the pictures in a
comment associated with that cell.

This should work if your pictures are all in the same folder and have an
extension of JPG.

Option Explicit
Sub testme()
   Dim iCtr As Long
   Dim myPath As String
   Dim TestStr As String
   Dim myPict As Picture
   Dim myPictName As String

   'change to the correct location of the picture files    
   myPath = "c:\my pictures\test\"
   If Right(myPath, 1) <> "\" Then
       myPath = myPath & "\"
   End If
   
   With Worksheets("Sheet1")
       .Pictures.Delete 'remove any existing pictures???
       For iCtr = 1 To 2 '171 when you've finished testing!
           myPictName = myPath & iCtr & ".jpg"
           TestStr = ""
           On Error Resume Next
           TestStr = Dir(myPictName)
           On Error GoTo 0
           If TestStr = "" Then
               MsgBox "Picture: " & myPictName & " wasn't found"
           Else
               Set myPict = .Pictures.Insert(myPictName)
               With .Cells(iCtr, "C")
                   myPict.Top = .Top
                   myPict.Width = .Width
                   myPict.Height = .Height
                   myPict.Left = .Left
                   myPict.Placement = xlMoveAndSize
                   myPict.Name = "Pict_" & .Address(0, 0)
               End With
           End If
       Next iCtr
   End With
End Sub

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

> I have a folder of images which are numerically named 1 thru 171. I want to
> insert image 1 into C1, image 2 into C2 etc etc. I have in the past had to
[quoted text clipped - 5 lines]
> floating over the cells? Would be nice to size the cell around the image by
> double clicking instead of dragging.

Signature

Dave Peterson

Dave Peterson - 12 Oct 2007 20:36 GMT
ps.  If you want to put the pictures in a comment, you can review the manual
technique that Debra Dalgleish shares:
http://contextures.com/xlcomments02.html#Picture

Option Explicit
Sub testme02()

   Dim iCtr As Long
   Dim myPath As String
   Dim TestStr As String
   Dim myPict As Picture
   Dim myPictName As String
   
   myPath = "c:\my pictures\test\"
   If Right(myPath, 1) <> "\" Then
       myPath = myPath & "\"
   End If
   
   With Worksheets("Sheet1")
       For iCtr = 1 To 2 '171 when you've finished testing!
           myPictName = myPath & iCtr & ".jpg"
           TestStr = ""
           On Error Resume Next
           TestStr = Dir(myPictName)
           On Error GoTo 0
           If TestStr = "" Then
               MsgBox "Picture: " & myPictName & " wasn't found"
           Else
               With .Cells(iCtr, "C")
                   .ClearComments 'remove any existing comment
                   .AddComment Text:=""
                   .Comment.Shape.Fill.UserPicture picturefile:=myPictName
               End With
           End If
       Next iCtr
   End With
End Sub

> Images float over the worksheet--they aren't contained in the cell.
>
[quoted text clipped - 59 lines]
>
> Dave Peterson

Signature

Dave Peterson

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.