need
need

Reputation: 13

Hiding/Revealing/Adding/Removing parts of a document with VBA

I want to make a form where you can select which parts of a document you need for example:

You have 4 Boxes - Textbox1, Textbox2, Textbox3, Textbox4

Via a Checkboxlisti in the Userform you can select you only want Textbox1 + 4 and the document adds/removes/hides/shows only the parts you want to so in this example it would only show Textbox1 and Textbox 4 - I am not sure if this works with showing/hiding/adding/removing/ maybe from an external file? Here is a picture how I would imagine it to look. (In the Picture example It would remove Textbox3 because it is not selected.

Does anyone have an solution? Or maybe how to approach this problem... Thanks in advance.

Userform picture

Upvotes: 0

Views: 38

Answers (1)

dbmitch
dbmitch

Reputation: 5386

This works fine using numerically added textboxes

Private Sub CheckBox1_Click()
    ActiveDocument.Shapes(1).Visible = CheckBox1
End Sub

Private Sub CheckBox2_Click()
    ActiveDocument.Shapes(2).Visible = CheckBox2
End Sub

Private Sub CheckBox3_Click()
    ActiveDocument.Shapes(3).Visible = CheckBox3
End Sub

Private Sub CheckBox4_Click()
    ActiveDocument.Shapes(4).Visible = CheckBox4
End Sub

EDIT - using named textboxes for easier readability

If you copy/paste your textboxes Word will create them all with the same names. If you want to create readible code and insure your textboxes are named according to the order you created them you can preload the names.

   For Each shp In ActiveDocument.Shapes
       If shp.Type = msoTextBox Then
           i = i + 1
           shp.Name = "TextBox" & i
        End If
    Next shp 
    
    Private Sub CheckBox1_Click()
        ActiveDocument.Shapes("TextBox1").Visible = CheckBox1 
    End Sub
    
    Private Sub CheckBox2_Click()
        ActiveDocument.Shapes("TextBox2").Visible = CheckBox2 
    End Sub
    
    Private Sub CheckBox3_Click()
        ActiveDocument.Shapes("TextBox3").Visible = CheckBox3 
    End Sub
    
    Private Sub CheckBox4_Click()
        ActiveDocument.Shapes("TextBox4").Visible = CheckBox4 
    End Sub

Example of output

Output Results

Upvotes: 1

Related Questions