L
Legacy 300089
Guest
Hi,
I've been working on a macro to list files with hyperlinks from a selected folder and for images puts copy into a comment. Cobbled to together from various code snippets. However working with a large set of files I realised that images inserted into comments aren't compressed or resized based on the comment size.
So I'm looking for vba code to temporarily resize the image prior to inserting into the comment; ie before the .AddComment.Shape.Fill.UserPicture command.
Have spent hours trying to solve this with no luck. I think there are two potential solutions.
1) create a temporary Worksheet, add .Shapes, resize, delete worksheet (seems overkill and can't work out how to put a .Shape picutre into the .Comment)
2) create a temporary graphic file, resize, add via .UserPicture, delete (currently beyond my coding skills in vba excel)
Any other suggestions or help much appreciated!!!!
Full code with **** showing where resizing is needed:
Sub FolderFileNamesInColWithImgInComment()
' from GetFileNames macro from How to list files in a directory to worksheet in Excel?
Dim xRow As Long
Dim xDirect$, xFname$, InitialFoldr$
Dim p As Shape ' temp picture container
' On Error GoTo ErrHandler
InitialFoldr$ = Application.ActiveWorkbook.Path '<<< mod to start in workbook folder
' InitialFoldr$ = "C:\" '<<< Startup folder to begin searching from
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select a folder to list Files from"
.InitialFileName = InitialFoldr$ & "\"
.Show
If .SelectedItems.Count <> 0 Then
xDirect$ = .SelectedItems(1) & "\"
xFname$ = Dir(xDirect$, 7)
Do While xFname$ <> ""
ActiveCell.Offset(xRow) = xFname$
' mod to add images in comment, need to delete existing comment first
If Not (ActiveCell.Offset(xRow).Comment Is Nothing) Then ActiveCell.Offset(xRow).Comment.Delete
If FileIsImage(xDirect$ & xFname$) Then
With ActiveCell.Offset(xRow).AddComment
.Text xFname$
With .Shape
' ************* Bug to be fixed; need image resizing code here for large picture files inserted into comments
.Fill.UserPicture xDirect$ & xFname$
.ScaleHeight 3, msoFalse, msoScaleFromTopLeft
.ScaleWidth 3, msoFalse, msoScaleFromTopLeft
End With
End With
End If
xRow = xRow + 1
xFname$ = Dir
Loop
End If
End With
Exit Sub
ErrHandler:
MsgBox "A runtime error has occurred, please report the following:" _
& vbCrLf & vbCrLf _
& " Process : " & "FolderFileNamesInColWithImgInComment" _
& vbCrLf _
& " Error : " & Err & ": " & Error(Err), vbExclamation
Exit Sub
End Sub
' function used in macro
I've been working on a macro to list files with hyperlinks from a selected folder and for images puts copy into a comment. Cobbled to together from various code snippets. However working with a large set of files I realised that images inserted into comments aren't compressed or resized based on the comment size.
So I'm looking for vba code to temporarily resize the image prior to inserting into the comment; ie before the .AddComment.Shape.Fill.UserPicture command.
Have spent hours trying to solve this with no luck. I think there are two potential solutions.
1) create a temporary Worksheet, add .Shapes, resize, delete worksheet (seems overkill and can't work out how to put a .Shape picutre into the .Comment)
2) create a temporary graphic file, resize, add via .UserPicture, delete (currently beyond my coding skills in vba excel)
Any other suggestions or help much appreciated!!!!
Full code with **** showing where resizing is needed:
Sub FolderFileNamesInColWithImgInComment()
' from GetFileNames macro from How to list files in a directory to worksheet in Excel?
Dim xRow As Long
Dim xDirect$, xFname$, InitialFoldr$
Dim p As Shape ' temp picture container
' On Error GoTo ErrHandler
InitialFoldr$ = Application.ActiveWorkbook.Path '<<< mod to start in workbook folder
' InitialFoldr$ = "C:\" '<<< Startup folder to begin searching from
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select a folder to list Files from"
.InitialFileName = InitialFoldr$ & "\"
.Show
If .SelectedItems.Count <> 0 Then
xDirect$ = .SelectedItems(1) & "\"
xFname$ = Dir(xDirect$, 7)
Do While xFname$ <> ""
ActiveCell.Offset(xRow) = xFname$
' mod to add images in comment, need to delete existing comment first
If Not (ActiveCell.Offset(xRow).Comment Is Nothing) Then ActiveCell.Offset(xRow).Comment.Delete
If FileIsImage(xDirect$ & xFname$) Then
With ActiveCell.Offset(xRow).AddComment
.Text xFname$
With .Shape
' ************* Bug to be fixed; need image resizing code here for large picture files inserted into comments
.Fill.UserPicture xDirect$ & xFname$
.ScaleHeight 3, msoFalse, msoScaleFromTopLeft
.ScaleWidth 3, msoFalse, msoScaleFromTopLeft
End With
End With
End If
xRow = xRow + 1
xFname$ = Dir
Loop
End If
End With
Exit Sub
ErrHandler:
MsgBox "A runtime error has occurred, please report the following:" _
& vbCrLf & vbCrLf _
& " Process : " & "FolderFileNamesInColWithImgInComment" _
& vbCrLf _
& " Error : " & Err & ": " & Error(Err), vbExclamation
Exit Sub
End Sub
' function used in macro
Function FileIsImage(filename As String) As Boolean
' Excel VBA - If condition on image - Stack Overflow
' VBA changes to Pictures.Insert / Shape.AddPicture - Microsoft Community
' Test/Check if Shape Exists on Worksheet
Dim test As StdPicture
On Error GoTo ErrorHandler
Set test = LoadPicture(filename)
FileIsImage = True
Set test = Nothing ' 20140903 added to improve mem usage
Exit Function
ErrorHandler:
FileIsImage = False
Set test = Nothing ' 20140903 added to improve mem usage
On Error GoTo 0
End Function
' Excel VBA - If condition on image - Stack Overflow
' VBA changes to Pictures.Insert / Shape.AddPicture - Microsoft Community
' Test/Check if Shape Exists on Worksheet
Dim test As StdPicture
On Error GoTo ErrorHandler
Set test = LoadPicture(filename)
FileIsImage = True
Set test = Nothing ' 20140903 added to improve mem usage
Exit Function
ErrorHandler:
FileIsImage = False
Set test = Nothing ' 20140903 added to improve mem usage
On Error GoTo 0
End Function