How to hide unchecked checkboxes when printing - vba

I am working on creating a contract specification page in Word 2013 with checkboxes so that my boss can click each box that he wants to include in the final printed contract, and hide the ones he doesn't need. I'm completely new to VBA but I know that I need to use it to achieve this. From searching the internet I've used bookmarks and the code below:
Private Sub CheckBox1_Click()
If CheckBox1.Value = False Then
ActiveDocument.Bookmarks("Work1").Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("Work1").Range.Font.Hidden = False
End If
End Sub
But this code seems to hide the checkboxes before I can click them. I would like the checkboxes to stay visible until they print, in case my boss needs to make a change. I also tried using another code, but it also didn't work the way I wanted it to:
Private Sub CheckBox1_Click()
If CheckBox1.Value = False Then
ActiveDocument.Bookmarks("Work1").Application.Options.PrintHiddenText = False
Else
ActiveDocument.Bookmarks("Work1").Application.Options.PrintHiddenText = True
End If
End Sub
I would also like to make it so there are no gaps where the unused checkboxes would be. Any help would be greatly appreciated!!

I seem to have overcomplicated the issue yesterday. Your only problem was that your checkboxes turn unvisible along with the text. You just need to bookmark exactly the text you want to hide/show leaving the checkbox (perceived by Word as a part of the text) outside of the bookmark.
Your code will do this fine, or you can replace it with this:
Private Sub CheckBox1_Click()
Bookmarks("Work1").Range.Font.Hidden = Not CheckBox1
End Sub
By the way, if you make Word show paragraph marks (Ctrl + *), you will be able to see hidden text.

I would suggest your macros:
a) save the current document (in the temporary folder if you want)
b) delete all unchecked boxes
c) print the document
d) close the document
e) open the file saved in the point a).
Should take a dozen lines of code or so.

Related

Hide Table in Word using VBA

I have a series of Tables in a Word document that depending on the situation may need to be filled out. To make the document look clean, I want to allow users to use a check-box to determine if the associated table is relevant or not. On a click of the check-box, the entire table should disappear. I've looked at some solutions available, but none provide consistent results.
I've tried the following:
Sub Hide()
With ActiveDocument.Bookmarks("Test").Range.Tables(1).Range.Font
If .Hidden = True Then
.Hidden = False
Else
.Hidden = True
End If
End With
End Sub
I assign the "Test" value to the Table in question. When I run the macro, the Table will disappear.
However, the next step is have the table disappear when clicking the Content Control Check-box. I am not sure how to structure that
After much playing around, I got it to work. Posting for others that may want to use this functionality:
Private Sub Test_Checkbox_Click()
If Test_Checkbox.Value = True Then
ActiveDocument.Bookmarks("Test_table").Range.Tables(1).Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("Test_table").Range.Tables(1).Range.Font.Hidden = False
End If
End Sub
Make sure to use ActiveX Checkbox controls. I used this code several times throughout my Word template and just assigned several different bookmarks

How to Delete Text in Word Using Active X Controls

I am currently attempting to create an automated letter on word and know essentially nothing about activex or vba.
I want to create a checkbox that will show text (a subsection of text) only if it is selected. I had originally used a code like this to hide or show text.
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
ActiveDocument.Bookmarks("TextToHide").Range.Font.Hidden = False
Else
ActiveDocument.Bookmarks("TextToHide").Range.Font.Hidden = True
End If
End Sub
However, since my letter had numbered bullets, depending on which checkbox was selected the order of the letter is completely messed up.
Example: I do not want subsections 1,3,5,6 included in the letter (and it is not) but the bullet's order look like this
Subsection...
Subsection...
Subsection...
I figured the only way to get around this is to completely delete the subsection so that the Bullet numbers are in the correct order. I attempted to write the following code but it will not delete the subsection/bookmarked text.
Sub DeleteBookmark()
If CheckBox1.Value = False Then
ActiveDocument.Bookmarks("TextToHide").Delete
End If
End Sub
Could anyone please help me delete the bookmarked text or update the bullet's numbers when hiding a subsection of the letter?
Try to use the Delete method of the Range class instead:
Sub DeleteBookmark()
If CheckBox1.Value = False Then
ActiveDocument.Bookmarks("TextToHide").Range.Delete
End If
End Sub

Hide specific sheets when closing workbook

I'm creating some VBA code which should do the following:
Users press a button a are required to input a code.
When the input the correct code the team relevant code they get access to certain sheets.
The sheets they get access to differs according to the team number and code they enter. So when they enter he password "banana": the sheets "Team_1" & Team_1_sub become visible.
I now created the following code to achieve this:
Sub filter_tabs()
Dim answer As String
answer = InputBox("Please enter your password")
If answer = "Password" Then
MsgBox "Correct, je krijgt nu de goede tabs te zien!"
Worksheets("Team_1").Visible = True
Worksheets("Team_1_sub").Visible = True
Else
MsgBox "Wrong password"
End If
End Sub
Two questions about the code above:
When the users close the document all sheet should "disappear" again. Does anybody know how to do this? So when opening the document sheets "Team_1" and "Team_1_sub" should be be standard
Worksheets("Team_1").Visible = False
Worksheets("Team_1_sub").Visible = False
Could you guys give me some feedback on whether the procedure I follow above (different if statements where users are prompted for a password and then get to see certain tabs) is the most efficient one to reach my goal? End goal is to make sure certain team leader can only see certain sheets.
Here for setting visible false, you can use Workbook_BeforeClose method as follow:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Worksheets("Team_1").Visible = False
Worksheets("Team_1_sub").Visible = False
'must save, if not save, it is not effect.
Me.Save
End Sub
Reference for that method is here.
One thing is that this method must have in the ThisWorkBook module.
For next question, you should say more like, which sheets for which user and password. Because you code is enough for your question. It can use for your requirement.
As you aren't using passwords you should at least make the sheets VeryHidden rather than Hidden - much harder for the average user to unhide.
The Me.Save proposed by the other answer also isn't necessary.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Worksheets("Team_1").Visible = VeryHidden
Worksheets("Team_1_sub").Visible = VeryHidden
End Sub
ad 1)
you best use a Workbook_BeforeSave() routine to code the hiding of all sheets ... in the VBAProject view you find this under ThisWorkbook
ad 2)
The code you post looks very nice - my point of concern would be the hard coding of user names vs. sheet names. I would consider putting this in a sheet/table using headers /code/ /sheetname/ ... this way you can adapt your logic at any time without having to modify the code.
With such a table at hand (in an all time hidden sheet if need be) you traverse it (one single piece of code) and - upon code entering - you unhide If CodeInTable = CodeEntered ... in the other case you unconditionally hide that sheet ... because hiding/unhiding differs only by 2 simple conditions.

