VBA code for unhiding a bookmarked text is not working - vba

I've created an ActiveX dropdown list and each option is linked to a bookmark for the text. Under the ActiveX controls there are the bookmarks (R1 andR2), hidden.
When I hit the btnselect button, all the other bookmarks, except the selected one, get deleted and the selected one becomes visible.
In the bookmark R2
I have a MacroButton for showing/hiding another text (CollapseMentiuniReclamant). When clicking the button it runs either Expand1 sub or Collapse1 sub, but the bookmark CollapseMentiuniReclamant doesn't show up.
I've simplified the document and codes as much as possible. Link to the document - https://wetransfer.com/downloads/1caea3c5d3b05e226e8b8f6a29760ad220190522071742/15db59.
The vba code is:
Private Sub btnselect_Click()
If ComboBox1.Value = "1" Then
Bookmarks("R1").Range.Font.Hidden = False
Bookmarks("R2").Range.Font.Hidden = False
Bookmarks("R2").Range.Delete
End If
If ComboBox1.Value = "2" Then
Bookmarks("R1").Range.Font.Hidden = False
Bookmarks("R1").Range.Delete
Bookmarks("R2").Range.Font.Hidden = False
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = True
End If
End Sub
Sub Expand1()
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Collapse1").Insert _
Where:=Selection.Range
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = False
End Sub
Sub Collapse1()
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Expand1").Insert _
Where:=Selection.Range
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = True
End Sub
Update: I've simplified the last part of code and the problem still persists:
Sub Expand1()
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = False
End Sub
I've even removed the button entirely and ran the macro from View Macros Tab and it's not working.
Why doesn't CollapseMentiuniReclamant show up?

It's not showing up because what you're trying to hide/unhide isn't inside the bookmarked range. In any event, you should be inserting/deleting the content, not simply toggling it's hidden property. Making something hidden is no guarantee it won't be seen or printed (even if not seen), as those settings depend on how the end user has Word configured.

Related

How do I hide/unhide a subform based on the input from two comboboxes?

I have a mainform, called TrainingsSU
In it I am calling subform qry_TrainingSU (built from the query of the same name)
I have it set up so that the records auto populate the subform based on the two comboboxes in the main form.
But what I need to do is hide the subform, and have it display only when the two comboboxes are populated and records are loaded.
Here's the current VBA (of which I am not really even a novice at)
Private Sub cbo_EmployeeLookup_AfterUpdate()
Me!qry_TrainingsSU.Requery
If Me.cbo_EmployeeLookup.Value = "" Then
Forms!qry_TrainingsSU.Visible = False
Else
Forms!qry_TrainingsSU = True
End If
End Sub
Private Sub cbo_TrainingName_AfterUpdate()
Me!qry_TrainingsSU.Requery
If Me.cbo_TrainingName.Value = "" Then
Forms!qry_TrainingsSU = False
Else
Forms!qry_TrainingsSU.Visible = True
End If
End Sub
I found the general form of this code in another answer, here: MS Access: Hide and Unhide subform based on drop down in Main Form
However the code doesn't seem to be working for me.
Currently the subform is set to Visible:No
So nothing shows up at all.
If I change that, it doesn't disappear and the empty subform is still visible.
This wouldn't be a problem, except I need to use this form for another query and want to layer them over eachother when the second subform is ready to be used.
Later this form will be used to push an UPDATE SET to a table based on the different subforms.
Is there something that is obviously wrong with the code, or did I miss a setting somewhere?
You can try this:
Private Sub updateStates()
Me!qry_TrainingsSU.Form.Requery
If (Me.cbo_EmployeeLookup.Value <> "" AND Me.cbo_TrainingName.Value <> "") Then
Me!qry_TrainingsSU.Visible = True
Else
Me!qry_TrainingsSU.Visible = False
End If
End Sub
Private Sub cbo_EmployeeLookup_AfterUpdate()
updateStates
End Sub
Private Sub cbo_TrainingName_AfterUpdate()
updateStates
End Sub
Forms!qry_TrainingsSU searches form opened as main form, not a subform.

Set a sub-form hidden field to visible, based on a check box status

C, Thank you for your input and encouragement! I have changed my form and script slightly, I am afraid I kept the if then statement as I am comfortable with the formatting. The script now works when the 'On Open'event runs.
Private Sub Form_Open(Cancel As Integer)
Me.ChkAlbumNotes.SetFocus
If Me.ChkAlbumNotes.Value = False Then
Me.lblAlbumNotes.Visible = False
Me.txtAlbumNotes.Visible = False
Me.btnAlbumNotes.Visible = True
Else
Me.lblAlbumNotes.Visible = True
Me.txtAlbumNotes.Visible = True
Me.btnAlbumNotes.Visible = False
End If
Me.TrackName.SetFocus
If Me.TrackName = " " Then
Me.btnAddRecord.SetFocus
Else
Me.btnNextRecord.SetFocus
End If
End Sub
This is fine when the form opens for the first time but I have a set of navigation buttons that are installed by the application as Macros. I cannot add my script to the On_Click event when the button is clicked, as On_Click is linked to the Macro. Is there a way to incorporate the script from the On_Load process to the pre-defined macro? Or can you suggest a neater way to achieve my requirements which are;
When the form opens,a check is made for the existence of a false value in the checkbox
if the check box is set to false, then the Notes Text Box and label are hidden and the notes button is visible.
If the check box has a true value, then the Notes text box and label are made visible and the button is hidden.
On completion of the test I check the field Track Name
if this is empty, I assume I am at the last record and give the Add New Record button the focus
If Track Name is not empty, then focus is set to Next Record button
when this button is clicked, the next record page opens and the process starts again.
Many Thanks
Mike
You should use the Form_Current event instead of Form_Open . This fires on starting the form (2 times) and everytime you move to another record.
Private Sub Form_Current()
Me.lblAlbumNotes.Visible = Me.ChkAlbumNotes.Value
Me.txtAlbumNotes.Visible = Me.ChkAlbumNotes.Value
Me.btnAlbumNotes.Visible = Not Me.ChkAlbumNotes.Value
If Me.TrackName = "" Then ' I suggest If Me.TrackName = " " being a typo and you want to check if empty ( that's why you should use vbNullString instead of "")
Me.btnAddRecord.SetFocus
Else
Me.btnNextRecord.SetFocus
End If
End Sub

