felix040893
New Member
- Joined
- Mar 11, 2019
- Messages
- 4
Hi guys & girls,
I'm currently trying to create a Macro in Excel that opens the most recent picture (.jpg) out of a folder for me.
I'm not bad with Excel, but I've never worked with Macros. Using the most recent office version (March 2019).
I found the following code online:
Sub AktuellesDokument()
Dim varDirectory As Variant
Dim flag As Boolean
Dim i As Integer
Dim strDirectory As String
Dim thisFile As String
Dim currentFile As String
currentFile = 0
Dim currentFileAll As String
strDirectory = "C:\Users\fw\Desktop\Bilder Kamera"
i = 1
flag = True
varDirectory = Dir(strDirectory, vbDirectory)
While flag = True
If varDirectory = "" Then
flag = False
Else
If i > 2 Then
thisFile = Right(varDirectory, 15)
thisFile = Replace(thisFile, ".jpg", "")
thisFile = Replace(thisFile, "_", "") * 1
If thisFile > currentFile Then
currentFile = thisFile
currentFileAll = varDirectory
End If
End If
varDirectory = Dir
i = i + 1
End If
Wend
Workbooks.Open strDirectory & currentFileAll
End Sub
Unfortunately that doesn't work.. I'm getting an error message back '1004': That tells me that excel cant access the data in the Folder "Bilder Kamera".
The red line of code is where I seem to be getting problems. Maybe because I found the code online and only changed the path to the folder and changed the code from a ".xls" to a ".jpg" and now the workbooks.open part doesn't match with the .jpg as it's not a workbook ?!
Can any of you help me fix this code or come up with a better one?
Thanks so much in advance!
Best from Germany,
Felix
I'm currently trying to create a Macro in Excel that opens the most recent picture (.jpg) out of a folder for me.
I'm not bad with Excel, but I've never worked with Macros. Using the most recent office version (March 2019).
I found the following code online:
Sub AktuellesDokument()
Dim varDirectory As Variant
Dim flag As Boolean
Dim i As Integer
Dim strDirectory As String
Dim thisFile As String
Dim currentFile As String
currentFile = 0
Dim currentFileAll As String
strDirectory = "C:\Users\fw\Desktop\Bilder Kamera"
i = 1
flag = True
varDirectory = Dir(strDirectory, vbDirectory)
While flag = True
If varDirectory = "" Then
flag = False
Else
If i > 2 Then
thisFile = Right(varDirectory, 15)
thisFile = Replace(thisFile, ".jpg", "")
thisFile = Replace(thisFile, "_", "") * 1
If thisFile > currentFile Then
currentFile = thisFile
currentFileAll = varDirectory
End If
End If
varDirectory = Dir
i = i + 1
End If
Wend
Workbooks.Open strDirectory & currentFileAll
End Sub
Unfortunately that doesn't work.. I'm getting an error message back '1004': That tells me that excel cant access the data in the Folder "Bilder Kamera".
The red line of code is where I seem to be getting problems. Maybe because I found the code online and only changed the path to the folder and changed the code from a ".xls" to a ".jpg" and now the workbooks.open part doesn't match with the .jpg as it's not a workbook ?!
Can any of you help me fix this code or come up with a better one?
Thanks so much in advance!
Best from Germany,
Felix