Is it possible set my Excel Userform Combo box to begin narrowing the fields while data is entered?

VBA Newbie here.
I have searched high and low for this answer, and even come across other questions very similar to mine, but cannot get an answer. So I am hoping that this is my lucky day.
I have a Userform in excel that has Four combo boxes. Each combo box has a drop down with several choices. In two of these boxes, there are many business names and a lot of these names are similar. I was wanting to have the feature where are the data was being typed into the box, it would begin to narrow the options. EXAMPLE: if I type "heating and air" it begins to only show items in the list that include that word or phrase.
Is this a properties change in the box, or a code written, or something else?
Please help- I am stumped and no one seems to have the answer.
Very grateful-
Excel Newbie
Yes this is very possible. All you have to do is create a sub that populates that combo box, set it up so that when adding it checks the value of the box for example if it was the basic typing example. basic format. This assumes the possible values are stored in an array. This would add any item that has the string entered in it. (in any position)
For I = 0 to Number of Values
If instr(Value(I), ComboBox.Text) > 0 then
add item
endif
next
I played around a bit and came up with something to get you started. This basically functions as an "auto search". It is not autocomplete because it will search entire terms, not just terms which begin with whatever you've typed in. Basically I assume you have a range of cells (in this example cells A2:A121) that have the date for your drop down in it.
Setup
Add a new generic module (I named mine GlobalVars and add the following code:
Option Explicit
Public IgnoreChange As Boolean
Public RangeOfData As Variant
The Code
Open the code to your UserForm.
Add the following code:
Private Sub UserForm_Initialize()
RangeOfData = Application.WorksheetFunction.Transpose(Sheet1.Range("A2:A121").Value)
IgnoreChange = False
End Sub
Be sure to update A2:A121 and Sheet1 (I am using code name, but Worksheets("Sheet1") would work just as well) to point to the data which contains your combobox choices.
Now, the meat of the job is handled in the Combobox_Change event
Private Sub ComboBox1_Change()
If Me.ComboBox1.Text = vbNullString Then
Me.ComboBox1.Clear
SendKeys ("{Enter}")
End If
If Me.ComboBox1.TextLength > 2 Then
Dim i As Long, j As Long
If IgnoreChange = False Then
Me.ComboBox1.Clear
SendKeys ("{Enter}")
DoEvents 'Bug with NumLock
For i = LBound(RangeOfData) To UBound(RangeOfData)
If UCase(RangeOfData(i)) Like "*" & UCase(Me.ComboBox1.Text) & "*" Then
Me.ComboBox1.AddItem RangeOfData(i)
End If
Next i
Me.ComboBox1.DropDown
End If
End If
IgnoreChange = False
End Sub
Be sure to change ComboBox1 to the name of your combobox control.
Basically, what this does is it handles user input when it reaches two characters or longer. The code will search through your input data range and then return results that match the string as the user is entering it. The results is like a suggestions box.
I should note that with this method, the combobox is NOT pre-populated with data, so users must begin typing something into the combobox.
Additionally, I added the following code to handle the backspace key:
Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 8 Then
IgnoreChange = True
End If
End Sub
Again be sure to change ComboBox1 as appropriate.
For my example, I loaded all 120 crayola crayon colors into a spreadsheet (that is what is in Sheet1 from A2:A121).
Here is example output for when I start typing, first I input blu:
As you can see I am getting all values that contain blu, including those that don't start with blu such as Cadet Blue or Midnight blue.
As another example, I will search for flower
So as you can see, instead of being a combobox with 120 static options, it is updated based on what the user types and tied to a list of values in your cells.
I did notice that SendKeys sometimes toggled my NumLock, this is a known issue. The point of that line is to collapse the drop down after the user deletes all of the text or continues texting so as to "refresh" the auto-generated list.

Hide / Unhide entire rows based on check box selection

I would like to hide specific rows based on whether a check box is ticked.
I have this code but it does not seem to work.
I right clicked on the check box, and assigned this macro:
Private Sub CheckBox68_Click()
If CheckBox68 = True Then
[24:26].EntireRow.Hidden = False
End If
End Sub
I am not sure why it does not work. Can anyone see something wrong with it?
Many thanks in advance
Ab
There is no point in your code where you hide the rows.
You need to to modify it as below:
Private Sub CheckBox68_Click()
[24:26].EntireRow.Hidden = Not CheckBox68.value
End Sub