问题
I have multiple labels created dynamically on Userform. I want to add a hyperlink to the created labels, is there a way I could add hyperlink to these labels. Here is the code of how I created the labels dynamically.
Private Sub cmdViewReports_Click()
Dim row_num As Long
Dim fso As Object
Dim src_path As String
Dim dest_path As String
Dim sub_folder As String
Dim theLabel1 As msforms.Label
Dim inc As Integer
Dim my_files As Object
Dim my_folder As Object
Dim i As Integer
Dim ctrl As Control
'Check if the record is selected in listbox
If Selected_List = 0 Then
MsgBox "No record is selected.", vbOKOnly + vbInformation, "Upload Results"
Exit Sub
End If
'Folder Name to be created as per the 3rd column value in the list
sub_folder = Me.lstDb.List(Me.lstDb.ListIndex, 3)
sub_folder = Replace(sub_folder, "/", "_")
dest_path = "C:\abc\xyz\Desktop\FV\" & sub_folder & "\"
Set fso = CreateObject("scripting.filesystemobject")
If Not fso.FolderExists(dest_path) Then
MsgBox "No reports are loaded"
Exit Sub
End If
Set my_folder = fso.GetFolder(dest_path)
Set my_files = my_folder.Files
i = 1
For Each oFiles In my_files
Set theLabel1 = Me.Frame1.Controls.Add("Forms.Label.1", "File_name" & i, True)
With theLabel1
.Caption = oFiles.Name
.Left = 1038
.Width = 60
.Height = 12
.Top = 324 + inc
.TextAlign = 1
.BackColor = &HC0FFFF
.BackStyle = 0
.BorderStyle = 1
.BorderStyle = 0
'.Locked = True
.ForeColor = &H8000000D
.Font.Size = 9
.Font.Underline = True
.Visible = True
End With
inc = inc + 12
i = i + 1
Next
End Sub
here's how the part of the form looks like
To give a brief of my use case: I have some files/reports (pdf,word etc..) that I need to attach to a record. User can attach their reports to the records and also view reports if they are attached. So with the above code I am able to generate the labels with the files inside the folder; now when the file names are displayed on the form, I want a functionality where is I click the report(label) I want that report to open.
Thanks in Advance !!!
回答1:
You can use most of the code in this answer with only slight modification. You will need to modify the MyControl class to use Labels instead of CommandButtons. You will also need to modify the event to pass the file name.
Once these modifications are complete, your code is pretty much the same, too. Here is your original code, simplified and modified to illustrate the concept:
UserForm
Option Explicit
Private WithEvents MyNotifier As Notifier
Private MyControls As Collection
Private Sub UserForm_Initialize()
Set MyNotifier = GetNotifier()
Set MyControls = New Collection
End Sub
Private Sub CommandButton1_Click()
Dim i As Integer
Dim inc As Integer
Dim theLabel1 As MSForms.Label
Dim mc As MyControl
inc = 0
For i = 1 To 2
Set theLabel1 = Me.Frame1.Controls.Add("Forms.Label.1", "File_name" & i, True)
With theLabel1
.Caption = "filename" & i
.Left = 100
.Width = 60
.Height = 12
.Top = 20 + inc
.TextAlign = 1
.BackColor = &HC0FFFF
.BackStyle = 0
.BorderStyle = 1
.BorderStyle = 0
'.Locked = True
.ForeColor = &H8000000D
.Font.Size = 9
.Font.Underline = True
.Visible = True
End With
Set mc = New MyControl
mc.Add theLabel1
MyControls.Add mc
inc = inc + 12
Next
End Sub
Private Sub MyNotifier_Click(ByVal Filename As String)
MsgBox Filename
End Sub
And here is the modified support files for quick reference:
Module
Option Explicit
Private m_Notifier As Notifier
Public Function GetNotifier() As Notifier
If m_Notifier Is Nothing Then Set m_Notifier = New Notifier
Set GetNotifier = m_Notifier
End Function
Notifier Class
Option Explicit
Public Event Click(ByVal Filename As String)
Public Function Click(ByVal Filename As String)
RaiseEvent Click(Filename)
End Function
MyControl Class
Option Explicit
Private MyNotifier As Notifier
Private WithEvents MyLabel As MSForms.Label
Public Sub Add(ByVal c As MSForms.Label)
Set MyNotifier = GetNotifier()
Set MyLabel = c
End Sub
Private Sub MyLabel_Click()
MyNotifier.Click MyLabel.Caption
End Sub
回答2:
I suggest you to use the following code for each Label:
Private Sub Label1_Click()
ActiveWorkbook.FollowHyperlink Label1.Caption
End Sub
- ---> The Function you need is
FollowHyperlink
from theActiveWorkbook
.
You need to make sure that:
- Label1 is the Label
- The Caption of each Label is a valid URL Hyperlink
- For better format, you need to make sure that the Label is well formatted as a Hyperlink
Once the user click on the Label, it should lead directly to the Default browser with the given Link.
Hope it will help you!
[Edition: Just formated the answer better]
来源:https://stackoverflow.com/questions/60994159/adding-hyperlink-to-dynamically-created-label-in-vba-excel