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
Related
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
So I'm attempting to filter a large section of data in a spreadsheet with checkboxes. So far around 80 individual checkboxes. I'm wondering if there is anyway to refer to the checkbox (or any other control) name within the sub, as a specific reference, i.e. thiscontrol.name. At present the first checkbox reads:
Private Sub_F1_Click()
StringVariableForLaterUse ="F1"
If F1.Value = True Then
'Display Data Relevant to F1
End If
End
I'm wondering if I can use
StringVariableForLaterUse = ThisControl.Name
and
If ThisControl.Value = True Then
I'd then be able to replicate this a further 78 times. Currently pure laziness factor, however I want my Subs to be self sufficient as possible. Any thoughts folks?
You can never use a string like an object... But, for an ActiveXcheck box, use the next workaround code.
Public chk1 As Shape
Private Sub CheckBox1_Click()
Set chk1 = ActiveSheet.Shapes(CheckBox1.Name)
MsgBox chk1.OLEFormat.Object.Object.value
End Sub
You can use now chk1 in another Sub inside the sheet module.
You can also refer to it from a module Sub, referencing the sheet, too:
Sub testSheetChk()
Debug.Print Worksheets("Sheet Name").chk1.OLEFormat.Object.Object.value
End Sub
But, this will work only after you firstly run (once) the click event, in order to alocate a value to the object variable.
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.
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.
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