more elegant way than switching back and forth between windows 
Author Message
 more elegant way than switching back and forth between windows

I've written a lot of macros for myself that gather certain text in
one document and put it in a second document.

Since it's the only way I know how, I just open the second document
window, paste, then reopen the first window and let the search
continue.

I've noticed that the going back and forth between windows seems to
slow down things enormously.

Is there some shortcut code that can just have the selected text
pasted into the second document without going my current route of
opening the second document window, pasting, then reopening the first
document window?

I looked in Help and Object browser under Insert but couldn't see how
do make it work in my situation.

Thanks as always!

Jerry



Sun, 02 Nov 2003 15:24:18 GMT  
 more elegant way than switching back and forth between windows

I assume you've tried Application.ScreenUpdating = False already.
But even if you have, the more central issue is probably that
you're not using Range objects for your pasting.  The main difference
in using Ranges is that you can address (and paste into) a place in
a document without making that document the active document, so you
don't get the switching frenzy.  Here's a sloppy example.  This'll
paste the 4th sentence of the active document (your "first" document)
into the second document (at the end of it -- I'm assuming your
second doc is empty to start with; if not it's not a big deal to
adjust) without ever activating the second document except at the
beginning when it's opened.

Dim DocX As Document, DocY As Document, r As Range
Set DocX = ActiveDocument
Set DocY = Documents.Open("C:\m\MySecondDocument.doc")

DocX.Activate
ActiveDocument.Sentences(4).Copy
Set r = DocY.Range
r.Start = DocY.Range.End
r.Paste

Ranges are quite often the answer to the too-much-screen-activity
problem.


------ WWW: http://www.speakeasy.org/~mtangard ----------------------
------------- "Life is nothing if you aren't obsessed." --John Waters
---------------------------------------------------------------------

Quote:

> I've written a lot of macros for myself that gather certain text in
> one document and put it in a second document.

> Since it's the only way I know how, I just open the second document
> window, paste, then reopen the first window and let the search
> continue.

> I've noticed that the going back and forth between windows seems to
> slow down things enormously.

> Is there some shortcut code that can just have the selected text
> pasted into the second document without going my current route of
> opening the second document window, pasting, then reopening the first
> document window?

> I looked in Help and Object browser under Insert but couldn't see how
> do make it work in my situation.

> Thanks as always!

> Jerry



Sun, 02 Nov 2003 15:56:53 GMT  
 more elegant way than switching back and forth between windows
Hi JMB,

Mark's answer gets you some of the way, but you can go much further than
that.
You can avoid using the clipboard altogether.

First of all, learn to love Ranges. You can only have one Selection at a
time, but as many Ranges defined as you like, in as many different documents
as you have open. A Range has many of the attributes of the Selection. Most
of the methods that can be applied to the Selection can also be applied to a
Range variable.

To start out on this, take a relatively small macro that you have written
using the Selection. Put the following line at the start

Dim MyRange as Range
Set MyRange = Selection.Range

That defines a range object variable called MyRange, and sets it so that it
is located at the same position as the current selection.

Then, wherever you have a line of code that says Selection.something,
replace it with MyRange.something.

You may get some compile errors when you first run the macro, so take a look
and see what the problem is. Most likely you have used the Homekey method to
move you to the top of the document, or something like that which can't be
done with a Range. There is almost always a way of doing the same thing. In
this case, this has the same effect

MyRange.Move Unit:=wdStory, Count:=-1

Once you have got used to the idea of using one Range variable instead of
the Selection, you can start getting more ambitious. If you need a
placeholder that you will go back to later in the macro, define a Range for
it. If you want to transfer unformatted text between two places (or even
between two documents), then you can find out that the text is at one place
by using the Text property of the Range, and insert it at the destination
using the InsertAfter method of the destination range. Something like this
would achieve that effect

MyNewRange.InsertAfter Text:=MyOldRange.Text

Almost everything within a document has a Range, so you don't necessarily
need to define a Range variable. If you know that you want to set the
contents of the second cell of the third row of the first table in the
document, then you can do it this way

ActiveDocument.Tables(1).Cell(3, 2).Range.Text = "My new text"

Finally, you can copy *formatted* text within and between documents without
the clipboard, by making use of the FormattedText property.

This example copies the text and formatting from the selection into a new
document.

Set myRange = Selection.FormattedText
Documents.Add.Content.FormattedText = myRange

Ranges have a number of advantages over the Selection.

1. They are usually faster to work with. When you move a Range variable, the
screen doesn't update in order to display where the Range has moved to. This
saves time. Also, when you start working on a Range in a different document,
you don't have to activate that document first.

2. You can have many of them scattered among several open documents, and its
easy to keep track of things using them.

There are a few occasions there the Selection has an advantage, where a
Range won't really do.

1. If you want to use the predefined bookmarks such as /Page or
/HeadingLevel. These are always defined relative to the Selection.

2. If you want to move up or down one line (as apposed to one paragraph) or
want to identify the current line of text.

3. If you want to use some items returned by the Information property.
Getting the position of some text on the page seems to work a bit more
reliably with the Selection than with a Range.