Click event on one control sometimes does not fire

I have two buttons on a sheet. On occasion, clicking on one of the buttons does not "fire" the event.
Floating the cursor over the button changes the cursor from a cross to an arrow
The button does not appear to "depress" as it would if the event fired.
Breakpoint or Stop statement in the code do not get reached.
The other button never demonstrates this problem.
If the code is started manually, the code will run as designed.
In Designer Mode, the code appears to be attached to the button.
Closing and re-opening the worksheet, and then triggering the button that normally works will restore functionality to this button.
Trouble shooting advice would be appreciated.
The button code is below as is the properties window, but I think the problem lies elsewhere; just don't know where to look.
Again, another very similar button, which calls different code, seems to work OK all the time.
Thanks for any guidance.
Option Explicit
Private Sub cbDeleteViewed_Click()
With cbDeleteViewed
.Width = .Width
End With
Application.ScreenUpdating = False
Select Case Environ("COMPUTERNAME")
Case "RON-DODIER"
sDrive = "F:\"
sBasePath = "Videos"
ProtectDisable Sheet1
UnProtectEnable Sheet3
Sheet3.Select
Case "RONBP"
sDrive = "Z:\"
sBasePath = ""
ProtectDisable Sheet3
UnProtectEnable Sheet1
Sheet1.Select
Case Else
MsgBox "Cannot Run on This Computer"
Exit Sub
End Select
SetUpZ
DeleteViewedShows
Application.ScreenUpdating = True
End Sub

Word 2010 VBA make a table and text disappear

I am working on a word document and made a command button that is suppose to hide a table. Now when I first set it, I thought I got it working I got it all styled and titled and when I clicked the button the table would disappear.
Then I saved it and closed the document but when I opened up the document I saw that the only thing that was hidden was the words inside the table, the table lines are not hidden and when I toggle the button the only thing hiding is the text.
Is there something I am doing wrong ? Here is the code in VBA
Private Sub CommandButton1_Click()
ThisDocument.Styles("HideText").Font.Hidden = Not ThisDocument.Styles("HideText").Font.Hidden
End Sub
I just want the button to toggle the text and the Table to hide every time it the button is pressed and when the document is open and closed.
Update may be on to something the table has its own style as well. should I be targeting that as well as the text within the style ? is that what is happening ?
Update #2
I was able to now hide and unhide the section of the table I wanted but it doesn't bring up the lines after I make the table visible. So is there a way to get the table grid to show up with the click of the button?
here is what I have so far.
Private Sub CommandButton1_Click()
ThisDocument.Styles("HideText").Font.Hidden = Not ThisDocument.Styles("HideText").Font.Hidden
'Table Grid
Dim s As Style
Dim An As Integer
An = 0
If An = 0 Then
For Each s In ActiveDocument.Styles
If s.Type = wdStyleTypeTable Then
If s.NameLocal = "Table Grid" Then
Debug.Print (s.NameLocal)
s.Visibility = False
s.UnhideWhenUsed = False
Call s.Delete
End If
End If
Next
An = 1
End If
If An = 1 Then
For Each s In ActiveDocument.Styles
If s.Type = wdStyleTypeTable Then
If s.NameLocal = "Table Grid" Then
Debug.Print (s.NameLocal)
s.Visibility = True
s.UnhideWhenUsed = True
Call s.Delete
End If
End If
Next
An = 0
End If
End Sub
I'd approach this by hiding the font of the table (as below) rather than attempting to hide a specific font style which you're using within the table.
You could try something along the lines of:
Public sub CommandButton1_Click()
With ActiveDocument.Tables(1).Range.Font
.Hidden = Not .Hidden
End With
End Sub

Userform disable all fields

I have a userform that can be filled in but only if a previous userform was filled in as there are calculations in the userform based on the previous input and if those are empty, the calculations crash.
Now I did write a few if statements that check for these empty values and then had a brain flash.. how about if one of the fields is missing, the userform is just disabled. So thought, so done and it works :)
if DP1 = "" then
reportback.enable = false
else
end if
That is the form and it shows up beautifully and nothing can be changed, but Ohhh.. you canĀ“t even close the form, nothing works .. lol.
So my question. Is there a way to disable all fields from any input but still have the cancel button active ?
Private Sub Cancel_Click()
Unload reportback
End Sub
Use below code to disable all controls on your form to avoid the issue.
UserForm1 refers to name of Userform kindly replace accordingly.
Dim ctrl As Control
For Each ctrl In UserForm1.Controls
ctrl.Enabled = False
Next
Set ctrl = Nothing