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
Related
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
I've got some code where the user has to multi-select items from a 2-column listbox. These items are variables which will later be plotted, and each have their own units (i.e. °C, °F, etc.). If the user selects items which have different units, an error message appears telling them to reselect.
The problem I'm having is that after the user reselects, it seems the initial selections are still there, because when the plot is generated I can see those variables being plotted. Here's what I've tried without any success:
If InStr(header, yUnit) = 0 Then 'this is the check to see if the selections use different units
MsgBox "Error! y variable selections contain different units. Please choose again"
Application.DisplayAlerts = False
ActiveChart.Delete 'Delete the chart which is currently being constructed
Application.DisplayAlerts = True
'Method 1:
Unload FormatChart
FormatChart.UserForm_Initialize 'FormatChart is the form in which the listbox is contained.
'UserForm_Initialize is the subroutine which constructs the listbox
FormatChart.Show
'Method 2:
With FormatChart.ListBox1
For x = 0 To .ListCount - 1
If .Selected(x) Then
.Selected(x) = False
End If
Next x
End With
FormatChart.Show
End If
'''
Solved it! I just have to use the following:
FormatChart.Show
Exit Sub
I believe what was happening was that the subroutine would continue to run, even though after I showed the form again I had an "Enter" button which would call the subroutine again, possibly resulting in it grabbing variables from the previous, still active, instance of it, or that multiple instances of that subroutine were running, not sure... anyway I'm just happy to end this headache :)
This feels like a stupid question but I can't figure it out, so here goes...
I have a workbook which, based on what's input into a userform, creates a new worksheet each time a command button on the userform is clicked, and deletes the old one.
That means that, although the new worksheet is always given the same name ("Results"), the sheet number increments each time; it's currently calling itself Sheet48 and next time I run it it will be Sheet49.
What I'd like to do is add a command button to that "Results" sheet, which users can click to take them to a different worksheet. I've got as far as adding some code into the userform which creates the command button itself on the "Results" worksheet. I haven't yet written the sub which would tell it to take the user to a different worksheet, but I'm confident that I could do this without much trouble.
My question is how I can assign the sub to the button, considering that the worksheet it's located on is not permanent? I thought the sub should go into a standard module, but all the examples I see for this kind of thing begin with "CommandButton1_Click", or "CommandButton2_Click", and are stored within the sheet object itself. It seems like these numbers 1 and 2 refer to the command button's order on a given worksheet, but presumably a sub on a standard module won't know which worksheet to look at.
I hope that makes sense - any helpful explanations will be much appreciated! Thanks.
EDIT:
This is the code I'm using to create the command button and to (attempt to) assign the sub to it:
'Add a command button, which will allow users to jump to the full report
ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
, DisplayAsIcon:=False, Left:=1025, Top:=130.5, Width:=160, Height:= _
400).Select
ActiveSheet.CommandButton1.Caption = "Click here to see the original report"
ActiveSheet.CommandButton1.WordWrap = True
ActiveSheet.CommandButton1.OnAction = "Module1.ButtonForOriginal" 'the sub works in isolation, but this line isn't assigning it.
Can anyone see any issues there? The ButtonForOriginal sub procedure works in isolation, so it looks like I'm just not calling it properly?
EDIT 2 - I have now achieved the right effect with a different method - see my answer posted below if you're interested. Thanks to those who helped and set me on the right path!
As i understand you are trying to add new sheet with new button, i would recommend using non-dependent buttons with hyperlinks to run your module.
Click insert shape - rectangle or so - and use Selection.OnAction = "YourWorkBook'!YOURSUB"
"CommandButton1_Click" should be an Event Procedure. They go into the module page connected with the sheet that the object sits on. So when you click on it, it knows which procedure to run, it's the one connected to the sheet the button is on.
OK, I sort of figured it out. I had for some reason decided to use an ActiveX command button, instead of a plain old Form one. This seems to have made things more difficult. I don't understand the detail, but I have achieved the effect I was after by doing this instead:
ActiveSheet.Buttons.Add(1025, 130.5, 160, 400).Select
Selection.Characters.Text = "Click here to see the original report"
With Selection.Characters(Start:=1, Length:=200).Font
.Name = "Verdana"
.FontStyle = "Regular"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 1
End With
Selection.OnAction = "ButtonForOriginal"
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.
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.