Results 1 to 9 of 9
  1. #1
    2 Star Lounger
    Join Date
    May 2002
    Location
    Atlanta, Georgia, USA
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Detecting character formatting w VBA (Word 2000/20

    Is there any way in a macro to determine if a given character (or word or paragraph) has different formatting than its style? I want to know if the character has direct or manual formatting. It seems that this should be possible, but I can't figure out how without going to a lot of trouble. I know that you can view this information using Shift+F1, so I thought it would be exposed in the object model.

    I'm writing a macro to "tag" each paragraph (or character) with its style or formatting. Then I'll save it to text, reopen it in Word and reformat it using the tags. The real reason I'm doing this is that my files are created by pasting in HTML, and they seem inordinately bloated.

    Thanks for any suggestions!
    Bob
    Writing Consultant / Word Template Designer
    Words Connect

  2. #2
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Detecting character formatting w VBA (Word 200

    See <post#=458,340>post 458,340</post#> for one method to detect formatting that is inconsistent with the Style.

  3. #3
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Detecting character formatting w VBA (Word 2000/20

    Hi Hans,

    It would really be great if it worked, but...
    ResetChar doesn't seem to return proper values in recent versions (2000/2003), and ResetPara only in Word2000.

    In Word2003, both ResetPara and ResetChar always seem to return 0 (vbFalse).

    According to the Word95 WordBasic help file, ResetPara only looks at the first paragraph in the selection:
    0 (zero) If the first paragraph in the selection contains formatting that differs from the formatting defined for the applied paragraph style
    -1 If the first paragraph in the selection contains no formatting that differs from the formatting defined for the applied paragraph style

    <img src=/S/frown.gif border=0 alt=frown width=15 height=15> Klaus

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Detecting character formatting w VBA (Word 2000/20

    Thanks for the heads-up. I'll add a remark to my reply.

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Detecting character formatting w VBA (Word 2000/20

    Added: this doesn't work correctly - see <post#=511,353>post 511,353</post: > below.

    WordBasic.ResetChar, if used as a function, returns False if the selection contains any direct (manual) character formatting, and True if not.
    WordBasic.ResetPara, if used as a function, returns False if the selection contains any direct (manual) paragraph formatting, and True if not.

    Example:

    If WordBasic.ResetChar = False Then
    MsgBox "The selection contains direct character formatting"
    End If

    Warning: if you use WordBasic.ResetChar or WordBasic.ResetPara as instructions, they actually remove manual formatting.
    Both only work on the current selection, there is no way (as far as I know) to make them work on arbitrary ranges.

  6. #6
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Detecting character formatting w VBA (Word 2000/20

    I had hoped that it did work for you, and I had made some stupid mistake.
    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Klaus

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Detecting character formatting w VBA (Word 2000/20

    I'm using Word 2002 SP-3.

    ResetChar works correctly on single and multiple paragraphs:
    - ResetChar returns 0 if some of the text anywhere in the selection has direct character formatting.
    - ResetChar returns -1 if there is no direct character formatting at all in the selection.

    ResetPara only returns information about the first paragraph in the selection.

    As far as I can see, they do work consistently, taking the first-paragraph restriction of ResetPara into account. I tried it on a document with about 40 pages.

  8. #8
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Detecting character formatting w VBA (Word 2000/20

    So it was something stupid on my part after all? Kind of glad to hear that <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    Both ResetChar and ResetPara still don't seem to work in Word2003, even starting Word with the /a switch.
    But now WordBasic.ResetChar() does work in Word2000... No idea what I did wrong before.

    Maybe they were both broken in Word2003?

    <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15> Klaus

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Detecting character formatting w VBA (Word 2000/20

    I don't think you said anything stupid. It was useful to point out that ResetChar and ResetPara don't always work, and if ResetPara works, only on the first paragraph in the selection.

Posting Permissions

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