Sub InsertPicture()
Dim fName As String: fName = "[COLOR=#2f4f4f]C:\Test\jpgFiles\[I]Image1[/I].jpg[/COLOR]"
Dim cel As Range: Set cel = Range("[COLOR=#ff0000]E3[/COLOR]")
On Error Resume Next
cel.Comment.Delete
cel.AddComment
With cel.Comment.Shape
.Fill.UserPicture fName
.Height = cel.Height
.Width = cel.Width
End With
End Sub
Private Sub Image1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim N As String, T As Double, Pic As Object, Cel As Range
N = "[I][COLOR=#ff0000]C:\Test\folder\Image1.jpg[/COLOR][/I]" '[COLOR=#006400][I]Amend this[/I][/COLOR]
T = Timer
Set Pic = Image1
Set Cel = Range("[COLOR=#0000ff]E5[/COLOR]")
Call SetProperties(Cel, Pic)
Pic.Picture = LoadPicture(N)
Do While Timer < (T + [COLOR=#0000ff]1[/COLOR]): DoEvents: Loop
Pic.Picture = LoadPicture("")
End Sub
Private Sub SetProperties(Cel As Range, Obj As Object)
[COLOR=#0000ff] Cel.ColumnWidth = 20
Cel.RowHeight = 50[/COLOR]
With Obj
.Left = Cel.Left
.Top = Cel.Top
.Width = Cel.Width
.Height = Cel.Height
.PictureSizeMode = fmPictureSizeModeStretch
End With
End Sub
Option Explicit
Private Type POINTAPI
X As Long
Y As Long
End Type
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL] VBA7 Then
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL]
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL] If
Private WithEvents oCmndBars As CommandBars
Private sTargetRangeAddr As String, sImageFileName As String
Public Sub AddPopUpImageToRange(ByVal Rng As Range, ByVal ImageFileName As String)
sTargetRangeAddr = Rng.Address(, , , True)
sImageFileName = ImageFileName
If Len(Dir(ImageFileName)) = 0 Then Err.Raise Number:=vbObjectError + 513, Description:="Unable to find file : '" & ImageFileName & "'"
Set oCmndBars = Application.CommandBars
Call oCmndBars_OnUpdate
End Sub
Private Sub oCmndBars_OnUpdate()
Dim oImage As Shape, tCurPos As POINTAPI
Application.CommandBars.FindControl(ID:=2040).Enabled = Not Application.CommandBars.FindControl(ID:=2040).Enabled
GetCursorPos tCurPos
On Error Resume Next
If Union(ActiveWindow.RangeFromPoint(tCurPos.X, tCurPos.Y), Range(sTargetRangeAddr)).Address(, , , True) = sTargetRangeAddr Then
If Err.Number = 0 Then
Set oImage = Range(sTargetRangeAddr).Parent.Shapes.AddPicture _
(Filename:=sImageFileName, LinktoFile:=msoFalse, SaveWithDocument:=msoTrue, _
Left:=Range(sTargetRangeAddr).Left, Top:=Range(sTargetRangeAddr).Top, _
Width:=Range(sTargetRangeAddr).Width, Height:=Range(sTargetRangeAddr).Height)
With oImage
.Name = sTargetRangeAddr
.Visible = True
End With
End If
Else
ActiveSheet.Shapes(sTargetRangeAddr).Delete
End If
End Sub
Option Explicit
Private oImageGenerator1 As New C_ImageGenerator
Private oImageGenerator2 As New C_ImageGenerator
Private oImageGenerator3 As New C_ImageGenerator
Sub AddPopUps()
Call oImageGenerator1.AddPopUpImageToRange _
(Rng:=[COLOR=#ff0000]Sheet1[/COLOR][COLOR=#0000ff].Range("B2:F10")[/COLOR], ImageFileName:="C:\Users\Test\[COLOR=#ff0000]Image1[/COLOR].bmp")
Call oImageGenerator2.AddPopUpImageToRange _
(Rng:=[COLOR=#ff0000]Sheet1[/COLOR][COLOR=#0000ff].Range("G20")[/COLOR], ImageFileName:="C:\Users\Test\[COLOR=#ff0000]Image2[/COLOR].bmp")
Call oImageGenerator3.AddPopUpImageToRange _
(Rng:=[COLOR=#ff0000]Sheet2[/COLOR][COLOR=#0000ff].Range("A6")[/COLOR], ImageFileName:="C:\Users\Test\[COLOR=#ff0000]Image3[/COLOR].bmp")
End Sub
Sub RemovePopUps()
Set oImageGenerator1 = Nothing
Set oImageGenerator2 = Nothing
Set oImageGenerator3 = Nothing
End Sub
Private Sub Workbook_Open()
Call AddPopUps
End Sub
It would be useful if the user could click on the image to trigger another macro
- how could that functionality be added?
Option Explicit
Private Type POINTAPI
X As Long
Y As Long
End Type
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL] VBA7 Then
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL]
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL] If
Private WithEvents oCmndBars As CommandBars
Private sTargetRangeAddr As String, sImageFileName As String
Private sMacroName As String
Public Sub AddPopUpImageToRange(ByVal Rng As Range, ByVal ImageFileName As String, Optional ClickMacro As String)
sTargetRangeAddr = Rng.Address(, , , True)
sImageFileName = ImageFileName
sMacroName = ClickMacro
If Len(Dir(ImageFileName)) = 0 Then Err.Raise Number:=vbObjectError + 513, Description:="Unable to find file : '" & ImageFileName & "'"
Set oCmndBars = Application.CommandBars
Call oCmndBars_OnUpdate
End Sub
Private Sub oCmndBars_OnUpdate()
Dim oImage As Shape, tCurPos As POINTAPI
Application.CommandBars.FindControl(ID:=2040).Enabled = Not Application.CommandBars.FindControl(ID:=2040).Enabled
GetCursorPos tCurPos
On Error Resume Next
If Union(ActiveWindow.RangeFromPoint(tCurPos.X, tCurPos.Y), Range(sTargetRangeAddr)).Address(, , , True) = sTargetRangeAddr Then
If Err.Number = 0 Then
Set oImage = Range(sTargetRangeAddr).Parent.Shapes.AddPicture _
(Filename:=sImageFileName, LinktoFile:=msoFalse, SaveWithDocument:=msoTrue, _
Left:=Range(sTargetRangeAddr).Left, Top:=Range(sTargetRangeAddr).Top, _
Width:=Range(sTargetRangeAddr).Width, Height:=Range(sTargetRangeAddr).Height)
With oImage
.Name = sTargetRangeAddr
.Visible = True
If Len(sMacroName) Then .OnAction = "'" & sMacroName & Chr(34) & "IMAGE_" & Split(.Name, "]")(1) & Chr(34) & "'"
End With
End If
Else
ActiveSheet.Shapes(sTargetRangeAddr).Delete
End If
End Sub
Option Explicit
Private oImageGenerator1 As New C_ImageGenerator
Private oImageGenerator2 As New C_ImageGenerator
Private oImageGenerator3 As New C_ImageGenerator
Sub AddPopUps()
Call oImageGenerator1.AddPopUpImageToRange _
(Rng:=Sheet1.Range("B2:F10"), ImageFileName:="C:\Users\Info-Hp\Pictures\Image1.bmp", ClickMacro:="Macro")
Call oImageGenerator2.AddPopUpImageToRange _
(Rng:=Sheet1.Range("G20"), ImageFileName:="C:\Users\Info-Hp\Pictures\Image2.bmp", ClickMacro:="Macro")
Call oImageGenerator3.AddPopUpImageToRange _
(Rng:=Sheet2.Range("A6"), ImageFileName:="C:\Users\Info-Hp\Pictures\Image3.bmp")
End Sub
Sub RemovePopUps()
Set oImageGenerator1 = Nothing
Set oImageGenerator2 = Nothing
Set oImageGenerator3 = Nothing
End Sub
Sub Macro(ByVal ImageName As String)
MsgBox "You Clicked :" & vbNewLine & vbNewLine & ImageName, vbInformation
End Sub
Option Explicit
Private Type POINTAPI
X As Long
Y As Long
End Type
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL] VBA7 Then
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL]
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL] If
Private WithEvents oCmndBars As CommandBars
Private sTargetRangeAddr As String, sImageFileName As String
Private sMacroName As String
Public Sub AddPopUpImageToRange(ByVal Rng As Range, ByVal ImageFileName As String, Optional ClickMacro As String)
sTargetRangeAddr = Rng.Address(, , , True)
sImageFileName = ImageFileName
sMacroName = ClickMacro
If Len(Dir(ImageFileName)) = 0 Then Err.Raise Number:=vbObjectError + 513, Description:="Unable to find file : '" & ImageFileName & "'"
Set oCmndBars = Application.CommandBars
Call oCmndBars_OnUpdate
End Sub
Private Sub oCmndBars_OnUpdate()
Dim oImage As Shape, tCurPos As POINTAPI
Application.CommandBars.FindControl(ID:=2040).Enabled = Not Application.CommandBars.FindControl(ID:=2040).Enabled
GetCursorPos tCurPos
On Error Resume Next
If Union(ActiveWindow.RangeFromPoint(tCurPos.X, tCurPos.Y), Range(sTargetRangeAddr)).Address(, , , True) = sTargetRangeAddr Then
If Err.Number = 0 Then
Set oImage = Range(sTargetRangeAddr).Parent.Shapes.AddPicture _
(Filename:=sImageFileName, LinktoFile:=msoFalse, SaveWithDocument:=msoTrue, _
Left:=Range(sTargetRangeAddr).Left, Top:=Range(sTargetRangeAddr).Top, _
Width:=Range(sTargetRangeAddr).Width, Height:=Range(sTargetRangeAddr).Height)
With oImage
.Name = sTargetRangeAddr
.Visible = True
If Len(sMacroName) Then .OnAction = "'" & sMacroName & Chr(34) & "IMAGE_" & Split(.Name, "]")(1) & Chr(34) & "'"
End With
End If
End If
If ActiveWindow.RangeFromPoint(tCurPos.X, tCurPos.Y).Name <> sTargetRangeAddr Then
ActiveSheet.Shapes(sTargetRangeAddr).Delete
End If
End Sub
Option Explicit
Private oImageGenerator1 As New C_ImageGenerator
Private oImageGenerator2 As New C_ImageGenerator
Private oImageGenerator3 As New C_ImageGenerator
Sub AddPopUps()
Call oImageGenerator1.AddPopUpImageToRange _
(Rng:=Sheet1.Range("B2:F10"), ImageFileName:="C:\Users\Info-Hp\Pictures\Image1.bmp", ClickMacro:="Macro")
Call oImageGenerator2.AddPopUpImageToRange _
(Rng:=Sheet1.Range("G20"), ImageFileName:="C:\Users\Info-Hp\Pictures\Image2.bmp", ClickMacro:="Macro")
Call oImageGenerator3.AddPopUpImageToRange _
(Rng:=Sheet2.Range("A6"), ImageFileName:="C:\Users\Info-Hp\Pictures\Image3.bmp")
End Sub
Sub RemovePopUps()
Set oImageGenerator1 = Nothing
Set oImageGenerator2 = Nothing
Set oImageGenerator3 = Nothing
End Sub
Sub Macro(ByVal ImageName As String)
MsgBox "You Clicked :" & vbNewLine & vbNewLine & ImageName, vbInformation
End Sub
Option Explicit
Private Type POINTAPI
X As Long
Y As Long
End Type
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL] VBA7 Then
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL]
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL] If
Private WithEvents oCmndBars As CommandBars
Private sTargetRangeAddr As String, sImageFileName As String
Private sMacroName As String
Public Sub AddPopUpImageToRange(ByVal Rng As Range, ByVal ImageFileName As String, Optional ClickMacro As String)
sTargetRangeAddr = Rng.Address(, , , True)
sImageFileName = ImageFileName
sMacroName = ClickMacro
If Len(Dir(ImageFileName)) = 0 Then Err.Raise Number:=vbObjectError + 513, Description:="Unable to find file : '" & ImageFileName & "'"
Set oCmndBars = Application.CommandBars
Call oCmndBars_OnUpdate
End Sub
Private Sub oCmndBars_OnUpdate()
Dim oImage As Shape, tCurPos As POINTAPI
Application.CommandBars.FindControl(ID:=2040).Enabled = Not Application.CommandBars.FindControl(ID:=2040).Enabled
GetCursorPos tCurPos
On Error Resume Next
If Union(ActiveWindow.RangeFromPoint(tCurPos.X, tCurPos.Y), Range(sTargetRangeAddr)).Address(, , , True) = sTargetRangeAddr Then
If Err.Number = 0 Then
Set oImage = Range(sTargetRangeAddr).Parent.Shapes.AddPicture _
(Filename:=sImageFileName, LinktoFile:=msoFalse, SaveWithDocument:=msoTrue, _
Left:=Range(sTargetRangeAddr).Left, Top:=Range(sTargetRangeAddr).Top, _
Width:=Range(sTargetRangeAddr).Width, Height:=Range(sTargetRangeAddr).Height)
With oImage
.Name = sTargetRangeAddr
.Visible = True
.OnAction = "'" & sMacroName & Chr(34) & "IMAGE_" & Split(.Name, "]")(1) & Chr(34) & "'"
End With
End If
End If
If ActiveWindow.RangeFromPoint(tCurPos.X, tCurPos.Y).Name <> sTargetRangeAddr Then
ActiveSheet.Shapes(sTargetRangeAddr).Delete
End If
End Sub
Option Explicit
Private oImageGenerator1 As New C_ImageGenerator
Private oImageGenerator2 As New C_ImageGenerator
Private oImageGenerator3 As New C_ImageGenerator
Sub AddPopUps()
Call oImageGenerator1.AddPopUpImageToRange _
(Rng:=Sheet1.Range("B2:F10"), ImageFileName:="C:\Users\Info-Hp\Pictures\Image1.bmp", ClickMacro:="Macro")
Call oImageGenerator2.AddPopUpImageToRange _
(Rng:=Sheet1.Range("G20"), ImageFileName:="C:\Users\Info-Hp\Pictures\Image2.bmp", ClickMacro:="Macro")
Call oImageGenerator3.AddPopUpImageToRange _
(Rng:=Sheet2.Range("A6"), ImageFileName:="C:\Users\Info-Hp\Pictures\Image3.bmp")
End Sub
Sub RemovePopUps()
Set oImageGenerator1 = Nothing
Set oImageGenerator2 = Nothing
Set oImageGenerator3 = Nothing
End Sub
Sub Macro(ByVal ImageName As String)
MsgBox "You Clicked :" & vbNewLine & vbNewLine & ImageName, vbInformation
End Sub