Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2005

Tip: Looking for answers? Try searching our database.

Code work unless certain code is run

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
frogman - 24 Oct 2005 17:33 GMT
Code work unless certain code is run then it messes up if the selected
text is in a bookmark and is part of a table

Sub Draft()
'Application.ScreenUpdating = False
Dim i As Integer
Dim strBookmarkName As String
Dim strTest As Variant
Application.ActiveWindow.ActivePane.View.ShowAll = True

'Find tables with blue italics text
   Selection.Find.ClearFormatting
   With Selection.Find
       .Text = ""
       .Replacement.Text = ""
       .Forward = True
       .Wrap = wdFindContinue
       .Format = True
       .Font.Italic = True
       .Font.Color = wdColorBlue
       .Font.Hidden = True
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   End With
   Selection.Find.Execute

'Loop through and find all tables and hide the tables
   While Selection.Find.Found
       'check to see if the table is inside a bookmark
       On Error Resume Next
       strBookmarkName = Selection.Bookmarks(1).Name

       'if table is not inside a book mark unhide it
       If strBookmarkName = "" Then
           Selection.SelectRow
           Selection.Font.Hidden = False
           If Selection.Font.Color = wdColorBlue And
Selection.Font.Italic = True Then
               Selection.Borders.Enable = True
           End If

           'if the selection is the last row of a table then hide the
return after it.
           If Selection.Rows.Last.IsLast Then
               Selection.Move unit:=wdParagraph, Count:=1
               Selection.Paragraphs(1).Range.Select
               Selection.Font.Hidden = False
           End If
       Else   ' if the table is inside a bookmark check to see if the
bookmark is hidden
            'if the bookmark is not hidden unhide the book mark
''**           If
ActiveDocument.Bookmarks(strBookmarkName).Range.Font.Hidden = False
Then
               Selection.SelectRow
               Selection.Font.Hidden = False
               If Selection.Font.Color = wdColorBlue And
Selection.Font.Italic = True Then
                   Selection.Borders.Enable = False
               End If

               'if the selection is the last row of a table then hide
the return after it.
               If Selection.Rows.Last.IsLast Then
                   Selection.Move unit:=wdParagraph, Count:=1
                   Selection.Paragraphs(1).Range.Select
                   Selection.Font.Hidden = False
               End If
               strBookmarkName = ""
           Else  'if the bookmark is hidden change the font color so
the search will skip this table
               Selection.Font.Color = wdColorGold
               strBookmarkName = ""
               Selection.Find.Font.Color = wdColorBlue
           End If
       End If
       Selection.HomeKey unit:=wdStory
       Selection.Find.Execute
   Wend

   'Find all the nonbold red "["
   Call FindBold("[", True, False)
   'Find all the nonbold red "]"
   Call FindBold("]", True, False)
   'Find all the bold red "{"
   Call FindBold("{", True, True)
   'Find all the bold red "}"
   Call FindBold("}", True, True)
   'Find all the bold red "["
   Call FindBold("[", True, True)
   'Find all the bold red "]"
   Call FindBold("]", True, True)

'change all the gold text back to blue so the final mode will work
again
Selection.Find.Replacement.ClearFormatting
Selection.Find.ClearFormatting
With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .Font.Color = wdColorGold
    .Replacement.Font.Color = wdColorBlue
End With

Selection.Find.Execute , Replace:=wdReplaceAll

Selection.Find.Replacement.ClearFormatting
Selection.Find.ClearFormatting

ActiveWindow.View.FieldShading = wdFieldShadingAlways
Application.ActiveWindow.ActivePane.View.ShowAll = False
Application.ActiveWindow.View.ShowHiddenText = False
Application.ScreenUpdating = True
End Sub

Function FindBold(strChar, blnHidden, blnBold)
Selection.HomeKey unit:=wdStory
Selection.Find.ClearFormatting
   With Selection.Find
       .Text = strChar
       .Forward = True
       .Wrap = wdFindContinue
       .Format = True
       .Font.Bold = blnBold
       .Font.Color = wdColorRed
       .Font.Hidden = blnHidden
       .Replacement.Font.Hidden = Not blnHidden
   End With

'Execute the find command
   Selection.Find.Execute Replace:=wdReplaceAll, Format:=True
End Function
Helmut Weber - 24 Oct 2005 17:58 GMT
Hi Frogman,

strip off from your code
all that isn't absolutely necessary
and all that has been tested successfully.

Tell us what you want to do.

A failure is the difference between reality and expectations.

What is the reality?
What do you expect?

And from what kind of data?

Friends here can help best,
if they can paste the code right into a module,
set up a simple document, according to the
information you gave them, and do the testing.

Like this, they can't do much, IMHO.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

frogman - 24 Oct 2005 19:13 GMT
the senerio:

I have these tables called specifiers the text with in them is italics
and blue and sometime they are standalone tables and other times they
are part of a table, now I have some cases where the table resides in a
bookmark that may or maynot be hidden.  The code below works.  after it
finds the first table in the bookmark it hides it if the bookmark is
hidden and doesn't hide it if the bookmark is not hidden.

the next time it finds a specifier it is part of a table and the code:

If ActiveDocument.Bookmarks(strBookmarkName).Range.Font.Hidden = False
Then
Selection.SelectRow
               Selection.Font.Hidden = False
               If Selection.Font.Color = wdColorBlue And
Selection.Font.Italic = True Then
                   Selection.Borders.Enable = False
               End If

               'if the selection is the last row of a table then hide
the return after it.
               If Selection.Rows.Last.IsLast Then
                   Selection.Move unit:=wdParagraph, Count:=1
                   Selection.Paragraphs(1).Range.Select
                   Selection.Font.Hidden = False
               End If
               strBookmarkName = ""
           Else  'if the bookmark is hidden change the font color so
the search will skip this table
               Selection.Font.Color = wdColorGold
               strBookmarkName = ""
               Selection.Find.Font.Color = wdColorBlue
           End If
       End If

doesn't work because the bookmark is not hidden and the code executes
the Else 'if the bookmark is hiden change...
frogman - 24 Oct 2005 20:36 GMT
if you want me to email you the file just let me know
frogman - 24 Oct 2005 22:26 GMT
by bad this if statement is not checking if the bookmark is hidden or
not and I don't know Why?

If ActiveDocument.Bookmarks(strBookmarkName).Range.Font.Hidden = False
Then
frogman - 24 Oct 2005 22:49 GMT
           If
ActiveDocument.Bookmarks(strBookmarkName).Range.Font.Hidden = False Or
ActiveDocument.Bookmarks(strBookmarkName).Range.Font.Hidden =
wdUndefined Then

this code fixed it if the table is hidden and is inside the bookmark
then the if statement returned wdUndefined or mixed so i added that as
a veriable and it works

Sometimes you just need to type things out so you can see were your
code is going wrong

Thanks
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.