Shapes.AddPicture - Application-Defined or Object-Defined Error

khabi21

New Member
Joined
Oct 12, 2016
Messages
35
This macro is supposed to add the most recent picture from my camera roll and insert it in the designated cell in Excel when a button is pressed on the worksheet. This macro was working at one point, but my guys ran into an issue with it last week, and I can't figure out why. It is giving me an Application-defined or Object-defined error at the "Shapes.AddPicture" line below.

I am working with Excel 2010. Any help would be appreciated.

Code:
Private Sub GetMostRecentImageTrend()


'When the trend screen button on the "Pictures" tab is pressed, this macro will insert the most recent image.


    Dim FileSys As FileSystemObject
    Dim objFile As File
    Dim myFolder
    Dim strFileName As String
    Dim dteFile As Date
    Dim myDir As String
    Dim pic As Object
        
    'Set path for files:
    myDir = Environ("USERPROFILE") & "\Pictures\Camera Roll"
    
    'Set up filesys objects:
    Set FileSys = New FileSystemObject
    Set myFolder = FileSys.GetFolder(myDir)
    
    'Loop through each file & get date last modified. If largest date (most recent) then store Filename:
    dteFile = DateSerial(1900, 1, 1)
    For Each objFile In myFolder.Files
        If objFile.DateLastModified > dteFile Then
            dteFile = objFile.DateLastModified
            strFileName = objFile.Name
        End If
    Next objFile
    
    'Insert the image into the designated cell.
    With Application.ActiveWorkbook.Sheets("Pictures").Shapes.AddPicture(myFolder & "\" & strFileName, False, True, 1, _
    1, -1, -1)
        .Top = ActiveWorkbook.Sheets("Pictures").Range("B9").Top
        .Left = ActiveWorkbook.Sheets("Pictures").Range("B9").Left
        .Height = ActiveWorkbook.Sheets("Pictures").Range("B9:B42").Height
        .Width = ActiveWorkbook.Sheets("Pictures").Range("B9:J9").Width
    End With
    
    'Delete the picture from the camaera roll directory after pasting:
    Kill (myFolder & "\" & strFileName)
        
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
When you get the error what are the values of myFolder and strFileName?
 
Upvote 0
When you get the error what are the values of myFolder and strFileName?

myFolder = "C:\Users\****\Pictures\Camera Roll" -- I put **** for privacy reasons. It changes to whomever it is currently logged in via the environ function.

strFileName = "WIN_20161012_075235.JPG" -- This is the file name of the most recent picture in the folder.
 
Upvote 0
Those both look fine and I assume the file/folder/directory all exist.

Where exactly do you get the error?
 
Upvote 0
Where exactly do you get the error?

The error occurs at this line:
Code:
With Application.ActiveWorkbook.Sheets("Pictures").Shapes.AddPicture(myFolder & "\" & strFileName, False, True, 1, _    1, -1, -1)

Yes, they all exist. This code worked for me when I tested it last week, then I gave it to my guys to start using and it bugged on them. So I'm pretty sure everything is defined correctly, and my guess is it's just some sort of underlying issue with the Shapes.AddPicture method. I am not a coder though, so I can't debug very well if it's a deeper issue.
 
Upvote 0
Where exactly do you want the image to go?

Is it on a sheet in the same workbook as the code is in?
 
Upvote 0
Where exactly do you want the image to go?

It goes on the same sheet where the button is pressed, which is in the same workbook that the code resides. There is no opening of other workbooks or anything in this part of the macro.

You can see in the with statement that it goes in cell B9 in the active workbook.

Code:
.Top = ActiveWorkbook.Sheets("Pictures").Range("B9").Top.Left = ActiveWorkbook.Sheets("Pictures").Range("B9").Left
 
Upvote 0
Sorry the code from the previous post is supposed to have a new line before the .Left. I am still figuring this stuff out. :)
 
Upvote 0
Do you think it could be some missing references?

I currently have the following checked in the References Dialogue Box:
Visual Basic for Applications
Microsoft Excel 14.0 Object Library
OLE Automation
Microsoft Office 14.0 Object Library
Microsoft Scripting Runtime
Microsoft Forms 2.0 Object Library
 
Upvote 0
If you had missing references the likelihood would be that the code wouldn't run at all.

Can you manually add the picture from the roll?
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,380
Members
448,955
Latest member
BatCoder

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top