PDA

View Full Version : Selecting Multiple Ranges and Hiding Them?



grub_rock
06-19-2009, 02:20 AM
Hi People - first post and my first venture into word vba,

I've used the piece of code below (that was posted by fumei), to hide one bookmark. What i'd like to do is engineer the code to hide more than one bookmark. alternatively I'd like to know how to name tables and do the same - hide multiple, not necessarily consecutive, tables when the checkbox is checked. alternatively define a range of non-consecutive lines, like one line at the top of the page and one somewhere else on the page. then hide them.

Can anyone help?

Private Sub checkbox1_Change()
Call ShowHideBookmark
End Sub


Sub ShowHideBookmark()
Dim orange As Range
Set orange = ActiveDocument.Bookmarks("seniorexec").Range
If CheckBox1.Value = True Then
With orange.Font
.Hidden = True
End With
With ActiveWindow.View
.ShowAnimation = True
.Draft = False
.WrapToWindow = False
.ShowPicturePlaceHolders = False
.ShowFieldCodes = False
.ShowBookmarks = False
.FieldShading = wdFieldShadingWhenSelected
.ShowTabs = False
.ShowSpaces = False
.ShowParagraphs = False
.ShowHyphens = False
.ShowHiddenText = True
.ShowDrawings = True
.ShowObjectAnchors = True
.ShowTextBoundaries = True
.ShowHighlight = True
.DisplayPageBoundaries = True
.DisplaySmartTags = True
End With
Else
With orange.Font
.Hidden = False
End With
With ActiveWindow.View
.ShowAnimation = True
.Draft = False
.WrapToWindow = False
.ShowPicturePlaceHolders = True
.ShowFieldCodes = False
.ShowBookmarks = False
.FieldShading = wdFieldShadingWhenSelected
.ShowTabs = False
.ShowSpaces = False
.ShowParagraphs = False
.ShowHyphens = False
.ShowHiddenText = False
.ShowDrawings = True
.ShowObjectAnchors = True
.ShowTextBoundaries = True
.ShowHighlight = True
.DisplayPageBoundaries = True
.DisplaySmartTags = True
End With
End If
End Sub

fumei
06-19-2009, 09:17 AM
1. Please use the VBA code tags when posting code. It makes it easier for us to read.

2. You can not make a single range object cover non-contiguous areas. It is possible to do this by Selecting areas, but NOT with VBA. Even a Selection that does cover non-contiguous areas will NOT have those areas action by VBA. VBA will only action the last area.

In other words: "alternatively define a range of non-consecutive lines" can not be done. Ranges MUST be contiguous.

3. "What i'd like to do is engineer the code to hide more than one bookmark"

Then define them as Bookmarks, rather than ranges.

Can you do what you want? Yes. But you have to change the code a little. Do you know which bookmarks? What is the logic determining which to hide? Work that out, and yes, it can be done.

4. "alternatively I'd like to know how to name tables and do the same"

You name tables by bookmarking them (which means the bookmark has a name), and then making a table object of the table in the bookmark. Like this (used AFTER the table has been bookmarked):Dim Table3 As Table
Set Table3 = ActiveDocument.Bookmarks("Table3") _
.Range.Tables(1)
Now you have a table object of THAT table - named as Table3. Now you can do whatever action you want.

So....

If Table1, Table2, Table3 are bookmarked (and thus named) you can create and use table objects for them independently. Not only that, but because they are ranges, they can be moved, resized (rows added for example) and it makes no difference. The bookmark will point to the table no matter where it is.

You can of course give more meaningful names. Instead of Table1, you could name it ClientName, or whatever.

grub_rock
06-22-2009, 03:04 AM
Fumei - thank you, your answer is very helpful - I have bookmarked all of my tables, can you show me what the code would be if I wanted to hide table "UHNWClientData" and table "HNWClientData" when Checkbox1 is checked? (i.e. according to the original code you posted).

Also thanks for the tip on forum etiquette. newbie, sorry :-)

fumei
06-22-2009, 12:21 PM
There are a few ways to go about it, as is often the case with VBA. One way would be a procedure that takes a table object as a parameter.

First of all is to be absolutely correct in the logic. I am going to assume:

1. you really do want to action against TWO tables.

2. you really have bookmarked them - and ONLY them. By that I mean, when you bookmarked them you selected just the table, and ONLY the table, for the bookmark.

