#1
|
|||
|
|||
I need to capitalize first word of every line
I have a 200 page document and need to capitalize the first word in every line. They are not sentences so I can't use the existing format option. Is there macro that will do this for me? I know I can go into options and have it done automatically but that is only for new typing and won't work on existing text. |
#2
|
||||
|
||||
What constitutes a 'line' in the document? There are no 'lines' in a Word document. Formatting is by text flow between the current margins. If the 'lines' are merely an artefact of text flow then capitalising the first Word on each displayed line is likely to reflow the document as Capitals can take up more space then the lower case letters they replace.
If the 'lines' are actually paragraphs then the matter is rather more straightforward. Code:
Dim oPara As Paragraph For Each oPara In ActiveDocument.Paragraphs oPara.Range.Characters(1).Case = wdUpperCase Next oPara Set oPara = Nothing
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
The bad news is I didn't understand a word of what you said but the good news is your macro word brilliantly! Thanks for the amazingly quick response.
It is for a collection of song lyrics. |
#4
|
||||
|
||||
One out of two is not that bad
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
|||
|
|||
delboy,
Graham already knows this, but for the sake of completeness and for others looking to do something similar, I'm going to mention it. Graham is spot on about lines and pages being sort of an illusion in a Word document, but interestingly enough, in "Print Layout" view there are things called "PAGES, RECTANGLES AND LINES" that can be leveraged with VBA for useful things. Not to mention any names, but lets say we have an elderly, bumbling and forgetful politician giving a speech and we want to use a PC with an open Word document as his (or her) teleprompter. We want to avoid gaffs so we are going to automatically scroll through the document line by line highlighting the text and pausing for say half a second on each LINE. Code:
Option Explicit Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Private bStop As Boolean Sub StopScroll() bStop = True End Sub Sub ScrollByLine() Dim oPage As Page Dim lngRec As Long, lngLine As Long Dim lngHLCI As Long Dim oRng As Word.Range bStop = False For Each oPage In ActiveDocument.ActiveWindow.ActivePane.Pages For lngRec = 1 To oPage.Rectangles.Count For lngLine = 1 To oPage.Rectangles(lngRec).Lines.Count lngHLCI = oPage.Rectangles(lngRec).Lines(lngLine).Range.HighlightColorIndex oPage.Rectangles(lngRec).Lines(lngLine).Range.Select Set oRng = Selection.Range oRng.Characters(1) = UCase(oRng.Characters(1)) Selection.Collapse wdCollapseEnd oRng.HighlightColorIndex = wdYellow Doze 500 'half second DoEvents ActiveDocument.ActiveWindow.SmallScroll Down:=1 'one line oRng.HighlightColorIndex = lngHLCI Application.ScreenRefresh If bStop Then GoTo lbl_Exit Next lngLine Next lngRec Next oPage lbl_Exit: Exit Sub End Sub Sub Doze(ByVal lngPeriod As Long) DoEvents Sleep lngPeriod End Sub Graham's code worked perfectly for you because your document is set up where each line is a separate paragraph. Had you used line breaks (Shift+Enter) to create new lines or if you have simply typed several lines of text and "still" wanted each of those lines capitalized, then by extension of what you have seen above, you could have used this: Code:
Sub CapFirstLine() Dim oPage As Page Dim lngRec As Long, lngLine As Long Dim lngHLCI As Long Dim oRng As Word.Range bStop = False For Each oPage In ActiveDocument.ActiveWindow.ActivePane.Pages For lngRec = 1 To oPage.Rectangles.Count For lngLine = 1 To oPage.Rectangles(lngRec).Lines.Count oPage.Rectangles(lngRec).Lines(lngLine).Range.Characters(1) = _ UCase(oPage.Rectangles(lngRec).Lines(lngLine).Range.Characters(1)) Next lngLine Next lngRec Next oPage lbl_Exit: Exit Sub End Sub Again, as Graham has pointed out, a CAP letter takes up more space than a lower case letter so the result of running that code on multi-line paragraphs could be disappointing. |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Capitalize one to two words if they are the only words on a line | jrhlavac | Word VBA | 1 | 10-08-2015 08:19 PM |
Create exceptions to the "Capitalize Each Word" command | TonyTyner@comcast.net | PowerPoint | 0 | 03-13-2015 09:13 AM |
Newbie - Need help with VBA code to capitalize last line of addresses | jjreg | Word VBA | 2 | 11-07-2014 10:19 PM |
Only Capitalize First Letter & Delete Last Name | zulhfreelancer | Excel | 5 | 12-10-2012 07:51 AM |
capitalize first letter of sentences | norco1 | Word | 0 | 06-25-2006 12:37 PM |