+ Reply to Thread
Results 1 to 4 of 4

Replace "ActiveSheet.Pictures.Insert" with "Shapes.Addpicture" and "SaveWithDocument"

Hybrid View

  1. #1
    Registered User
    Join Date
    03-17-2018
    Location
    Ro
    MS-Off Ver
    2010
    Posts
    3

    Replace "ActiveSheet.Pictures.Insert" with "Shapes.Addpicture" and "SaveWithDocument"

    Hello, I have little to no experience in righting codes. I have managed to get my hands on the code bellow .
    Function: insert's picture in excel from location

    The problem i have is once I send the file to another user, if he/she will not have an opened connection to the location from were the pictures were inserted , they will get an error instead of the picture. It's something with "ActiveSheet.Pictures.Insert" that requires an established connection to the source, even thow the pictures were inserted in the sheet.

    "Shapes.Addpicture" and "SaveWithDocument" should solve this problem. I`ve tried myself to modify the code with no result.

    Can you please help me ?


    Sub Picture()
     Dim picname As String
        
     Dim pasteAt As Range
     Dim lThisRow As Long
        
        lThisRow = 2
        
        Do While (Cells(lThisRow, 2) <> "")
           
            'Range("AB2").Select 'This is where picture will be inserted
            Set pasteAt = Cells(lThisRow, 28)
            pasteAt.Select 'This is where picture will be inserted
               
            'Dim picname As String
            'picname = Range("A2") 'This is the picture name
             picname = Cells(lThisRow, 1) 'This is the picture name
                
            ActiveSheet.Pictures.Insert("C:\Users\xxxxxxxxxx\" & picname & ".jpg").Select 'Path to where pictures are stored
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''
            ' This resizes the picture
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''
            With Selection
                '.Left = Range("A2").Left
                '.Top = Range("A2").Top
                .Left = pasteAt.Left
                .Top = pasteAt.Top
                   
                .ShapeRange.LockAspectRatio = msoTrue
                .ShapeRange.Height = 105
                .ShapeRange.Rotation = 0#
                
            End With
               
            lThisRow = lThisRow + 1
           
        Loop
           
        Range("AA1").Select
        Application.ScreenUpdating = True
           
        Exit Sub

  2. #2
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Replace "ActiveSheet.Pictures.Insert" with "Shapes.Addpicture" and "SaveWithDocument"

    Use this method:
    - note that you MUST include all 7 paramaters when using AddPicture
    - AddPicture creates a picture from an existing file
    - AddPicture returns a Shape object that represents the new picture.
    - setting height and width to -1 leaves the size the same as original image

    Dim myPic As Shape
    Dim picPath as String
    picPath = "C:\TestArea\jpg\"            'end your path with  "\"
    
    
    ..... rest of code 
    
    'EVERY PARAMATER IS REQUIRED
        Set mypic = ActiveSheet.Shapes.AddPicture(Filename:=picPath & picName & ".jpg", linktofile:=False, savewithdocument:=True, Left:=0, Top:=0, Width:=-1, Height:=-1)
    'RESIZE AND POSITION
            With mypic
                .LockAspectRatio = msoCTrue
                .Left = pasteAt.Left
                .Top = pasteAt.Top
                .Height = 105
            End With
    
    
    ..... rest of code
    Last edited by kev_; 03-17-2018 at 10:55 AM.
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  3. #3
    Registered User
    Join Date
    03-17-2018
    Location
    Ro
    MS-Off Ver
    2010
    Posts
    3

    Re: Replace "ActiveSheet.Pictures.Insert" with "Shapes.Addpicture" and "SaveWithDocument"

    Thank you. It works.

    Can you please help me with two more things?

    1) I want to replace : picPath = "C:\TestArea\jpg\" with the adresse found in cell B1 and do something like picPath = "B1"

    2) On error don`t show message , write in cell "Unable to Find Photo"

    Thank you

  4. #4
    Registered User
    Join Date
    03-17-2018
    Location
    Ro
    MS-Off Ver
    2010
    Posts
    3

    Re: Replace "ActiveSheet.Pictures.Insert" with "Shapes.Addpicture" and "SaveWithDocument"

    Thank you. It works.

    Can you please help me with two more things?

    1) I want to replace : picPath = "C:\TestArea\jpg\" with the adresse found in cell B1 and do something like picPath = "B1"

    2) On error don`t show message , write in cell "Unable to Find Photo"

    Thank you

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 02-05-2019, 12:03 AM
  2. [SOLVED] Column X-Ref list - Sheet1 Col A "pages", Col B:FL "Req" to Sheet2 ColA "req", ColB "page"
    By excel-card-pulled in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 04-07-2017, 09:30 AM
  3. [SOLVED] VBA help needed to remove all "/" then replace with "-" from cell "B3"and "B5"
    By krjoshi in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-17-2014, 02:11 PM
  4. [SOLVED] If Not ActiveSheet.Range("A1").Value Like "apple" Then MsgBox "Error"
    By HerryMarkowitz in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-18-2014, 02:16 PM
  5. Method "AddPicture" of object "InlineShapes" failed - but only one 1 computer
    By Alves76 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-20-2013, 10:49 AM
  6. Replies: 4
    Last Post: 11-17-2013, 12:05 PM
  7. [SOLVED] How to USE """"" cells count """"" change font color
    By austin123456 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-09-2013, 06:14 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1