Log in

View Full Version : Auto generated userform from bookmarks



Prpikachu
02-16-2017, 06:04 AM
Hi,

I am thinking about creating some interactive userforms autogenerated from bookmarks, if possible.

The idea would be as follows:

- I would have a Word document with several bookmarks. The bookmarks' names would reflect their type (i.e. those which simply require a textbox asking for the corresponding content will start with "TB", those which require a yes/no response in a checkbox to keep or delete the bookmark content will start with "YN" or "CB", etc.).
- The userform to populate the word document should be autogenerated from its content.
- That is: the VBA code should go thorugh the list of bookmarks and, depending on its type (the initial TB, CB, etc.) should display a textbox (the label would reflect the rest of the name of the bookmark's name), a checkbox, etc.
- Then, the responses would populate the word document as expected (inserting answers from textboxes, deleting or keeping optional contant, etc.)

Does all of this make sense? It is possible to do something similar?

Ideally, the different textboxes, checkboxes, etc. should be somehow grouped, not just individually displayed, but I do not know if it is possible to display, let's say, 10 of them each time, with a Next button to go to the next page with 10 more...

Any idea that may help to understand if this is the best approch would be welcome.

Thank you!

gmaxey
02-16-2017, 08:52 AM
Creating userform controls from document bookmarks is not that much trouble. The trouble will be arranging your dynamic controls in a manner that makes sense to the user.


Sub Test()
Dim oFrm As UserForm1
Dim oBM As Bookmark
Dim oCtr As Control, oCtrLabel As Control
Dim lngCount As Long, dblTop As Long
Dim bAdded As Boolean
dblTop = 5.6
Set oFrm = New UserForm1
For Each oBM In ActiveDocument.Bookmarks
bAdded = False
Select Case Left(oBM.Name, 2)
Case "YN"
Set oCtr = oFrm.Controls.Add("Forms.CheckBox.1")
bAdded = True
Case "TB"
Set oCtr = oFrm.Controls.Add("Forms.TextBox.1")
bAdded = True
End Select
If bAdded Then
With oCtr
.Top = dblTop
.Height = 16.2
.Name = oBM.Name
dblTop = .Top + .Height + 5.6
End With
If TypeName(oCtr) <> "CheckBox" Then
Set oCtrLabel = oFrm.Controls.Add("Forms.Label.1")
With oCtrLabel
.Top = oCtr.Top
.Left = oCtr.Left + oCtr.Width + 5.6
.Caption = oCtr.Name
End With
Else
oCtr.Caption = oBM.Name
End If
End If
Next
oFrm.Show
End Sub

Prpikachu
02-16-2017, 12:38 PM
Wow, Greg!

Thank you very much!

I will try it and to build something on top of it. I am sure it will work perfectly!

Thanks again!