Query tabstop in paragraph using vba in word 2013

Hi Gurus - Newbie to VBA in word, but not to macros - 

I would like to run a macro that checks how many tab indents the text starts at in a paragraph and add the number received as a digit at the start of the paragraph.

For example:

No indents

      One indent

             Two indents

and turn it into the following:

0No indents

      1One indent

             2Two indents

Should be fairly easy, but can't seem to find an answer....

Thanks!

Michael

Answer
Answer

You were clear enough. If you have tabs the macro I posted will do what you ask. Indents are a different issue and do not necessarily relate to the tab positions. They are a measure of distance. Press the ¶ button on the Home tab of the ribbon and see what you have. The first para below has tabs, the second an indent. If you have an indent then what information do you want to provide. Also with Tabs, I would only ever use one and set that appropriately.

Graham Mayor (Microsoft Word MVP 2002-2019)
For more Word tips and downloads visit my web site
https://www.gmayor.com/Word_pages.htm

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Answer
Answer

If the document was formatted correctly there wouldn't be any leading tabs. Word is a word processor not a typewriter and the leading space should have been created with an indent. However given what you have, a macro to do that would be simple enough e.g.

Dim oPara As Paragraph
Dim oRng As Range
    For Each oPara In ActiveDocument.Content.Paragraphs
        If oPara.Range.Characters(1) = Chr(9) Then 'Check if the first character in the paragraph is a tab
            Set oRng = oPara.Range ' If it is set a range to the paragraph
            oRng.Collapse wdCollapseStart 'Collapse the range to the start of the paragraph
            oRng.MoveEndWhile Chr(9) 'Move the end of the range to the end of the tabs
            oRng.InsertAfter oRng.Characters.Count ' Count the characters in the range and add the number to the end of the range
        End If
    Next oPara 'Process the next paragraph

Graham Mayor (Microsoft Word MVP 2002-2019)
For more Word tips and downloads visit my web site
https://www.gmayor.com/Word_pages.htm

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 373 Applies to: