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!
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!