3. you named the bookmarks UHNWClientData and HNWClientData, respectively. Here are the procedures to hide both the text (as Hidden font), and remove all border format for the tables themselves. Thus making the tables become "hidden".
Option Explicit

Sub HideTable(oTable As Table)
With oTable
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
.Borders(wdBorderTop).LineStyle = wdLineStyleNone
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
.Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone
.Borders(wdBorderVertical).LineStyle = wdLineStyleNone
.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
.Borders.Shadow = False
End With

End Sub


Sub ShowTable(oTable As Table)
With oTable
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderHorizontal)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderVertical)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
.Borders.Shadow = False
End With
With Options
.DefaultBorderLineStyle = wdLineStyleSingle
.DefaultBorderLineWidth = wdLineWidth050pt
.DefaultBorderColor = wdColorAutomatic
End With
End Sub

Sub HideTableText(oTable As Table)
oTable.Range.Font.Hidden = True
End Sub

Sub ShowTableText(oTable As Table)
oTable.Range.Font.Hidden = False
End Sub

Now here is the code to action your tables.
Sub HideTwoTables()
Dim oTable As Table
Set oTable = ActiveDocument.Bookmarks("UHNWClientData") _
.Range.Tables(1)
' action first table
Call HideTableText(oTable)
Call HideTable(oTable)
' clear object memory and reset
Set oTable = Nothing
Set oTable = ActiveDocument.Bookmarks("HNWClientData") _
.Range.Tables(1)
' action second table
Call HideTableText(oTable)
Call HideTable(oTable)

fumei
06-22-2009, 12:33 PM
I just want to add that you could make the action procedure itself take a parameter. This could be a string (text) you input via an inputbox.
Sub HideMe()
Dim strTable As String
strTable = Inputbox("Enter the name of the table to hide.")
Call HideTHISTable(strTable)
end sub


Sub HideTHISTable(strIn As String)
Dim oTable As Table
Set oTable = ActiveDocument.Bookmarks(strIn) _
.Range.Tables(1)

Call HideTableText(oTable)
Call HideTable(oTable)
End Sub
Thus, you give the name of the table to HideMe, and it calls another procedure performs the actions.

Or......you could have it that the action is performed on the table the cursor (the Selection) is in.
Sub HideTHIS_One()
Dim oTable As Table
' obviously if Selection NOT in table
' get out of this
If Selection.Information(wdWithInTable) = False Then
MsgBox "Not in table."
Exit Sub
Else
Set oTable = Selection.Tables(1)
Call HideTableText(oTable)
Call HideTable(oTable)
End If
End Sub

grub_rock
06-23-2009, 09:34 AM
Fumei - I really appreciate your time in this, unfortunately running the code did not do what i wanted, which is the same as in your original posting: I'm trying to reduce the size of a 44page long form by essentially collapsing data tables that don't need to be there, when a checkbox is ticked.

I don't fully understand how to work the code you've prescribed in to the "if" format of your earlier posting, where the checkbox determines whether the tables are showing.

sorry to be such an ignoramus in this, it probably seems like i should do some homework and come back, but:

1) I literally don't have time to leave the office to get a user manual for word.
2) I learnt excel VBA through recording Macros - that doesn't seem to be an option with Word!

Please feel free to give up on me as a lost cause, but once i've got the basics of this I'll be nailing it.

cheers,

C.

fumei
06-23-2009, 10:15 AM
"1) I literally don't have time to leave the office to get a user manual for word.
2) I learnt excel VBA through recording Macros - that doesn't seem to be an option with Word!"

1. there is no VBA user manual for Word. There are some commercial books that are good though.

2. That is not correct. That is mostly how I learned VBA in Word. Recording macros and deconstructing them...and reading some books.

"Fumei - I really appreciate your time in this, unfortunately running the code did not do what i wanted, which is the same as in your original posting: I'm trying to reduce the size of a 44page long form by essentially collapsing data tables that don't need to be there, when a checkbox is ticked."

I am sorry, but that is so ill-defined that I am do not see how I can help.

"that don't need to be there". Does that mean...sometimes it does? Because if it means EXACTLY as it says...then don't hide them...delete them. THAT would surely shorten your document.

I do not know what you mean that it does not work. You do not describe this. It works for me. Do you have Show/Hide set as Hide? if not...then they will still be visible. Are you in Print Preview? Who knows?