PDA

View Full Version : Run-time eerror 424



missb
03-26-2018, 10:55 PM
Hi forum!
I found a VBA script in another thread to help me insert pictures with their filenames, but it's not working for me so I'm hoping someone can tweak it to get it to work, because coding COMPLETELY eludes me. No matter how hard I try.
Here's what I've got:


Sub PicWithCaption()
Dim file
Dim path As String
path = "C:\Users\bianca.bianca-PC\Desktop\Barcodes"
file = Dir(path & "*.*") ' ALL files

CaptionLabels.Add Name:="Filename"

Do While file <> ""
' test for extension
If UCase(Right(file, 3)) = "PNG" Or _
UCase(Right(file, 3)) = "TIF" Or _
UCase(Right(file, 3)) = "JPG" Then
With Selection
.EndKey Unit:=wdStory
.InlineShapes.AddPicture Filename:=path & file, _
LinkToFile:=False, SaveWithDocument:=True
.InsertAfter vbCrLf & vbCrLf
.Collapse 0
.MoveLeft Unit:=wdCharacter, Count:=1
.Style = "Caption"
.Text = path & file
End With
End If
file = Dir()
Loop

Selection.EndKey Unit:=wdStory
End Sub

Here's what it does:
I have the workbook saved as macro-enabled (.xlsm)
I type Alt+F8, click on Run
"Run-time error "424": Object required"
Click on Debug
It brings me back to the module where CaptionLabels.Add Name:="Filename" is highlighted.

Any help would be appreciated!

Aflatoon
03-27-2018, 03:05 AM
This code is for Word, not Excel, which is why it doesn't work.

SamT
03-27-2018, 07:11 AM
CaptionLabels is not Declared

Dim CaptionLabels As Collection

This section is all about Word and does not apply in any way to Excel

With Selection
.EndKey Unit:=wdStory
.InlineShapes.AddPicture Filename:=path & file, _
LinkToFile:=False, SaveWithDocument:=True
.InsertAfter vbCrLf & vbCrLf
.Collapse 0
.MoveLeft Unit:=wdCharacter, Count:=1
.Style = "Caption"
.Text = path & file
End With

You need to tell us exactly what you have on the Excel Sheet and what you are trying to accomplish in Excel, before we can help you.

Aflatoon
03-27-2018, 07:24 AM
CaptionLabels is not Declared

Dim CaptionLabels As Collection


CaptionLabels is a Word object.

SamT
03-27-2018, 08:16 AM
Ahah.

missb
03-27-2018, 09:04 AM
Thank you!
I have a blank workbook, I'm trying to insert pictures with their filenames as text.