Pasting Selected Text

M

mcp6453

What is the VBA syntax to paste text that is selected in Doc1 into Doc2?
How can I error test such that if no text is selected in Doc1, the macro
beeps and does nothing else when executed in Doc2?
 
E

Ed

Select your text and Selection.Copy. Then activate Doc2 and
Selection.Paste. You can check the content or length of Selection.Text
before working with it. I don't know how to make the computer beep (I think
I could have fun with that one!), but I think I'd do it in Doc1 instead of
Doc2 to give you a chance to correct the selection. If you have nothing to
paste, the macro will paste nothing, which will look like it's done nothing.

Dim Doc1, Doc2 As Document

Documents(Doc1).Activate
If Selection.Text <> "" Then
Selection.Copy
Else: ' computer beep
End If

Documents(Doc2).Activate
Selection.Paste

HTH
Ed
 
M

mcp6453

Ed said:
Select your text and Selection.Copy. Then activate Doc2 and
Selection.Paste. You can check the content or length of Selection.Text
before working with it. I don't know how to make the computer beep (I think
I could have fun with that one!), but I think I'd do it in Doc1 instead of
Doc2 to give you a chance to correct the selection. If you have nothing to
paste, the macro will paste nothing, which will look like it's done nothing.

Dim Doc1, Doc2 As Document

Documents(Doc1).Activate
If Selection.Text <> "" Then
Selection.Copy
Else: ' computer beep
End If

Documents(Doc2).Activate
Selection.Paste


Thanks, Ed. Is there a way to test whether any text is selected? It is
possible to accidentally run the macro before any text has been manually
selected.
 
M

Mark Tangard

You can use:

If Selection.Start = Selection.End Then
MsgBox "Yo! Select some text first."
End Sub
End If
<---rest of your code here

A beginning user learns to put a lot of this sort of thing in the front
end of a macro, to avoid errors. But there's a flip side: It can also
help, if you're making this macro just for yourself or for just a small
group, to handle the 'nothing-selected' possibility in the opposite way,
if you'll always or often be using the macro in a specific way. For
example, if it's likely you'll very often want to copy exactly the
current paragraph, you can use:

If Selection.Start = Selection.End Then
Selection.Paragraphs(1).Range.Select
End If
<---rest of your code here

Just something to ponder.
 
P

Peter Hewett

Hi Ed

In VB/VBA the statement:
Dim Doc1, Doc2 As Document

does not in fact declare Doc1 as type document. It actually declares Doc1
as type Variant!

If you want to declare both variables as type Document use the syntax:

Dim Doc1 as Document, Doc2 as Document

Once you've assigned something to Doc1 and Doc2 you can dispense with the
documents collection and change:

Documents(Doc1).Activate
to:
Doc1.Activate

The statement "Documents(Doc1).Activate" is equivalent to
"Documents(Doc1.Name).Activate".

I hope this was of some interest + Cheers - Peter
 
E

Ed

does not in fact declare Doc1 as type document. It actually declares Doc1
as type Variant!

Thanks for the tip. I thought all items on a single line were declared as
that type. Time to go back and fix some things! May explained some
otherwise unexplained errors.
Once you've assigned something to Doc1 and Doc2 you can dispense with the
documents collection and change:

Documents(Doc1).Activate
to:
Doc1.Activate

Often, as a "newbie", I'll forgo the shorthand just to make sure I have a
firm grasp on what I'm doing. It's easier, then, for me to spell everything
out, especially if I need to go back and find where I took a wrong turn.

Thanks for helping.
Ed
 
P

Peter Hewett

Hi Ed

Without trying to be pedantic, it's not actually a case of shorthand. The
code "Documents(Doc1).Activate" only works because the default property of
the Document object is Name. So when you write "Documents(Doc1).Activate"
you're really writing "Documents(Doc1.Name).Activate".

Since you've already got a Document object (Doc1) using it to index the
Documents collection is a complete duplication in that it gives you back
EXACTLY what you give it. It's like writing "intCount = intCount".

The reason I belabour the point is that it leads to confusing and error
prone code.

Cheers - Peter
 

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