Results 1 to 12 of 12
  • Thread Tools
  1. 3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Paragraph Style vs Hidden Text (VBA Office 2000)

    This is an that dates back to WordBasic. And VBA has inherited.

    Assume you have a document with four paragraphs.
    The first two are heading 1 and 2 - -both of which are formatted (via style) as hidden.

    The second two are heading 3 (not hidden) and Normal.

    Now assume you have HIDDEN showing and you do:

    msgbox ActiveDocument.Paragraphs(1).Style

    You will get "Heading 1" as the style. All fine.

    Now HIDE hidden text. The first paragraph is now Heading 3. If you do:

    <pre>msgbox ActiveDocument.Range( _
    Start:=ActiveDocument.Paragraphs(1).Range.Start, _
    End:=ActiveDocument.Paragraphs(1).Range.End - 1)
    </pre>


    It will display the text of Heading 3 (the first visible paragraph). But if you again query the style:

    msgbox ActiveDocument.Paragraphs(1).Style

    It will display "heading 1" -- the style that is HIDDEN in front of Heading 3.

    Argh. I've attached a document that demos the problem.
    Attached Files Attached Files

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Style vs Hidden Text (VBA Office 2000)

    Try the following with your document.
    <pre>Option Explicit
    Private Sub TestHideandSeek()
    Debug.Print "With hidden text displayed"
    HideandSeek True
    Debug.Print "With hidden text not displayed"
    HideandSeek False
    End Sub

    Private Sub HideandSeek(blnHideMe As Boolean)
    Dim parTemp As Paragraph

    With ActiveWindow.View
    .ShowHiddenText = blnHideMe
    For Each parTemp In ActiveDocument.Paragraphs
    With parTemp
    Debug.Print vbTab & .Style & ": " & .Range.Text
    Debug.Print vbTab & .Style & ": " & _
    ActiveDocument.Range(Start:=.Range.Start, End:=.Range.End - 1).Text
    End With
    Next parTemp
    .ShowHiddenText = Not blnHideMe
    End With
    End Sub
    ' THe following are the results.
    'With hidden text displayed
    ' Heading 1: HEADING LEVEL ONE - HIDDEN
    '
    ' Heading 1: HEADING LEVEL ONE - HIDDEN
    ' Heading 2: HEADING LEVEL TWO - HIDDEN
    '
    ' Heading 2: HEADING LEVEL TWO - HIDDEN
    ' Heading 3: HEADING LEVEL THREE - NOT HIDDEN
    '
    ' Heading 3: HEADING LEVEL THREE - NOT HIDDEN
    ' Normal: Paragraph of Normal
    '
    ' Normal: Paragraph of Normal
    'With hidden text not displayed
    ' Heading 1: HEADING LEVEL THREE - NOT HIDDEN
    '
    ' Heading 1: HEADING LEVEL THREE - NOT HIDDEN
    ' Normal: Paragraph of Normal
    '
    ' Normal: Paragraph of Normal
    </pre>


  4. 3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Style vs Hidden Text (VBA Office 2000)

    Howard,

    Nice code, but it proves the bug. The debug.print resuts of your code with Hidden Hidden are:

    <pre>With hidden text not displayed

    Heading 1: HEADING LEVEL THREE - NOT HIDDEN

    Heading 1: HEADING LEVEL THREE - NOT HIDDEN
    Normal: Paragraph of Normal

    Normal: Paragraph of Normal
    </pre>


    Which basically is returning the text of heading 3 and the style of the hidden heading 1.

    It should be:

    Heading 3 : HEADING LEVEL THREE - NOT HIDDEN

    The solution I've come up with is, when querying a paragraph for it's style, set the range to the last character:

    Set aRange = ActiveDocument.Range( _
    Start:=ActiveDocument.Paragraphs(i).Range.End - 1, _
    End:=ActiveDocument.Paragraphs(i).Range.End)

    debug.print aRange.Style

  5. Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Style vs Hidden Text (VBA Office 2000)

    Here's a solution.

    <pre>Option Explicit
    Private Sub TestHideandSeekWorkAround()
    Debug.Print "With hidden text displayed"
    HideandSeek True
    Debug.Print "With hidden text not displayed"
    HideandSeek False
    End Sub

    Private Sub HideandSeek(blnHideMe As Boolean)
    Dim blnHidden As Boolean
    Dim parTemp As Paragraph

    With ActiveWindow.View
    .ShowHiddenText = blnHideMe
    For Each parTemp In ActiveDocument.Paragraphs
    With parTemp
    blnHidden = .Range.Font.Hidden
    .Range.Font.Hidden = False
    Debug.Print vbTab & .Style & ": " & .Range.Text
    Debug.Print vbTab & .Style & ": " & _
    ActiveDocument.Range(Start:=.Range.Start, End:=.Range.End - 1).Text
    .Range.Font.Hidden = blnHidden
    End With
    Next parTemp
    .ShowHiddenText = Not blnHideMe
    End With
    End Sub
    </pre>


  6. 3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Style vs Hidden Text (VBA Office 2000)

    Howard,

    Your solution REMOVES the hidden attribute of heading 1 and 2.

    Perhaps I wasn't clear. The point of the bug is :

    I want to determine the style of the first VISIBLE pargraph when hidden is NOT displayed.

    In this case that would be heading 3.

    I do not want to alter (or display) heading's 1 and 2 (the first two "hidden" pargraphs in the test document).

    The point is that when ViewHidden = False

    ActiveDocument.Paragraph(1).Range.Text returns the text of the Heading 3 paragraph

    but

    ActiveDocument.Paragraph(1).Style

    returns the style name of the first hidden paragraph in front of the first visible paragraph (in the demo case, this is Heading Level 1).

    The bug is if you have Hidden in front of Non-Hidden, you have ShowHidden = False, you query the non-hidden paragraphs style, you get the Hidden paragraph is the return.

    This was the case with WordBasic. If the insertion point was at the beginning of such a line, it returned the hidden style name. The workaround was to always move to Character 2 and do the StyleName() query.

    Using the Word Object, for whatever reason, the equivalent of setting the Range Start = start+1, End = end - 1 didn't work, but start = end-2, end = end -1 did work.

  7. Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Style vs Hidden Text (VBA Office 2000)

    Upon further examination, I see that the behavior you are seeing is by DESIGN and is NOT a bug. You might not like it, but that's just the way it is.

    Your problem is that the first character of your Heading 3 paragraph, and maybe other characters within the paragraph, are assigned as hidden. It's usually a good idea to have a space between hidden and non-hidden text. hidden

    Add a space at the end of your heading 2 paragraph and make it non-hidden, and then select the heading 3 paragraph abd make it non-hidden.

    Note that I had to tinker a bit to get the fonts just right.

    Try running the following:

    <pre>Option Explicit
    Private Sub TestHideandSeekWorkAround()
    Debug.Print "With hidden text displayed"
    HideandSeek True
    Debug.Print "With hidden text not displayed"
    HideandSeek False
    End Sub


    Private Sub HideandSeek(blnHideMe As Boolean)
    Dim blnHidden As Boolean
    Dim blnSaveHidden As Boolean
    Dim parTemp As Paragraph

    With ActiveWindow.View
    blnSaveHidden = .ShowHiddenText
    .ShowHiddenText = blnHideMe
    End With
    For Each parTemp In ActiveDocument.Paragraphs
    With parTemp
    Select Case .Range.Characters(1).Font.Hidden
    Case 0 'False
    Debug.Print "False";
    Case -1 'True
    Debug.Print "True";
    Case Else 'wdUndefined
    Debug.Print "Mixed";
    End Select
    Debug.Print " ";
    Select Case .Range.Font.Hidden
    Case 0 'False
    Debug.Print "False";
    Case -1 'True
    Debug.Print "True";
    Case Else 'wdUndefined
    Debug.Print "Mixed";
    End Select
    Debug.Print
    Debug.Print vbTab & .Style & ": " & .Range.Text
    ' Debug.Print vbTab & .Style & ": " & _
    ' ActiveDocument.Range(Start:=.Range.Start, End:=.Range.End - 1).Text
    End With
    Next parTemp
    With ActiveWindow.View
    .ShowHiddenText = blnSaveHidden
    End With
    End Sub
    </pre>


  8. 3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Style vs Hidden Text (VBA Office 2000)

    "It's not a bug, it's a feature." That was a button distributed at the first WordBasic conference. You call it a feature. So, I'm sure, does MS.

    Nonesense. It's a bug.

    <hr>Your problem is that the first character of your Heading 3 paragraph, and maybe other characters within the paragraph, are assigned as hidden<hr>

    Not so. The first character of my heading 3 paragraph is not hidden. They are *reported* as hidden when showhidden=false by Word, but they are not hidden by their underlying style definition. The insertion point angles to the right to tell the user visually that there is hidden text in the previous paragraph. But it is not actually IN a stream paragraph of hidden, There is no reason -- by design -- that this should also report that the letter "H" -- in the case of the example text -- possesses the hidden attibute. It doesn't. And it isn't style heading 1 or heading 2. It is heading 3.

    <hr>Add a space at the end of your heading 2 paragraph and make it non-hidden, and then select the heading 3 paragraph abd make it non-hidden.
    <hr>

    Assuming this kludge worked, it would only demonstrate that the handling of a document containing both hidden and non-hidden styles has been poorly designed (or is bug-ridden) It violates the larger design of template/styles and is inconsistent at best.

    But it doesn't work in my test. Adding non-hidden to the end of heading 2 manually does nothing to return the proper style name when my original test macro is run. It does misalign the paragraphs visually when hidden is not display. The whole point of styles is that I don't need to do any manual formatting, any extra carriage returns.

    (If you are seeing direct formatting of either hidden or non-hidden in the test document, this is residual effect of previous HideAndSeek tests -- do RestCharacter to the entire document to return it to the underlying style definitions and you'll find there are no hidden characters in the non-hidden styles.)

    The fact that it's a bug is obvious by the contradiction that paragraph(1). text is the text of the visible paragraph. and paragraph(1).style is the wrong style.

    For anyone who uses hidden text a lot and for applications that manipulate documents that use hidden text, the "feature" is a bug. And the workaround is for the developer to remember to always query a paragraph's style from within the pargraph (however you get there -- in the stream or by moving the insertion point) NOT from the home position of the paragraph or from the entire paragraph range. They return false info.

    The argument is moot. I have found a functional workaround.
    <pre>Set aRange = ActiveDocument.Range( _
    Start:=ActiveDocument.Paragraphs(i).Range.End - 1, _
    End:=ActiveDocument.Paragraphs(i).Range.End)
    debug.print aRange.Style
    </pre>


  9. 3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Style vs Hidden Text (VBA Office 2000)

    Let's look at the question underlying my problem from a different angle and see if it doesn't appear more like a bug.

    A word document is a stream of single characters, some actual characters, some pointers to strings, some holders for attributes.

    For the sake of argument, say you have a fifteen character document which would appear, in stream form as :

    h1h1h1h1h1p1h2h2h2h2h2p2v3v3v3v3v3p3

    Where h1 is paragraph styled hidden, h2 is a paragraph styled hidden, v3 is a paragraph styled visible. px is the pargraph mark for each.

    This will look, on the screen, as:

    h1h1h1h1h1

  10. 3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Style vs Hidden Text (VBA Office 2000)

    It occurs to me that MS could easily fix the problem I'm having by NOT changing the index of paragraphs depending on whether or not visible is showing.

    In the previous example, v3 would always be paragraph(3) -- and h1 would always be paragraph(1).

    Curiously, the number of paragraphs returned by:

    Debug.Print ActiveDocument.Paragraphs.Count

    does NOT change dependent upon the hiddenvisible state.

    So if, in my example

    ActiveDocument.Paragraphs.Count

    is always equal to 3, how can v3 be paragraph 3 when hidden is showing and change to 1 when hidden is hidden. (I am saying it CHANGES to 1 when hidden is not showing based on the fact that querying the text of p1 returns the text of the visible paragraph, not of the first paragraph in the stream.)

  11. Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Style vs Hidden Text (VBA Office 2000)

    The first character of the Heading 3 paragraph in your original document does have the hidden attribute assigned when ShowHidden is false because that's the way Word does things, wisely or not.

    You can see this by selecting the first character and using the Format | Font menu.

    It is a "feature" of Word that having hidden text, or even certain of te special fields such as PRIVATE and SET, adjacent to normal text /fields causes problems.

    Yes, it is dumb design, but since this problem has been there since at least Word 6, I expect that MSFT figures it would be too expensive to change, or, if changed, might break some extant code.

    Of course, since MSFT inadequately documents Word/VBA/WordBasic, it's kind of hard to tell what is a bug and what is a feature.

    Unlike WordBasic, VBA has the Range object, so, in such situations, one could unhide a range and get the needed info without affecting the user's selection

  12. Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Style vs Hidden Text (VBA Office 2000)

    You can see the changing paragraph numbering in the following:

    <pre>Option Explicit
    Private Sub TestHideandSeekWorkAround()
    Debug.Print "With hidden text displayed"
    HideandSeek True
    Debug.Print "With hidden text not displayed"
    HideandSeek False
    End Sub

    Private Sub HideandSeek(blnHideMe As Boolean)
    Dim blnHidden As Boolean
    Dim blnSaveHidden As Boolean
    Dim parTemp As Paragraph

    With ActiveWindow.View
    blnSaveHidden = .ShowHiddenText
    .ShowHiddenText = blnHideMe
    End With
    Debug.Print "Document paragraph count: " & ActiveDocument.Paragraphs.Count
    For Each parTemp In ActiveDocument.Paragraphs
    With parTemp
    Debug.Print "Current paragraph number: " & _
    ActiveDocument.Range(Start:=0, End:=.Range.End).Paragraphs.Count
    Select Case .Range.Characters(1).Font.Hidden
    Case 0 'False
    Debug.Print "False";
    Case -1 'True
    Debug.Print "True";
    Case Else 'wdUndefined
    Debug.Print "Mixed";
    End Select
    Debug.Print " ";
    Select Case .Range.Font.Hidden
    Case 0 'False
    Debug.Print "False";
    Case -1 'True
    Debug.Print "True";
    Case Else 'wdUndefined
    Debug.Print "Mixed";
    End Select
    Debug.Print
    Debug.Print vbTab & .Style & ": " & .Range.Text
    ' Debug.Print vbTab & .Style & ": " & _
    ' ActiveDocument.Range(Start:=.Range.Start, End:=.Range.End - 1).Text
    End With
    Next parTemp
    With ActiveWindow.View
    .ShowHiddenText = blnSaveHidden
    End With
    End Sub
    </pre>


  13. 3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Solution

    Here's my solution to the problem. A function called
    WhatsMyStyle which returns the actual stylename of the current paragraph regardless of whether or not it is preceeded by a pargraph that is hidden.

    Hope this saves someone the headache.

    <pre>Option Explicit
    Private Sub TestHideandSeekWorkAround()
    ' Code Header inserted by the Procedure Header Add-In
    '================================================= ============
    ' WordAround.TestHideandSeekWorkAround
    '-------------------------------------------------------------
    ' Purpose
    ' Demonstrates how to get the actual style of a paragraph
    ' without having to worry about whether or not the queried
    ' paragraph is preceeded by a paragraph of hidden style.
    '
    ' Author : Guy Gallo, based on code by Howard Kaikow
    '-------------------------------------------------------------
    ' 06-01-2002 :
    '================================================= ============
    ' End Code Header block


    Debug.Print "With hidden text displayed"
    HideandSeek True

    Debug.Print "With hidden text not displayed"
    HideandSeek False

    End Sub

    Private Sub HideandSeek(blnHideMe As Boolean)

    Dim blnHidden As Boolean
    Dim blnSaveHidden As Boolean
    Dim parTemp As Paragraph
    Dim aRange As Range


    With ActiveWindow.View

    blnSaveHidden = .ShowHiddenText
    .ShowHiddenText = blnHideMe

    End With

    Debug.Print "Document paragraph count: " & ActiveDocument.Paragraphs.Count

    For Each parTemp In ActiveDocument.Paragraphs

    With parTemp
    'Get the numer of paragraphs. This is depended on visible state
    Debug.Print "Current paragraph number: " & _
    ActiveDocument.Range(Start:=0, End:=.Range.End).Paragraphs.Count

    'Show underlying style and the contents of the paragraph
    'Use Function to return style
    Debug.Print vbTab & WhatsMyStyle() & ": " & parTemp.Range.Text

    End With

    Next parTemp

    With ActiveWindow.View
    .ShowHiddenText = blnSaveHidden
    End With

    End Sub



    Private Function WhatsMyStyle()
    ' Code Header inserted by the Procedure Header Add-In
    '================================================= ============
    ' WorkAroundFunction.WhatsMyStyle
    '-------------------------------------------------------------
    ' Purpose
    ' Author : Guy Gallo, 06-01-2002
    ' Notes :
    '-------------------------------------------------------------
    ' Parameters
    '-----------
    ' none
    '-------------------------------------------------------------
    ' Returns:
    ' The current paragraphs style name not matter what the
    ' previous paragraph.
    '-------------------------------------------------------------
    ' Revision History
    '-------------------------------------------------------------
    ' 06-01-2002 :
    '================================================= ============
    ' End Code Header block
    Dim aRange As Range
    Dim i As Integer

    'Get current paragraphs index
    i = ActiveDocument.Range( _
    Start:=0, End:=Selection.Paragraphs(1).Range.End).Paragraphs .Count

    'Assign paragraph mark to a range object.
    'This is the key part of the workaround. Query the paragraph mark,
    'not the paragraph.
    Set aRange = ActiveDocument.Range( _
    Start:=ActiveDocument.Paragraphs(i).Range.End - 1, _
    End:=ActiveDocument.Paragraphs(i).Range.End)

    WhatsMyStyle = aRange.Style

    End Function

    </pre>


    I'm attaching a second copy of Hidden Error.doc which contains all the code that's been tossed about in this thread.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •