copy paste with target

  • Thread starter FIRSTROUNDKO via OfficeKB.com
  • Start date
F

FIRSTROUNDKO via OfficeKB.com

Hi!

Can somebody please tell me why this Code stops at pTarget, i think its an
easy we you know who situation.

I am trying to copy the range abc "other stuff" xyz with is not in the same
paragraph

Sub abcxyz()
Dim pH1 As Word.Range
Dim pH2 As Word.Range
Dim pTarget As Word.Range

Set pH1 = ActiveDocument.Range
With pH1.Find
.ClearFormatting
.Text = "abc"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False
End With

Set pH2 = ActiveDocument.Range
With pH2.Find
.ClearFormatting
.Text = "xyz"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False
End With

Set pTarget = ActiveDocument.Range(Start:=pH1.End + 1, End:=pH2.Start - 1)

pTarget.Copy
wrdApp.Documents.Add DocumentType:=wdNewBlankDocument
pTarget.Paste

End Sub

Thanks in advance
 
J

Jay Freedman

I suspect you macro doesn't really "stop" -- it's just that you're pasting
the contents of pTarget into pTarget (in the original document), not into
the new document. You might be able to make it work with

ActiveDocument.Range.Paste

because the Documents.Add causes the second document to become the
ActiveDocument.

The second problem, which you haven't yet run into, has to do with using
"ActiveDocument" in a macro that deals with two or more documents. It just
isn't reliable -- you can never be sure which document is active at any
given point. Instead, declare Document objects and assign them as needed.
Like this:

Dim pH1 As Word.Range
Dim pH2 As Word.Range
Dim pTarget As Word.Range
Dim Doc1 As Word.Document, Doc2 As Word.Document

Set Doc1 = ActiveDocument ' at the time the macro starts
Set Doc2 = wrdApp.Documents.Add(DocumentType:=wdNewBlankDocument)

Set pH1 = Doc1.Range
' do the first Find

Set pH2 = Doc1.Range
' do the second Find

Set pTarget = Doc1.Range(Start:=pH1.End + 1, End:=pH2.Start - 1)
pTarget.Copy
Doc2.Range.Paste

Even better, you can avoid using Copy/Paste altogether, so if the user has
something on the clipboard it won't be wiped out. Replace the last two
statements with

Doc2.Range.FormattedText = pTarget.FormattedText

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
F

FIRSTROUNDKO via OfficeKB.com

Thanks for your help

I updated my code this, the error I now get is "object required"

Sub abcxyz()


Dim pH1 As Word.Range
Dim pH2 As Word.Range
Dim pTarget As Word.Range
Dim Doc1 As Word.Document
Dim Doc2 As Word.Document

Set Doc1 = ActiveDocument ' at the time the macro starts
Set Doc2 = wrdApp.Documents.Add(DocumentType:=wdNewBlankDocument)


Set pH1 = Doc1.Range

With pH1.Find
.ClearFormatting
.Text = "abc"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False
End With

Set pH2 = Doc1.Range

With pH2.Find
.ClearFormatting
.Text = "xyz"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False
End With

Set pTarget = Doc1.Range(Start:=pH1.End + 1, End:=pH2.Start - 1)
pTarget.Copy
Doc2.Range.Paste



End Sub


Jay said:
I suspect you macro doesn't really "stop" -- it's just that you're pasting
the contents of pTarget into pTarget (in the original document), not into
the new document. You might be able to make it work with

ActiveDocument.Range.Paste

because the Documents.Add causes the second document to become the
ActiveDocument.

The second problem, which you haven't yet run into, has to do with using
"ActiveDocument" in a macro that deals with two or more documents. It just
isn't reliable -- you can never be sure which document is active at any
given point. Instead, declare Document objects and assign them as needed.
Like this:

Dim pH1 As Word.Range
Dim pH2 As Word.Range
Dim pTarget As Word.Range
Dim Doc1 As Word.Document, Doc2 As Word.Document

Set Doc1 = ActiveDocument ' at the time the macro starts
Set Doc2 = wrdApp.Documents.Add(DocumentType:=wdNewBlankDocument)

Set pH1 = Doc1.Range
' do the first Find

Set pH2 = Doc1.Range
' do the second Find

