Selecting text in a macro

S

Sophie

Hi,
I regularly copy and paste text from numerous different sources into a Word
document. I want to create a macro to select the text I have just pasted,
and automatically format it. The size of the passage of text varies from one
line to several paragraphs, but obviously I would like to use the same macro
each time. How do I get Word to select a piece of text I have just pasted?
Many thanks for any advice you may be able to offer
Sophie
 
G

Graham Mayor

Why not simply use edit paste special > unformatted text and the formatting
will match the formatting of the style you paste into. Thus there is no need
to subsequently reformat it.

If you want to do that with a macro

Sub PasteUnfText()
On Error GoTo Oops
Selection.PasteSpecial DataType:=wdPasteText, Placement:= _
wdInLine
End
Oops:
Beep
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Sophie

Hi Graham, thanks for getting back to me so quickly.
Unfortunately I don't think your idea would be suitable, because I don't
want the pasted text to be formatted the same as the rest of the document.
Different people are contributing to this document by e-mail, and I want each
person to have a different formatting... I want to be able to create a
series of macros, each of which is specific to a particular person , and
which formats the text accordingly.
By the way,
I should say that this is my first attempt at a macro, and I don't
understand your directions:) do I copy and paste that into the macro code
section?
(I'm afraid anybody answering this question will have to pitch their
response at a slightly more basic level!)
However, I really do appreciate your trying to help me, and any further
advice would be greatly appreciated.
 
J

Jonathan West

Sophie said:
Hi,
I regularly copy and paste text from numerous different sources into a
Word
document. I want to create a macro to select the text I have just pasted,
and automatically format it. The size of the passage of text varies from
one
line to several paragraphs, but obviously I would like to use the same
macro
each time. How do I get Word to select a piece of text I have just
pasted?
Many thanks for any advice you may be able to offer
Sophie

Try this

Dim iStart as Long
iStart = Selection.Start
Selection.Paste
Selection.Start = iStart

This pastes the contents of the clipboard and selects the pasted text. You
can then format it how you wish.

You need to have the paste and select as part of the same macro, because you
need to save the position of the start of the selection as it was before you
pasted, so that you can mave the start of the selection back there
afterwards.
 
G

Graham Mayor

I would still use the paste special approach, and have a different (say
coloured) style for each contributor. As I don't know how many contributors
you have, I have assumed 4 in the following example:

Sub PasteContributions()
Dim iStyle As Integer
iStyle = InputBox("For Contributor John, enter 1" & vbCr & _
"For Contributor Fred, enter 2" & vbCr & _
"For Contributor Bill, enter 3" & vbCr & _
"For Contributor John, enter 4", , 1)
On Error GoTo Oops
With Selection
.Style = "Contributor " & iStyle
.PasteAndFormat (wdFormatSurroundingFormattingWithEmphasis)
'.PasteSpecial DataType:=wdPasteText, Placement:= _
wdInLine
End With
End
Oops:
Beep
End Sub

Create your paragraph styles and name them
Contributor 1, Contributor 2, Contributor 3 and Contributor 4
Run the macro and pick the style number. The macro will format with the
appropriate style.

See http://www.gmayor.com/installing_macro.htm


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Sophie

hi,
Graham, you wrote:
Create your paragraph styles and name them
Contributor 1, Contributor 2, Contributor 3 and Contributor 4
Run the macro and pick the style number. The macro will format with the
appropriate style.
Where do I do this? I have word 2000, and I'm struggling to understand you
-- even though I did look at your 'idiots guide to creating macros'! :)
 
S

Sophie

Thanks Jonathan,
you said:
This pastes the contents of the clipboard and selects the pasted text. You
can then format it how you wish.

After a bit of guesswork, I worked out how to put this into a macro, and it
worked -- thank you. However, I now want to be able (as part of the macro)
to select a particular font, font size and colour of text. For example, say
the email is from Judith, I want her e-mails to be in red, arial size 9. So
I want to be able to select a macro which does the bit that you've already
taught me above, but then also changes the formatting of the text.
Ultimately I want to have half a dozen macro shortcuts on the toolbar, each
of which relates to a different person, so that I copy their e-mail, and then
simply press one button to have it pasted into the Word document, selected
and reformatted in their particular style.

I have absolutely no idea how to do this! Can you help? :)
thanks for the advice so far
 
G

Graham Mayor

Word formats its documents using styles. You can create styles in the
document. What I am suggesting is that you create a unique style for each
contributor, formatted as you want the inserted text from that contributor
to appear, and name the styles Contributor 1, Contributor 2, Contributor 3
and Contributor 4 etc

The macro I posted will then ask you to enter the number associated with the
contributor and paste the text formatted with the appropriate style.

Create those four different styles - in a document. Copy the macro to the
macro editor using the method shown in my web page, and with some text
copied to the clipboard, run the macro :)
 
J

Jonathan West

Sophie said:
Thanks Jonathan,
you said:

After a bit of guesswork, I worked out how to put this into a macro, and
it
worked -- thank you. However, I now want to be able (as part of the
macro)
to select a particular font, font size and colour of text. For example,
say
the email is from Judith, I want her e-mails to be in red, arial size 9.
So
I want to be able to select a macro which does the bit that you've already
taught me above, but then also changes the formatting of the text.
Ultimately I want to have half a dozen macro shortcuts on the toolbar,
each
of which relates to a different person, so that I copy their e-mail, and
then
simply press one button to have it pasted into the Word document, selected
and reformatted in their particular style.

I have absolutely no idea how to do this! Can you help? :)
thanks for the advice so far

Rather than just give you code for one item, I'm going to provide a way for
you to explore this for yourself, so you can decide what you want to do.
Take some time to read this article

Getting To Grips With VBA Basics In 15 Minutes
http://www.word.mvps.org/FAQs/MacrosVBA/VBABasicsIn15Mins.htm

Once you have been through the article and how you can do things with the
ActiveDocument object, experiment with doing the same with the Selection
object. You will for instance find that you can set the Font of the
Selection object, and that the Font in turn has properties such as Size,
Color and Name.

Take some time over it, you have a whole new world about to open up before
you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top