4. In Word 97, using the Selection to do a Find is noticeably faster than
using a Range variable. In Word 2000, the speed is about the same. I haven't
yet checked Word 2002 to see what the relative performance is.

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
Word FAQs at http://www.multilinker.com/wordfaq
Please post any follow-up in the newsgroup. I do not reply to Word questions
by email


Quote:
> I've written a lot of macros for myself that gather certain text in
> one document and put it in a second document.

> Since it's the only way I know how, I just open the second document
> window, paste, then reopen the first window and let the search
> continue.

> I've noticed that the going back and forth between windows seems to
> slow down things enormously.

> Is there some shortcut code that can just have the selected text
> pasted into the second document without going my current route of
> opening the second document window, pasting, then reopening the first
> document window?

> I looked in Help and Object browser under Insert but couldn't see how
> do make it work in my situation.

> Thanks as always!

> Jerry



Sun, 02 Nov 2003 17:38:33 GMT  
 more elegant way than switching back and forth between windows
Jonathan and Mark, thanks for the useful responses!

Jonathan, your "tutorial" was terrific!

One question based on 4. below: If I'm going down a list and after
each paragraph is selected searching for the contents of that
paragraph in another document, is this the kind of situation where in
Word 97 the Find will be faster than the selection?

Jerry



Quote:
>4. In Word 97, using the Selection to do a Find is noticeably faster than
>using a Range variable. In Word 2000, the speed is about the same. I haven't
>yet checked Word 2002 to see what the relative performance is.



Mon, 03 Nov 2003 12:06:50 GMT  
 more elegant way than switching back and forth between windows
Hi Jerry,

That's not quite what I meant. What I was thinking of was a situation like
this

Imaging you wanted to do something to every paragraph that contains the word
"Jonathan" - for instance you want to color the whole paragraph blue. You
could do this

Dim MyRange as Range
Set MyRange = ActiveDocument.Content
MyRange.Move Unit:=wdStory, Count:=-1
With MyRange.Find
    .Text = "Jonathan"
    .Format = False
    .Wrap = wdFindStop
    Do While .Execute
        MyRange.Paragraphs(1).Range.Font.ColorIndex = wdColorBlue
    Loop
End With

In Word 97, for reasons that are a mystery to me, it is usually faster to
use the selection, like this

Application.ScreenUpdating = False
Selection.Move Unit:=wdStory, Count:=-1
With Selection.Find
    .Text = "Jonathan"
    .Format = False
    .Wrap = wdFindStop
    Do While .Execute
        Selection.Paragraphs(1).Range.Font.ColorIndex = wdColorBlue
    Loop
End With

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
Word FAQs at http://www.multilinker.com/wordfaq
Please post any follow-up in the newsgroup. I do not reply to Word questions
by email


Quote:
> Jonathan and Mark, thanks for the useful responses!

> Jonathan, your "tutorial" was terrific!

> One question based on 4. below: If I'm going down a list and after
> each paragraph is selected searching for the contents of that
> paragraph in another document, is this the kind of situation where in
> Word 97 the Find will be faster than the selection?

> Jerry



> >4. In Word 97, using the Selection to do a Find is noticeably faster than
> >using a Range variable. In Word 2000, the speed is about the same. I
haven't
> >yet checked Word 2002 to see what the relative performance is.



Mon, 03 Nov 2003 16:37:21 GMT  
 more elegant way than switching back and forth between windows
One last thing. The difference in performance in Find disappears in Word
2000, and may even be the other way round in Word 2002 (I haven't had time
to check that yet).

I only mention the difference because from time to time I write macros that
do some very heavy-duty document processing on large batches of documents,
involving hundreds of thousands of Find operations across tens of thousands
of pages of documents. With applications like that, even minor increases in
execution speed are worth working for :-)

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
Word FAQs at http://www.multilinker.com/wordfaq
Please post any follow-up in the newsgroup. I do not reply to Word questions
by email


Quote:
> Hi Jerry,

> That's not quite what I meant. What I was thinking of was a situation like
> this



Mon, 03 Nov 2003 17:48:00 GMT  
 more elegant way than switching back and forth between windows
I understand now.  Thanks!



Quote:
>One last thing. The difference in performance in Find disappears in Word
>2000, and may even be the other way round in Word 2002 (I haven't had time
>to check that yet).

>I only mention the difference because from time to time I write macros that
>do some very heavy-duty document processing on large batches of documents,
>involving hundreds of thousands of Find operations across tens of thousands
>of pages of documents. With applications like that, even minor increases in
>execution speed are worth working for :-)



Tue, 04 Nov 2003 13:07:28 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. OVC crashes Outlook when switching back and forth continuously between folders

2. Newbie Question- switching back and forth between two forms

3. Switching back to a particular window that is already on top of z order

4. Passing Variables Back And Forth From Access To Word

5. Linking Access tables to MS Outlook (back and forth)

6. Send the form back and forth

7. BinHex conversion back and forth

8. AT Commands - Back and Forth

9. How to Scroll Text in a Text Box back and forth accross a Fomr in VB

10. back and forth between vbscript and javascript

11. Sending data back and forth with current object

12. browsing back and forth through recordset...?

 

 
Powered by phpBB® Forum Software