Nested With and For Each

D

Deejay

I have at the moment some code which allows me through a macro to make
graphics visibile or hidden. I am trying to add some code which makes some
bookmarks visible/hidden to be nested within the With that is already there.
So in effect there will be 3 states and the macro will toggle between those
states.

1. graphics visible
2. graphics and bookmarks hidden
3. graphics hidden but bookmarks visible.

I have placed a * at the beginning of those lines that I'm not sure about
and I require help on.

Many many thanks

Dim boolFax As Boolean
Dim boolBookMark As Boolean
Dim aShape As Shape
*Dim aBookmark As Bookmark


With ActiveDocument
' Check current status - it's in fax format if graphics are visible
boolFax = .Sections(1).Headers(wdHeaderFooterFirstPage).Shapes(1).Visible
*boolBookMark = .
' Show/Hide the graphics
With .Sections(1)
For Each aShape In .Headers(wdHeaderFooterFirstPage).Shapes
aShape.Visible = Not boolFax
*With ActiveDocument.Bookmarks
* For Each aBookmark In ActiveDocument.Bookmarks
* aBookmark.Range.Font.Hidden = Not boolBookmark
* Next aBookmark
* End With
Next aShape

End With
 
D

Deejay

The following code works partially but it only gives me 2 of the 3 states. I
still can't get no graphice with visible bookmarks.

Dim boolFax As Boolean
Dim aShape As Shape
Const strBookMark As String = "faxonly"
Dim rgeBookMark As Range
Set rgeBookMark = ActiveDocument.Bookmarks(strBookMark).Range
Dim aBookmark As Bookmark
Dim VisibleBookMark As Boolean

With ActiveDocument
' Check current status - it's in fax format if graphics are visible
boolFax = .Sections(1).Headers(wdHeaderFooterFirstPage).Shapes(1).Visible
boolbookmark = rgeBookMark.Font.Hidden
' Show/Hide the graphics
With .Sections(1)
For Each aShape In .Headers(wdHeaderFooterFirstPage).Shapes
aShape.Visible = Not boolFax
With ActiveDocument.Bookmarks
For Each aBookmark In ActiveDocument.Bookmarks
aBookmark.Range.Font.Hidden = Not boolBookMark
Next aBookmark
End With
Next aShape
End With
 
A

Astrid

Hi Deejay,

For me, the first difficult thing about your code is that you don't define
all possible options (for example does your option 1 includes the bookmarks
to be visible or not?).
I assumed that would be the case and rewrote your code, but used a different
approach. I used an if then else construction and wrote different procedures
to toggle the state of shapes and bookmarks:

Sub Test()
Dim bBookMarkVisible As Boolean
Dim bGraphicVisible As Boolean
Dim oShape As Shape
Dim oRange As Range
Const csBookMark As String = "faxonly"

bGraphicVisible = ActiveDocument.Sections(1). _
Headers(wdHeaderFooterFirstPage).Shapes(1).Visible
bBookMarkVisible = Not ActiveDocument. _
Bookmarks(csBookMark).Range.Font.Hidden

If bGraphicVisible And bBookMarkVisible Then
'both shapes and bookmarks visible
'hide both shapes as bookmarks
ToggleFirstHeaderShapes bHidden:=True
ToggleBookmarks bHidden:=True
ElseIf Not bGraphicVisible And Not bBookMarkVisible Then
'both shapes and bookmarks NOT visible
'show bookmarks
ToggleBookmarks bHidden:=False
ElseIf Not bGraphicVisible And bBookMarkVisible Then
'shapes NOT visible, bookmarks visible
'show both shapes as bookmarks
ToggleFirstHeaderShapes bHidden:=False
ToggleBookmarks bHidden:=False
End If

End Sub


Sub ToggleBookmarks(bHidden As Boolean)
Dim oBookmark As Bookmark

For Each oBookmark In ActiveDocument.Bookmarks
oBookmark.Range.Font.Hidden = bHidden
Next

Set oBookmark = Nothing
End Sub


Sub ToggleFirstHeaderShapes(bHidden As Boolean)
Dim oShape As Shape

For Each oShape In ActiveDocument.Sections(1) _
.Headers(wdHeaderFooterFirstPage).Shapes
oShape.Visible = Not bHidden
Next

Set oShape = Nothing
End Sub

Hope this helps,
kind regards,

Astrid
 
R

Russ

Deejay,
Like Astrid says, you didn't mention all possibilities of the truth table:
S= Show Shapes and B= Show Bookmarks
S B
---
T T
F F
T F
F T
You can add another elseif clause to Astrid's code if you want to toggle
through that possibility, too.

Here's another way to handle the situation by using MsgBoxes:
Public Sub Show_Hide_Shapes_Bookmarks()
Dim aShape As Shape
Dim aBookmark As Bookmark
Dim strMsg As String, lngStyle As Long, strTitle As String
Dim lngResponse As Long, lngResponse2 As Long

strMsg = "Show Shapes?" ' Define message.
lngStyle = vbYesNo + vbCritical + vbDefaultButton1 ' Define buttons.
strTitle = "Shapes" ' Define title.
lngResponse = MsgBox(strMsg, lngStyle, strTitle)
strMsg = "Show Bookmarks?" ' Define message.
strTitle = "Bookmarks" ' Define title.
lngResponse2 = MsgBox(strMsg, lngStyle, strTitle)

With ActiveDocument
With .Sections(1)
' Show/Hide the graphics
If lngResponse = vbYes Then ' User chose Yes.
For Each aShape In .Headers(wdHeaderFooterFirstPage).Shapes
aShape.Visible = True
Next aShape
Else ' User chose No.
For Each aShape In .Headers(wdHeaderFooterFirstPage).Shapes
aShape.Visible = False
Next aShape
End If
' Show/Hide the bookmarks
If lngResponse2 = vbYes Then ' User chose Yes.
For Each aBookmark In ActiveDocument.Bookmarks
aBookmark.Range.Font.Hidden = True
Next aBookmark
Else ' User chose No.
For Each aBookmark In ActiveDocument.Bookmarks
aBookmark.Range.Font.Hidden = False
Next aBookmark
End If
End With
End With

MsgBox "Finished!"
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top