AgentSmith2000

New Member
Joined
Sep 25, 2022
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Hello, Ive seen a few questions about adding controls at runtime but Im afraid I must be missing a few concepts. I have tried to adapt some code but the project Im working on is difficult to extrapolate the sample code to. I have the form creation down but cannot figure out adding items to a class module for use after creation. I know how to program these things work in a static userform for the most part but its a bit more difficult to get dynamically. Any help, direction would be great. Thank you.

The project objective:
1) Create multiple objects for viewing/adding PDFs at runtime based on user data on Sheet1.
2) View PDFs on userform with web browser,
3) Drop new pdfs in list view (panel) to update web browser,
4) Drop event also adds file to associated file directory,
5) After object creation during Initialize, loop through new web browsers to load existing URL currently from Sheet2,
6) Individual Command buttons updates bad load on web browsers,
7) double click web browser opens new large userform with WB following URL.
8) I wont get into the tree view requirements here but the tree view needs to print checked selection and go to selected item on double click (if possible); however, it must be able to reference class1 objects, which is why I mention here.

Issues:
1) Cannot get objects to add properly to class module to handle click, double click, and drop events.
2) I cannot loop through web browsers to load existing URL until objects are "available,"

Attempted Solutions:
1) Adding New Class Module from module or userform module at runtime,
2) Insert lines of code to UF module at runtime,

Sheet 1
1 Table of Contents
2 Schedule of Values
3 Unit Price Breakdown
4 General Conditions
5 Subcontractor Spread
6 Other Costs
7 Landscaping Trade Summary Proposal 1 Proposal 2 Proposal 3
8 Building Clean Trade Summary Proposal 1 Proposal 2 Proposal 3
9 Demo Trade Summary Proposal 1 Proposal 2 Proposal 3

Sheet2 - Sheet6 <blank>

UserForm1:
height 660
Width 950
has treeview called TreeView1
has frame called Frame1
has command button called Hide

Code:
Private Sub Initialize_()
Dim myForm As DocuBuilder
Dim myFrame As Control
Dim addLabel As Control
Dim addBTN As Control
Dim addLV As Control
Dim WB As Object
Dim x As String, y As String, z As String

'Create Object Code
Dim i, j, k, colcount, rowcount As Integer

rowcount = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row 'Application.WorksheetFunction.Count(Sheet1.Range("A:A"))

With Frame1
.ScrollHeight = (260 * rowcount)
End With

For i = 1 To rowcount
colcount = Sheet1.Cells(i, Columns.Count).End(xlToLeft).Column
x = Sheet1.Cells(i, 1).Text & " " & Sheet1.Cells(i, 2).Text 'Worksheets("Sheet1").Cells(i, 1).Text & " " & Worksheets("Sheet1").Cells(i, 2).Text
y = "C:\...\Test PDFs\1.pdf"

TreeView1.Nodes.Add Key:=Sheet1.Cells(i, 2).Text, Text:=Sheet1.Cells(i, 2).Text

If colcount < 3 Then

Set addLabel = Me.Frame1.Controls.Add("Forms.Label.1")
With addLabel
.Name = "Label" & 100 + i
.Top = (258 * i) - 228
.Left = 6
.Height = 15
.Width = Len(x) * 8
.Caption = x
.FontSize = 10
.FontBold = True
Sheet3.Cells(i, 1).Value = .Name 'This line and others like it are simply a way of mapping the name of the objects so I can make sure the naming convention works correctly, this can be commented
End With
Set WB = Me.Frame1.Controls.Add("Shell.Explorer.2")
With WB
.Name = "WebBrowser" & 100 + i
.Top = (258 * i) - 210
.Left = 6
.Height = 220
.Width = 170
.RegisterAsBrowser = True
.RegisterAsDropTarget = True
.Navigate "about:blank"
'.Navigate2 y
.Offline = True
Sheet4.Cells(i, 1).Value = .Name
End With
Set addLV = Me.Frame1.Controls.Add("MSComctlLib.ListViewCtrl.2")
With addLV
.Name = "ListView" & 100 + i
.Top = (258 * i) - 210
.Left = 176
.Height = 220
.Width = 10
.Appearance = 0
.BackColor = &H80000004
.BorderStyle = 0
.OLEDragMode = 0
.OLEDropMode = 1
Sheet5.Cells(i, 1).Value = .Name
'.Visible = False
End With
Set addBTN = Me.Frame1.Controls.Add("Forms.CommandButton.1")
With addBTN
.Name = "CommandButton" & 100 + i
.Top = (258 * i) - 228
.Left = 130
.Height = 18
.Width = 45
.Caption = "Update"
.FontSize = 8
.FontBold = False
Sheet6.Cells(i, 1).Value = .Name
End With

