Copy from variant to clipboard

K

keith

Hello

I have some text in a variant variable created with...

Dim MyVariable as variant
MyVariable = "some text"

How do I copy that to the clipboard?

I tried...

selection.copy = MyVariable

but received an error.

any suggestions?

Keith
 
J

Jay Freedman

keith said:
Hello

I have some text in a variant variable created with...

Dim MyVariable as variant
MyVariable = "some text"

How do I copy that to the clipboard?

I tried...

selection.copy = MyVariable

but received an error.

any suggestions?

Keith

Hi Keith,

You can do either of two things:

(1) Insert the content of the variable into the document and then cut it
from there into the clipboard:

Dim MyString As Variant
Dim MyRange As Range
MyString = "some text"

' save the selection
Set MyRange = Selection.Range

With Selection
.Collapse wdCollapseStart
.Text = MyString
.Cut
End With

' restore the selection
MyRange.Select

OR (2) see http://word.mvps.org/FAQs/MacrosVBA/ManipulateClipboard.htm for a
way to place the text directly into the clipboard without messing around in
the document.

One other thing: experience tells me that people often ask about ways to use
the clipboard when they really don't need to use the clipboard at all.
What's the main task you're trying to do?
 
J

Jean-Guy Marcil

keith was telling us:
keith nous racontait que :
Hello

I have some text in a variant variable created with...

Dim MyVariable as variant
MyVariable = "some text"

How do I copy that to the clipboard?

I tried...

selection.copy = MyVariable

This copies the currently selected range in the document, nothing to do with
variables.
but received an error.

any suggestions?

See
http://word.mvps.org/FAQs/MacrosVBA/ManipulateClipboard.htm

What are you trying to do?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
K

keith

Hello Jean-Guy and Jay,

Thank you each for your responses. It looks like there is some potential in
that link you sent, although it appears to be a work-around. Pasting to the
document and then cutting from the paste can be problematic. Still, both
posts are very helpful. Jean-Guy asked what I am trying to do. It is this.
I need to be able to read, sort, and print the TA field so I can create my
own Table of references, in my own design. This means that I need to be able
to read the text (with formatting) into an array, sort the array, eliminate
duplicates, and print the modified array. So far, all the text manipulation
code in VBA brings along the characters, but leaves off italic formatting.

I look forward to your thoughts.

keith
 
J

Jean-Guy Marcil

keith was telling us:
keith nous racontait que :
Hello Jean-Guy and Jay,

Thank you each for your responses. It looks like there is some
potential in that link you sent, although it appears to be a
work-around. Pasting to the document and then cutting from the paste
can be problematic. Still, both posts are very helpful. Jean-Guy
asked what I am trying to do. It is this. I need to be able to read,
sort, and print the TA field so I can create my own Table of
references, in my own design. This means that I need to be able to
read the text (with formatting) into an array, sort the array,
eliminate duplicates, and print the modified array. So far, all the
text manipulation code in VBA brings along the characters, but leaves
off italic formatting.

But, even if you copy the text into the clipboard, as soon as you sort it
through the array variable, you will lose formatting anyway.
How does the clipborad help in this case?

Why not read the code from the TA field and modify it so it outputs what you
want?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jay Freedman

Hi Keith,

As I said, hacking the clipboard is often not the preferable way of solving
a problem. Knowing the overall objective helps in selecting a better method.

In your case, I would create a separate blank document as a scratch pad.
Declare and define two Range objects, one in the source document and one in
the scratch pad document. Set the source range to cover the first piece of
desired text (whether by its style, by its content or location, or from the
Selection). Then use a statement like this:

DestRange.FormattedText = SourceRange.FormattedText

to copy both the text and the formatting of the source into the
destination. This doesn't use the clipboard at all!

Now collapse the destination range and move it to the end of the scratch
pad, insert a paragraph mark there, move the source range to the next piece
of text in the original document, and repeat... until all needed text has
been copied.

Finally, sort the scratch pad, delete the duplicates, and print. You can
save the scratch pad as a separate document, discard it, or copy the
FormattedText of the entire scratch pad into a range in the original
document.
 
K

keith

Hello Jay and Jean-Guy

Thank you again for your responses. I'm going to need to study these for a
while to work through the details. Although I have been thinking about going
through this extended VBA solution to the problem I face, maybe one of you
knows an easier solution to the root issue. The root problem is that I can
create a TA in the APA (American Psychological Association) required style.
However, when try to print the table of autorities, Word includes both a
dotted tab and a page number. I can turn off the dotted tab, but not the
page number. APA Style requires that there not be either a dotted tab or a
page number. Is there any way to use VBA to turn off the page number?

Keith
 
J

Jean-Guy Marcil

keith was telling us:
keith nous racontait que :
Hello Jay and Jean-Guy

Thank you again for your responses. I'm going to need to study these
for a while to work through the details. Although I have been
thinking about going through this extended VBA solution to the
problem I face, maybe one of you knows an easier solution to the root
issue. The root problem is that I can create a TA in the APA
(American Psychological Association) required style. However, when
try to print the table of autorities, Word includes both a dotted tab
and a page number. I can turn off the dotted tab, but not the page
number. APA Style requires that there not be either a dotted tab or
a page number. Is there any way to use VBA to turn off the page
number?

If you are not using categories, you could use a TOC field with TC fields
instead. TC fields allow a switch that suppresses page numbering, TA fields
don't.

You could still have the regular TOC based on outlining, and your TOA would
in fact be a TOC based on TC fields.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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