i have 5 pages in multipage userform.
if the next button enabled, which it can be clicked by user then it should move to next hidden page, i always got an error "Object Required" it drives me crazy.
Private Sub btnGenerate_Click()
iPageNo = MultiPage1.Value + 1
MultiPage1.Pages(iPageNo).Visible = True
MultiPage1.Value = iPageNo
End Sub
that code seems doesnt work for me, any help would be appreciate.
Thanks
Which line is causing the error when you step thru?
Ensure there are enough existing pages. Also, has the name of the MultiPage object changed?
This code below tested working (2 Pages in MultiPage1, Page2 set hidden):
Option Explicit
Private Sub CommandButton1_Click()
Dim iNextPage As Long
With Me.MultiPage1
iNextPage = .Value + 1
If iNextPage < .Pages.Count Then
.Pages(iNextPage).Visible = True
.Value = iNextPage
End If
End With
End Sub
Related
I want to set up a VBA so that for any document based on a template hidden bookmarks are deleted prior to the document closing. We publish documents on our website. They are written as Word and an API converts them to html. If there are hidden bookmarks they appear as links on the website (the hidden bookmarks convert to html anchors). Currently we remove the bookmarks manual prior to the API, but this is time consuming (we publish 1000s of documents a year) and ineffective (people forget).
I found VBA to remove hidden bookmarks which works and tried to add DocumentBeforeClose as the trigger. But it doesn't work:
Private Sub DocumentBeforeClose(cancel As Boolean)
Dim nBK As Long
With ActiveDocument
For nBK = .Bookmarks.Count To 1 Step -1
If LCase(Left(.Bookmarks(nBK).Name, 3)) = "_hl" Then
.Bookmarks(nBK).Delete
End If
Next nBK
End With
ActiveDocument.Save
End Sub
I went through Visual Basic Window, Normal, Microsoft Word Objects, ThisDocument.
Nothing happens, the hidden bookmarks remain if I close and re-open the document.
I think you need to add this line :
.Bookmarks.ShowHidden = True
Like this it should work :
Private Sub DocumentBeforeClose(cancel As Boolean)
Dim nBK As Long
With ActiveDocument
.Bookmarks.ShowHidden = True
For nBK = .Bookmarks.Count To 1 Step -1
If LCase(Left(.Bookmarks(nBK).Name, 3)) = "_hl" Then
.Bookmarks(nBK).Delete
End If
Next nBK
End With
ActiveDocument.Save
End Sub
This has solved it:
Sub AutoClose()
On Error Resume Next
Dim nBK As Long
With ActiveDocument
.bookmarks.ShowHidden = True
For nBK = .bookmarks.Count To 1 Step -1
If LCase(Left(.bookmarks(nBK).Name, 3)) = "_hl" Then
.bookmarks(nBK).Delete
End If
Next nBK
End With
ActiveDocument.Save
End Sub
Only issue is that it tries to run when you open a document and puts up an error message that there is no active document. 'On error resume next' is to stop that error message
On Page 4 of the MultiPage form I've created a WebBrowser1 object. On Page 4 there are 2 buttons: one for msn.com, the other for google.com. If the UserForm defaults to Page 4 when opened, the buttons work fine initially, but if one of the other Pages is selected, and then the user returns to Page 4, clicking either one of the buttons causes the macro to crash with an error message 'Navigate' of object 'IWebBrowser2' failed.
Private Sub CommandButton23_Click()
Me.WebBrowser1.Navigate ("https://www.msn.com")
End Sub
Private Sub CommandButton24_Click()
Me.WebBrowser1.Navigate ("https://google.com")
End Sub
It appears that the WebBrowser needs to be "refreshed" each time Page 4 gets re/loaded. One solution is to eliminate the WebBrowser1 object from the UserForm and dynamically created a WebBrowser (wbr, below) initially, and then each time Page 4 is reselected.
Dim wbr As SHDocVw.WebBrowser
Private Sub MultiPage1_Change()
If MultiPage1.SelectedItem.Name = "Page 4" Then
Set wbr = Nothing
Set wbr = Me.MultiPage1.SelectedItem.Controls.Add("Shell.Explorer.2")
wbr.Height = 700
wbr.Left = 96
wbr.Top = 24
wbr.Width = 570
wbr.Navigate "About:Blank"
End If
End Sub
Private Sub UserForm_Initialize()
Set wbr = Me.MultiPage1.SelectedItem.Controls.Add("Shell.Explorer.2")
wbr.Height = 700
wbr.Left = 96
wbr.Top = 24
wbr.Width = 570
wbr.Navigate "About:Blank"
End Sub
I can't take credit for this solution - it was actually on another post in stackoverflow for a different problem!
Check it out:
Resizing WebBrowser Control on Excel UserForm with DPI
I have a problem with a MS Access (MSO 365 x64 v2004) form. In the detail section of the form I have a listbox with multiple selections enabled. The form has a footer ("frmOptions") which is hidden by default.
When having selected multiple items in the listbox and unhiding the footer (frmOptions.visible=true), all selections are cleared for some reason. This only occurs when setting the visibility of the footer to true.. Hiding the form footer does not seem to have any effect on the listbox selections. Off course I can write a function where the selections are restored again, but this should not be necessary. The listbox has no Beforeupdate/Afterupdate routine. The code to hide/unhide the footer is this:
Private Sub btn_options_Click()
If Me.frmOptions.Visible Then
Me.frmOptions.Visible = False
Else
Me.frmOptions.Visible = True
End If
End Sub
Is there something I have to consider in this code, maybe a setting of the listbox or form footer to prevent the unwanted de-selection in the listbox?
Thx for your help!
Art.
First, your code can be reduced to:
Private Sub btn_options_Click()
Me.frmOptions.Visible = Not Me.frmOptions.Visible
End Sub
Next, I can reproduce this, but can't tell why. Will try to find an explanation.
Most likely, this is by design for some reason and will not be changed in the near future, perhaps never. So you can just as well start programming the reselection of the listbox items.
I also can reproduce the behaviour and also think you would have to accept that.
A workaround is to resize the footer instead of hiding it.
Add a class variable mFooterHeight to the form and store the footers height during opening the form there.
Private mFrmOptionsInitialHeight As Long
Private Sub Form_Open(Cancel As Integer)
mFrmOptionsInitialHeight = Me.frmOptions.Height
End Sub
Then you can toggle the size of the footer with this line of code:
Me.frmOptions.Height = IIf(Me.frmOptions.Height = 0, mFrmOptionsInitialHeight, 0)
Thx for your replies! Too bad that this may be by design. I now solved this based on the following change in the form code and new function. Any improvements on the code are as always very welcome)
Changed procedure in the form code:
Private Sub btn_options_Click()
Dim varSelItems() As Variant
varSelItems = ListBoxItems(Me, "customer_contact_cat") 'Save the listbox selections
Me.frmOptions.Visible = Not Me.frmOptions.Visible
Call ListBoxItems(Me, "customer_contact_cat", varSelItems) 'Restore the listbox Selections
End Sub
Added function in a module so it may be reused in other forms:
Public Function ListBoxItems(frmName As Form, strLbName As String, Optional varSelItems As Variant) As Variant
Dim lngA As Long
Dim lngB As Long
Dim ctl As Control
Dim varItem As Variant
Dim varItems() As Variant
Set ctl = frmName.Controls(strLbName)
If Not IsArray(varSelItems) Then
ReDim varItems(ctl.ItemsSelected.Count)
lngA = 0
For Each varItem In ctl.ItemsSelected
varItems(lngA) = ctl.ItemData(varItem)
lngA = lngA + 1
Next varItem
ListBoxItems = varItems()
Else
For lngA = 0 To UBound(varSelItems) - 1
For lngB = 0 To ctl.ListCount - 1
If ctl.ItemData(lngB) = varSelItems(lngA) Then
ctl.Selected(lngB) = True
End If
Next lngB
Next lngA
End If
End Function
Is it possible to add a button on page one on a Userform with multipages so that it will take you to the other pages? Can sopmeone post a simple code for that?
Value property of the MultiPage Control, sets/gets the active page.
Private Sub cmdNext_Click()
Dim lCurrentPage As Long
lCurrentPage = Me.MultiPage1.Value + 1
If lCurrentPage = Me.MultiPage1.Pages.Count Then
MsgBox "You are on the last page."
Else
Me.MultiPage1.Value = lCurrentPage
End If
End Sub
I created a Userform (manually in the VBA Projectbrowser). I have written VBA code, which fills this Userform with different Objects in runtime (Labels, Optionbuttons etc.). So far everything worked fine
The Userform is filled with data read from my Excel sheets and correctly displayed. However I'm not able to read the inputs from the objects on it (for example Optionbutton - TRUE or FALSE). These objects do not appear anywhere (except on the userform) so that I can link them and use them in another Module.
I guess they are only displayed and not really read into the memory or whatever (initialized !?).
There are two ways to go about it.
WAY 1
Declare your option button object as Public.
Module Code
Public theOpBut As Object
Sub Fill()
If theOpBut.Value = True Then
ActiveSheet.Cells(1, 5) = 1
Else
ActiveSheet.Cells(1, 5) = "NO"
End If
End Sub
Userform Code
Private Sub UserForm_Initialize()
Set theOpBut = UserForm1.Controls.Add("Forms.optionbutton.1", "OptionButton", True)
With theOpBut
.Caption = "Test Button"
'.GroupName = OpButGroupCounter
.Top = 10
.Left = 20
.Height = 16
.Width = 50
.Font.Size = 12
.Font.Name = "Ariel"
End With
End Sub
Private Sub CommandButton1_Click()
Call Fill
End Sub
WAY 2
Declare a Boolean Variable and create a click event of the Option button and then set the value of the Boolean Variable in that click event. To create the click event of the Option button at Run Time, see THIS EXAMPLE
You can then check the value of Boolean Variable in Sub Fill() and act accordingly.