MS Office Forum / Word / Programming / August 2006
Tabs to indents
|
|
Thread rating:  |
stevewy@hotmail.com - 14 Aug 2006 09:35 GMT We get documents into our section that have been typed by other people around the building who are (how can I put this?) amateur Word users. When they need to do a hanging paragraph, they type a number, TAB, then type a line, but when the cursor word-wraps to the start of the line again, they do another TAB before typing. So you end up with a paragraph with TABs at the start of every line, which of course doesn't look too good if we have to start editing the paragraph, because the text doesn't word-wrap around properly.
We get a lot of these documents in, despite attempts to instruct people to indent properly, use styles etc.
I wondered if anyone could point me in the right direction of a macro that would take the current paragraph, and replace any occurrence of a tab, after the first one, with a blank space, then hanging-indent the whole paragraph to the first tab marker (or 0.5 inch if not existing). So in other words take the unwanted Tabs out and indent the paragraph properly, like we currently have to do manually.
My command of VBA is not that advanced. Is there such a thing as FOREACH Tab in Paragraph....?
Helmut Weber - 14 Aug 2006 13:09 GMT Hi,
like this, which may not look too elegant, but without knowing more about the structure of your doc and whatever your amateur typists are doing, I thought, you may be on the safe side with this code.
Sub Test0001()
ActiveDocument.Range(0, 0).Select ' cursor to start of doc Do With Selection.Bookmarks("\line").Range If .Characters.First = Chr(9) Then .Characters.First.Delete End If ' check for end of doc If .Characters.Last.End + 1 = _ ActiveDocument.Range.End Then Selection.WholeStory With Selection.Range.ParagraphFormat .LeftIndent = CentimetersToPoints(0.63) .FirstLineIndent = CentimetersToPoints(-0.63) .TabStops.ClearAll .TabStops.Add _ Position:=CentimetersToPoints(0.63), _ Alignment:=wdAlignTabLeft, _ Leader:=wdTabLeaderSpaces End With Exit Sub End If End With With Selection .HomeKey unit:=wdLine .MoveDown End With Loop End Sub
 Signature Greetings from Bavaria, Germany Helmut Weber, MVP WordVBA "red.sys" & chr(64) & "t-online.de" Word 2002, Windows 2000 (german versions)
stevewy@hotmail.com - 14 Aug 2006 13:30 GMT Thank you for your reply, but that's not working quite as I imagined. I just really need a macro that would work only on the current paragraph, and would delete any occurrence of a tab symbol, then would do a hanging indent for the whole paragraph to 0.5 inches.
I was thinking of doing some kind of Find & Replace, but wondered if a more straightforward solution would be to locate and delete each tab code found in the active paragraph.
Helmut Weber - 14 Aug 2006 14:46 GMT ok.
How about this one:
Sub Test0002()
Dim rTmp As Range Dim lCnt As Long Set rTmp = Selection.Paragraphs(1).Range With rTmp.Find .Text = Chr(9) If .Execute Then rTmp.Collapse direction:=wdCollapseEnd rTmp.End = Selection.Paragraphs(1).Range.End Else Exit Sub End If .Text = Chr(9) .Replacement.Text = "" .Execute Replace:=wdReplaceAll End With With rTmp.Paragraphs(1).Range.ParagraphFormat .LeftIndent = CentimetersToPoints(0.63) .FirstLineIndent = CentimetersToPoints(-0.63) .TabStops.ClearAll .TabStops.Add _ Position:=CentimetersToPoints(0.63), _ Alignment:=wdAlignTabLeft, _ Leader:=wdTabLeaderSpaces End With End Sub
 Signature Greetings from Bavaria, Germany Helmut Weber, MVP WordVBA "red.sys" & chr(64) & "t-online.de" Word 2002, Windows 2000 (german versions)
stevewy@hotmail.com - 14 Aug 2006 15:09 GMT Ah that's great, Helmut. Thank you very much.
Steve
Greg Maxey - 14 Aug 2006 16:55 GMT Helmut,
Why this line? rTmp.End = Selection.Paragraphs(1).Range.End
> ok. > [quoted text clipped - 33 lines] > "red.sys" & chr(64) & "t-online.de" > Word 2002, Windows 2000 (german versions) Helmut Weber - 14 Aug 2006 17:08 GMT Hi Greg,
>Why this line? > rTmp.End = Selection.Paragraphs(1).Range.End hmm...
I had an additional line in the code before, for testing. rtmp.select. The range rTmp, after a tab is found, will be reduced to the tab, the first tab in the paragraph, if there is any tab at all. So I thought I'd have to expand it again til the end of the paragraph.
No?
All comments and improvements are welcome.
By the way, Dim lCnt As Long was left over from testing as well.
Cheers
Helmut
Greg Maxey - 14 Aug 2006 17:34 GMT Helmut,
The scope of a defined range is altered in an interesting manner using Find and Replace. Take this one sentence paragraph,
Find Helmut if Helmut can be found.
Run this code.
Sub ScratchMacro() Dim oRng As Word.Range Set oRng = Selection.Paragraphs(1).Range With oRng.Find .Text = "Helmut" If .Execute Then oRng.Select 'Point A oRng.Collapse wdCollapseEnd With oRng.Find 'Point B .Text = "Helmut" .Replacement.Font.Bold = True .Execute Replace:=wdReplaceAll 'Point C End With End If End With End Sub
While at Point A oRng is clearly limited to the text "Helmut." It must be self expanding to the end of the initial range by Point B or it couldn't find the sencond "Helmut" at Point C.
> Hi Greg, > [quoted text clipped - 22 lines] > > Helmut Helmut Weber - 14 Aug 2006 17:45 GMT Hi Greg,
I see, I'll test and double test it, but not before after tomorrow, to make sure that you are right, not to make sure that you are wrong.
Cheers
Helmut
Helmut Weber - 15 Aug 2006 03:08 GMT Hi Greg,
>Sub ScratchMacro() >Dim oRng As Word.Range [quoted text clipped - 12 lines] >End With >End Sub Yessssss!
Cheers.
Helmut Weber
|
|
|