Else

Set addLabel = Me.Frame1.Controls.Add("Forms.Label.1")
With addLabel
.Name = "Label" & 500 + i
.Top = (258 * i) - 240
.Left = 6
.Height = 15
.Width = Len(x) * 10
.Caption = x
.FontSize = 10
.FontBold = True
Sheet3.Cells(i, 1).Value = .Name
End With

For j = 3 To colcount
z = Worksheets("Sheet1").Cells(i, j).Text

'CHILD LOOP
TreeView1.Nodes.Add Sheet1.Cells(i, 2).Text, tvwChild, CStr(one), Sheet1.Cells(i, j).Text

Set addLabel = Me.Frame1.Controls.Add("Forms.Label.1")
With addLabel
.Name = "Label" & 100 + i & j
.Top = (258 * i) - 222
.Left = (184 * (j - 2)) - 174 'The j-2 is due to column 3 that these items first appear on
.Height = 10
.Width = 80
.Caption = z
.FontSize = 8
.FontBold = True
Sheet3.Cells(i, j).Value = .Name
End With

Set WB = Me.Frame1.Controls.Add("Shell.Explorer.2")
With WB
.Name = "WebBrowser" & 100 + i & j
.Top = (258 * i) - 210
.Left = (184 * (j - 2)) - 174
.Height = 220
.Width = 170
.RegisterAsBrowser = True
.RegisterAsDropTarget = True
.Navigate "about:blank"
'.Navigate2 y
.Offline = True
Sheet4.Cells(i, j).Value = .Name
End With

Set addLV = Me.Frame1.Controls.Add("MSComctlLib.ListViewCtrl.2")
With addLV
.Name = "ListView" & 100 + i & j
.Top = (258 * i) - 210
.Left = (184 * (j - 2)) - 174 + 170
.Height = 220
.Width = 10
.Appearance = 0
.BackColor = &H80000004
.BorderStyle = 0
.OLEDragMode = 0
.OLEDropMode = 1
'.Visible = False
Sheet5.Cells(i, j).Value = .Name
End With

Set addBTN = Me.Frame1.Controls.Add("Forms.CommandButton.1")
With addBTN
.Name = "CommandButton" & 100 + i & j
.Top = (258 * i) - 230
.Left = (184 * (j - 2)) - 53
.Height = 18
.Width = 50
.Caption = "Update"
.FontSize = 8
.FontBold = False
Sheet6.Cells(i, j).Value = .Name
End With

Next j

End If

Next i


End Sub

UserForm2
Label called FileViewLBL
Web browser called FileViewWB
 

Attachments

  • Userform Parent and Child.JPG
    Userform Parent and Child.JPG
    136.1 KB · Views: 42

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Tried to fix tab indent...

VBA Code:
Private Sub Initialize_()
Dim myForm As DocuBuilder
Dim myFrame As Control
Dim addLabel As Control
Dim addBTN As Control
Dim addLV As Control
Dim WB As Object
Dim x As String, y As String, z As String

'Create Object Code
Dim i, j, k, colcount, rowcount As Integer

rowcount = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row 'Application.WorksheetFunction.Count(Sheet1.Range("A:A"))

With Frame1
    .ScrollHeight = (260 * rowcount)
End With

For i = 1 To rowcount
    colcount = Sheet1.Cells(i, Columns.Count).End(xlToLeft).Column
    x = Sheet1.Cells(i, 1).Text & " " & Sheet1.Cells(i, 2).Text 'Worksheets("Sheet1").Cells(i, 1).Text & " " & Worksheets("Sheet1").Cells(i, 2).Text
    y = "C:\...\Test PDFs\1.pdf"

    TreeView1.Nodes.Add Key:=Sheet1.Cells(i, 2).Text, Text:=Sheet1.Cells(i, 2).Text

    If colcount < 3 Then

        Set addLabel = Me.Frame1.Controls.Add("Forms.Label.1")
            With addLabel
                .Name = "Label" & 100 + i
                .Top = (258 * i) - 228
                .Left = 6
                .Height = 15
                .Width = Len(x) * 8
                .Caption = x
                .FontSize = 10
                .FontBold = True
                Sheet3.Cells(i, 1).Value = .Name 'This line and others like it are simply a way of mapping the name of the objects so I can make sure the naming convention works correctly, this can be commented
            End With
        Set WB = Me.Frame1.Controls.Add("Shell.Explorer.2")
            With WB
                .Name = "WebBrowser" & 100 + i
                .Top = (258 * i) - 210
                .Left = 6
                .Height = 220
                .Width = 170
                .RegisterAsBrowser = True
                .RegisterAsDropTarget = True
                .Navigate "about:blank"
                '.Navigate2 y
                .Offline = True
                Sheet4.Cells(i, 1).Value = .Name
            End With
        Set addLV = Me.Frame1.Controls.Add("MSComctlLib.ListViewCtrl.2")
            With addLV
                .Name = "ListView" & 100 + i
                .Top = (258 * i) - 210
                .Left = 176
                .Height = 220
                .Width = 10
                .Appearance = 0
                .BackColor = &H80000004
                .BorderStyle = 0
                .OLEDragMode = 0
                .OLEDropMode = 1
                Sheet5.Cells(i, 1).Value = .Name
            End With
        Set addBTN = Me.Frame1.Controls.Add("Forms.CommandButton.1")
            With addBTN
                .Name = "CommandButton" & 100 + i
                .Top = (258 * i) - 228
                .Left = 130
                .Height = 18
                .Width = 45
                .Caption = "Update"
                .FontSize = 8
                .FontBold = False
                Sheet6.Cells(i, 1).Value = .Name
            End With
    Else
        Set addLabel = Me.Frame1.Controls.Add("Forms.Label.1")
            With addLabel
                .Name = "Label" & 500 + i
                .Top = (258 * i) - 240
                .Left = 6
                .Height = 15
                .Width = Len(x) * 10
                .Caption = x
                .FontSize = 10
                .FontBold = True
                Sheet3.Cells(i, 1).Value = .Name
            End With

        For j = 3 To colcount
            z = Worksheets("Sheet1").Cells(i, j).Text

            'CHILD LOOP
            TreeView1.Nodes.Add Sheet1.Cells(i, 2).Text, tvwChild, CStr(one), Sheet1.Cells(i, j).Text

            Set addLabel = Me.Frame1.Controls.Add("Forms.Label.1")
                    With addLabel
                    .Name = "Label" & 100 + i & j
                    .Top = (258 * i) - 222
                    .Left = (184 * (j - 2)) - 174 'The j-2 is due to column 3 that these items first appear on
                    .Height = 10
                    .Width = 80
                    .Caption = z
                    .FontSize = 8
                    .FontBold = True
                    Sheet3.Cells(i, j).Value = .Name
                End With
            Set WB = Me.Frame1.Controls.Add("Shell.Explorer.2")
                With WB
                    .Name = "WebBrowser" & 100 + i & j
                    .Top = (258 * i) - 210
                    .Left = (184 * (j - 2)) - 174
                    .Height = 220
                    .Width = 170
                    .RegisterAsBrowser = True
                    .RegisterAsDropTarget = True
                    .Navigate "about:blank"
                    '.Navigate2 y
                    .Offline = True
                    Sheet4.Cells(i, j).Value = .Name
                End With
            Set addLV = Me.Frame1.Controls.Add("MSComctlLib.ListViewCtrl.2")
                    With addLV
                    .Name = "ListView" & 100 + i & j
                    .Top = (258 * i) - 210
                    .Left = (184 * (j - 2)) - 174 + 170
                    .Height = 220
                    .Width = 10
                    .Appearance = 0
                    .BackColor = &H80000004
                    .BorderStyle = 0
                    .OLEDragMode = 0
                    .OLEDropMode = 1
                    Sheet5.Cells(i, j).Value = .Name
                End With
            Set addBTN = Me.Frame1.Controls.Add("Forms.CommandButton.1")
                With addBTN
                    .Name = "CommandButton" & 100 + i & j
                    .Top = (258 * i) - 230
                    .Left = (184 * (j - 2)) - 53
                    .Height = 18
                    .Width = 50
                    .Caption = "Update"
                    .FontSize = 8
                    .FontBold = False
                    Sheet6.Cells(i, j).Value = .Name
                End With
        Next j
    End If
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,135
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top