Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2007

Tip: Looking for answers? Try searching our database.

copy paste with target

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
FIRSTROUNDKO - 13 Jul 2007 15:34 GMT
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
Jay Freedman - 13 Jul 2007 15:56 GMT
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

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

> Hi!
>
[quoted text clipped - 39 lines]
>
> Thanks in advance
FIRSTROUNDKO - 13 Jul 2007 16:45 GMT
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

>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
[quoted text clipped - 40 lines]
>>
>> Thanks in advance
Jay Freedman - 13 Jul 2007 17:06 GMT
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

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

> Thanks for your help
>
[quoted text clipped - 84 lines]
>>>
>>> Thanks in advance
FIRSTROUNDKO - 13 Jul 2007 17:23 GMT
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

>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
[quoted text clipped - 11 lines]
>>>>
>>>> Thanks in advance
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.