Set pTarget = Doc1.Range(Start:=pH1.End + 1, End:=pH2.Start - 1)
pTarget.Copy
Doc2.Range.Paste

Even better, you can avoid using Copy/Paste altogether, so if the user has
something on the clipboard it won't be wiped out. Replace the last two
statements with

Doc2.Range.FormattedText = pTarget.FormattedText
[quoted text clipped - 39 lines]
Thanks in advance
 
J

Jay Freedman

Ah, I missed that in your first post: For each of the Find operations,
you've set the parameters but you never called the .Execute method. That
means both pH1 and pH2 remain the same as Doc1.Range. In fact, I got a
different error -- when the macro tried to assign pTarget, it got an "out of
range" error because pH1.End + 1 is past the end of the document.

In two places, after .MatchWildcards = False, add

.Execute

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
Thanks for your help

I updated my code this, the error I now get is "object required"

Sub abcxyz()


Dim pH1 As Word.Range
Dim pH2 As Word.Range
Dim pTarget As Word.Range
Dim Doc1 As Word.Document
Dim Doc2 As Word.Document

Set Doc1 = ActiveDocument ' at the time the macro starts
Set Doc2 = wrdApp.Documents.Add(DocumentType:=wdNewBlankDocument)


Set pH1 = Doc1.Range

With pH1.Find
.ClearFormatting
.Text = "abc"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False
End With

Set pH2 = Doc1.Range

With pH2.Find
.ClearFormatting
.Text = "xyz"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False
End With

Set pTarget = Doc1.Range(Start:=pH1.End + 1, End:=pH2.Start - 1)
pTarget.Copy
Doc2.Range.Paste



End Sub


Jay said:
I suspect you macro doesn't really "stop" -- it's just that you're
pasting the contents of pTarget into pTarget (in the original
document), not into the new document. You might be able to make it
work with

ActiveDocument.Range.Paste

because the Documents.Add causes the second document to become the
ActiveDocument.

The second problem, which you haven't yet run into, has to do with
using "ActiveDocument" in a macro that deals with two or more
documents. It just isn't reliable -- you can never be sure which
document is active at any given point. Instead, declare Document
objects and assign them as needed. Like this:

Dim pH1 As Word.Range
Dim pH2 As Word.Range
Dim pTarget As Word.Range
Dim Doc1 As Word.Document, Doc2 As Word.Document

Set Doc1 = ActiveDocument ' at the time the macro starts
Set Doc2 = wrdApp.Documents.Add(DocumentType:=wdNewBlankDocument)

Set pH1 = Doc1.Range
' do the first Find

Set pH2 = Doc1.Range
' do the second Find

Set pTarget = Doc1.Range(Start:=pH1.End + 1, End:=pH2.Start - 1)
pTarget.Copy
Doc2.Range.Paste

Even better, you can avoid using Copy/Paste altogether, so if the
user has something on the clipboard it won't be wiped out. Replace
the last two statements with

Doc2.Range.FormattedText = pTarget.FormattedText
[quoted text clipped - 39 lines]
Thanks in advance
 
F

FIRSTROUNDKO via OfficeKB.com

thanks, here is my solution

Sub abcxyz()

Dim pH1 As Word.Range
Dim pH2 As Word.Range
Dim pTarget As Word.Range
Dim Doc1 As Word.Document

Set Doc1 = ActiveDocument ' at the time the macro starts



Set pH1 = Doc1.Range

With pH1.Find
.ClearFormatting
.Text = "abc"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False
.Execute
End With

Set pH2 = Doc1.Range

With pH2.Find
.ClearFormatting
.Text = "xyz"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False
.Execute
End With

Set pTarget = Doc1.Range(Start:=pH1.End + 1, End:=pH2.Start - 1)
Word.Documents.Add
pTarget.Copy
ActiveDocument.Range.Paste

End Sub



Jay said:
Ah, I missed that in your first post: For each of the Find operations,
you've set the parameters but you never called the .Execute method. That
means both pH1 and pH2 remain the same as Doc1.Range. In fact, I got a
different error -- when the macro tried to assign pTarget, it got an "out of
range" error because pH1.End + 1 is past the end of the document.

In two places, after .MatchWildcards = False, add

.Execute
Thanks for your help
[quoted text clipped - 84 lines]
 

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