• Detecting character formatting w VBA (Word 2000/20

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Detecting character formatting w VBA (Word 2000/20

    Author
    Topic
    #423268

    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

    Viewing 1 reply thread
    Author
    Replies
    • #968096

      See post 458,340 for one method to detect formatting that is inconsistent with the Style.

    • #968051

      Added: this doesn’t work correctly – see post 511,353 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.

      • #968339

        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

        frown Klaus

        • #968355

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

          • #968416

            I had hoped that it did work for you, and I had made some stupid mistake.
            cheers Klaus

            • #968421

              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.

            • #968432

              So it was something stupid on my part after all? Kind of glad to hear that grin

              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?

              shrug Klaus

            • #968436

              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.

    Viewing 1 reply thread
    Reply To: Detecting character formatting w VBA (Word 2000/20

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: