Re: How to create Macro that will prefix all lines with a character? by Graham
Graham
Tue Jan 06 06:54:39 CST 2004
The code pre-supposes that your document is formatted in paragraphs each of
which occupies more than one line, and it is in the paragraphs where the
problems lie. If the text comprises a range of lines each terminated with a
paragraph mark (or line feed mark) then the job is somewhat simpler and can
be handled in the way you require. However, the conversation has moved on
while I have been sleeping and the later suggestions should do what you
require.
--
<>>< ><<> ><<> <>>< ><<> <>>< <>>< ><<>
Graham Mayor - Word MVP
E-mail gmayor@mvps.org
Web site www.gmayor.com
Word MVP web site www.mvps.org/word
<>>< ><<> ><<> <>>< ><<> <>>< <>>< ><<>
Cumulous wrote:
> The code sample I quoted in my initial message is able to process
> a single line. What I need is a macro that will process an entire
> selected block of text.
>
> As for your mentioning that there is no such thing as a "line" in
> Word - I understand where you are coming from. However, Word is able
> to understand the concept because it's right there in the Status Bar
> on the bottom of the screen "Ln X" where X is the line you are on.
> This has nothing to do with Line Breaks, but simply the visual
> structure of the document itself as it is displayed at that moment.
>
> Therefore, is it not possible to write a macro that basically
> does this (in plain text instructions):
>
> 1. Go to the first line in the selected block of text.
> 2. Enter "Home Key", "> ", "Down Arrow", "Home Key"
> 3. Repeat until you reach the bottom of the selected block of text.
> 4. End Macro
>
>
> That doesn't seem like it should be that impossible. I have the
> structure to enter the keys and text that are listed in Step 2. I
> just don't know how to implement the processing of a selected block
> of text.
>
> It has to be possible somehow - even if it's some form of:
>
> 1. Set FirstLine to the Line Number of the first line in the
> selection
> 2. Set LastLine to the Line Number of the last line in the selection
> 3. Set TotalLines to (LastLine - FirstLine)
> 4. And then start implementing Step 2 from the first section above
> while incrementing a counter until you have reached what should be
> the last line.
>
>
> It can't be impossible, if Outlook can do it, can it?
>
>
>
> Cumulous
>
>
>
> "Graham Mayor" <gmayor@mvps.org> wrote in message
> news:uqABS260DHA.2156@TK2MSFTNGP12.phx.gbl...
>> This is not as simple as you imagine, as there is no such thing as a
>> 'line' in Word. A line of text is an arbitrary entity determined by
>> a variety of factors, not least of which is the current printer
>> driver, and adding your character will screw up even that
>> formatting; however, you can mark individual 'lines' using the
>> following bit of code, and if you assign the macro to a keyboard
>> shortcut or toolbar button, it is a simple process to mark a block
>> of text, line by line:
>>
>> With Selection
>> .HomeKey Unit:=wdLine
>> .TypeText Text:=">"
>> .EndKey Unit:=wdLine
>> .TypeBackspace
>> .TypeParagraph
>> .MoveUp Unit:=wdLine, Count:=1
>> .Style = ActiveDocument.Styles("Normal")
>> .MoveDown Unit:=wdLine, Count:=1
>> End With
>>
>> --
>> <>>< ><<> ><<> <>>< ><<> <>>< <>>< ><<>
>> Graham Mayor - Word MVP
>> E-mail gmayor@mvps.org
>> Web site www.gmayor.com
>> Word MVP web site www.mvps.org/word
>> <>>< ><<> ><<> <>>< ><<> <>>< <>>< ><<>
>>
>>
>> Cumulous wrote:
>>> This is too unwieldy. Users need to do this for sections of
>>> text in the middle of a document. They cannot start copy/pasting
>>> sections to a new document, saving it under a different format,
>>> running a
>>> macro, copy/pasting it back, etc.... Their productivity would
>>> grind to a screaching halt.
>>>
>>> I can't imagine that this is such a hard thing to program.
>>> After all, just about every email program in existence does this -
>>> including Outlook (which uses Word to edit emails).
>>>
>>> I have seen far more complex macros in use - is this one really
>>> so difficult to produce?
>>>
>>>
>>>
>>>
>>> Cumulous
>>>
>>>
>>>
>>>
>>> "Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL
>>> ADDRESS" <dkr@mOSTvALUABLEpROFESSIONALs.org> wrote in message
>>> news:%23sQlrUo0DHA.1668@TK2MSFTNGP10.phx.gbl...
>>>> Hi Cumulous,
>>>>
>>>> Probably the easiest way to do this is to save the section of text
>>>> as a .txt file, accepting the option to terminate each line with a
>>>> carriage return, and then to open the file again in Word and use
>>>> Edit replace to replace each ^p with >^p.
>>>>
>>>> Please post any further questions or followup to the newsgroups for
>>>> the benefit of others who may be interested. Unsolicited questions
>>>> forwarded directly to me will only be answered on a paid consulting
>>>> basis.
>>>>
>>>> Hope this helps
>>>> Doug Robbins - Word MVP
>>>> "Cumulous" <Cumulous128@yahoo.com> wrote in message
>>>> news:%23%23vOXkj0DHA.1744@TK2MSFTNGP12.phx.gbl...
>>>>>
>>>>> Hi! I need a VBA Macro that will process a selected section
>>>>> of text as Quoted Text - similar to how email programs do it when
>>>>> you Reply to a Plain Text message.
>>>>>
>>>>> Basically, it needs to take a selected section of text and
>>>>> prefix every line with a ">" character, whether the selected lines
>>>>> have a Line Break at the end, or if they are simply wrapped around
>>>>> the next line.
>>>>>
>>>>> Creating a Macro that prefixes a single line of text is easy:
>>>>>
>>>>> -------------------------------
>>>>>
>>>>> Sub QuoteLine()
>>>>>
>>>>> Selection.HomeKey Unit:=wdLine
>>>>> Selection.TypeText Text:=">"
>>>>> Selection.MoveDown Unit:=wdLine, Count:=1
>>>>> Selection.HomeKey Unit:=wdLine
>>>>>
>>>>> End Sub
>>>>>
>>>>> ---------------------------------
>>>>>
>>>>>
>>>>> But I don't know how to alter this code so that it processes a
>>>>> selected section of text.
>>>>>
>>>>> Can anyone help?
>>>>>
>>>>> Thanks!