Paste text to currently open document

S

sweet_dreams

Hi all,

I have such a problem: I wrote sub to copy text form one doc to
currently open document. I attached this sub to button I would like it
to insert text form another doc in the selection in document which I
edit. But instead of inserting text to currently open doc it copies
text to the file from which this sub takes text:

Dim OpisKryterium As New Document

Set OpisKryterium =
Application.Documents.Open(SciezkaKryterium(NrKryt))

Dim TekstOpisu As Range
Dim DlugoscTekstu As Long

DlugoscTekstu = OpisKryterium.Characters.Count

Set TekstOpisu = OpisKryterium.Range(0, DlugoscTekstu - 1)

Selection.FormattedText = TekstOpisu.FormattedText

OpisKryterium.Close
Set OpisKryterium = Nothing
Set TekstOpisu = Nothing


I think that it is because of this line:
Selection.FormattedText = TekstOpisu.FormattedText

but I tried to do it with ActiveDocument but result is the same. So how
can I reference to currently open doc.

Please help.

Regards,
sweet_dreams
 
J

Jean-Guy Marcil

sweet_dreams was telling us:
sweet_dreams nous racontait que :
Hi all,

I have such a problem: I wrote sub to copy text form one doc to
currently open document. I attached this sub to button I would like it
to insert text form another doc in the selection in document which I
edit. But instead of inserting text to currently open doc it copies
text to the file from which this sub takes text:

Dim OpisKryterium As New Document

Set OpisKryterium =
Application.Documents.Open(SciezkaKryterium(NrKryt))

Dim TekstOpisu As Range
Dim DlugoscTekstu As Long

DlugoscTekstu = OpisKryterium.Characters.Count

Set TekstOpisu = OpisKryterium.Range(0, DlugoscTekstu - 1)

Selection.FormattedText = TekstOpisu.FormattedText

OpisKryterium.Close
Set OpisKryterium = Nothing
Set TekstOpisu = Nothing


I think that it is because of this line:
Selection.FormattedText = TekstOpisu.FormattedText

but I tried to do it with ActiveDocument but result is the same. So
how can I reference to currently open doc.

Do not use Selection.

What you want is something like this:

'_______________________________________
Dim docSource As Document
Dim docTarget As Document
Dim TekstOpisu As Range
Dim DlugoscTekstu As Long

Set docTarget = ActiveDocument
Set docSource = Application.Documents.Open(SciezkaKryterium(NrKryt))
'What is SciezkaKryterium(NrKryt)?

DlugoscTekstu = docSource.Characters.Count
Set TekstOpisu = docSource.Range(0, DlugoscTekstu - 1)

docTarget.Range.FormattedText = TekstOpisu.FormattedText
'_______________________________________

It is always better to declare Document objects for all the document we
intend to manipulate in our code.
I am not sure if this is what you want... but according to your own code
this will take the full text form a document and insert it (Overwriting any
existing text) in the active document.

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

sweet_dreams

Jean-Guy Marcil,

Thanks a lot it is exactly what I wanted to achieve.

Regards,
sweet_dreams
 

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