Macro to open new document and then switch back to old doc, word20

A

Alan Stancliff

Is it possible to write a macro that does the following:
1. Notice what document the cursor is in
2. Open a new document. Perform some task there
3. Close the new document without saving.
4. Go back to the document that was active when the macro began

Regards,

Alan
 
J

Jay Freedman

Is it possible to write a macro that does the following:
1. Notice what document the cursor is in
2. Open a new document. Perform some task there
3. Close the new document without saving.
4. Go back to the document that was active when the macro began

Regards,

Alan

Sub demo()
Dim firstDoc As Document
Dim secondDoc As Document

' remember where you parked the car
Set firstDoc = ActiveDocument

' make a new doc based on Normal.dot,
' which automatically becomes active
Set secondDoc = Documents.Add

' do some work in secondDoc
secondDoc.Range.Text = "Hello Word"

' close without saving
secondDoc.Close SaveChanges:=wdDoNotSaveChanges

' firstDoc probably becomes active here,
' but if not then force it
firstDoc.Activate
End Sub
 
B

BHW

Jay (or anyone else),

Can you possibly expand this example to show the following:

1) find some text in the original document
2) copy and paste it in the second document
3) go back to first document
4) find more text (i.e., some loop structure that finds the text in
doc1 and transfers it to doc2)

- what I have in mind is some sort of intelligent proofreader that
will (with my codes instructions) find offending text in doc1 and put
it in doc2. I can then read only the offending text in doc2 and see if
I need to change it. As a bonus, it would be nice to put bookmarks in
doc1 when I find offending text.

Thanks,

Bruce
 
J

Jay Freedman

The last three steps are easy enough (although I wouldn't use copy/paste and
"go back" -- there are much better ways to do that). The hard part, at least
from your description, is defining what "offending text" to look for. Is it
always the same phrase, or is there some other way to recognize it?

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

BHW

Some simple criteria for offending text includes long sentences (say,
40 words or more - easy to check for) or sentences with multiple
instances of "of" (e.g., the x of y of z - probably harder to check
for). I already know how to find long sentences, but the pasting and
switching back and forth is beyond my current level.

Thanks for your interest and help :)

Bruce
 
J

Jay Freedman

Hi Bruce,

Study this example. Don't get nervous; it's more comments than code.

Sub demo()
Dim SrcDoc As Document ' the document being checked
Dim DestDoc As Document ' the document to receive text
Dim SrcRg As Range ' the text currently being checked
Dim DestRg As Range ' the place to copy it to

' As simple example, assume we're checking the
' currently active document. Could use Documents.Open
' to get user's choice instead.
Set SrcDoc = ActiveDocument

' Create a new blank doc to receive text, and the location
' in it to insert copied text.
Set DestDoc = Documents.Add
Set DestRg = DestDoc.Range
DestRg.Collapse wdCollapseEnd

For Each SrcRg In SrcDoc.Sentences
' The Sentences collection contains ranges, one per
' "sentence". Note that Word's idea of a sentence may
' not match yours, depending on how you punctuate and
' what non-text items (tables, etc.) are in the document.

' Call the function to check whether the text needs
' to be copied to the destination document.
If IsOffendingText(SrcRg) Then
' Copy the source to the destination without
' using the clipboard. Add a paragraph mark to
' separate entries.
DestRg.FormattedText = SrcRg.FormattedText
DestRg.InsertAfter vbCr

' Adjust the location for the next insertion
Set DestRg = DestDoc.Range
DestRg.Collapse wdCollapseEnd
End If
Next SrcRg

' Optional: open the Save dialog to name and save DestDoc
' DestDoc.Save
End Sub

Private Function IsOffendingText(objRg As Range) As Boolean
' Build code here to examine the text in objRg.
' Return True if it meets the criteria for offending text,
' otherwise return False.

Dim Result As Boolean

' Default value:
Result = False

' Example: offending if it contains more than 40 words
Result = (objRg.Words.Count > 40)

' Another example: offending if it starts with "But"
If Not Result Then
Result = (objRg.Words(1).Text = "But ")
End If

' Keep checking more criteria until one of them is true
' or you have checked them all...

' Finally, set the function value
IsOffendingText = Result
End Function

Things to notice: By using two range objects, one in the "source" document
and one in the "destination" document, you can copy text from one to the
other simply by assigning the FormattedText property of one to the other.
This (a) does not use the clipboard, and (b) does not change which document
is active. Everything is done by referring to the two ranges and the two
document objects; there is no mention of ActiveDocument or Selection other
than to assign SrcDoc (and, as noted, even that could be done differently).

The part that needs your attention is the code in the function
IsOffendingText that looks at the text of the current range and decides
whether it's "offending" and thus needs to be copied. The way I've shown
this setup, each sentence in the document will be examined only once, and it
will be copied if it meets any of the criteria. If you need separate lists
of sentences matching separate criteria, with the same sentence in more than
one list, then the macro needs to be restructured (there are a couple of
ways you might do that).

As noted in one of the comments, it's possible that some of the things Word
regards as a "sentence" aren't what you would expect. If you get odd-looking
results, single-step through the code with the F8 key and watch the contents
of SrcRg.Text.
 
B

BHW

Jay,

Thank you so much! I will study this and apply it. This is great.

Thanks again - very generous of you.

Bruce
 
B

BHW

The code, as sent, works fine. I tried to modify it so that it would
place bookmarks in the original document at the start (or even
spanning) the offending sentences. I tried this:

For Each SrcRg In SrcDoc.Sentences
' The Sentences collection contains ranges, one per
' "sentence". Note that Word's idea of a sentence may
' not match yours, depending on how you punctuate and
' what non-text items (tables, etc.) are in the document.

' Call the function to check whether the text needs
' to be copied to the destination document.
If IsOffendingText(SrcRg, MaxLength) Then
' Copy the source to the destination without
' using the clipboard. Add a paragraph mark to
' separate entries.
DestRg.FormattedText = SrcRg.FormattedText
DestRg.InsertAfter vbCr

' Adjust the location for the next insertion
Set DestRg = DestDoc.Range
DestRg.Collapse wdCollapseEnd

' Bookmark original document
BookmarkIndex = BookmarkIndex + 1
BookmarkStr = "longsent" & CStr(BookmarkIndex)
'SrcDoc.Bookmarks.Add Name:=CStr(BookmarkIndex)
SrcDoc.Bookmarks.Add (BookmarkStr)
End If
Next SrcRg

and it added the all the bookmarks at the start of the first offending
sentence (which happened to be the first sentence in my test
document). I tried

Srcdoc.Bookmarks.Add(BookmarkStr,SrcRg) - but Word VB did not like the
SrcRg argument. How do I give Word an argument that specifies where
the bookmark should be placed? Thanks!
 
J

Joe Geiermann

Great code!

I have a similar need to move data between two docs but in my case the two
documents are already opened in two different application instances? They are
mail merge documents that were created from Access vb code using CreateObject
for the word application so if you go to either document and chooses the
window menu it does not list the other document only itself. In the task bar
you see both documents and can switch back and forth. Neither are saved so I
can't use the file name to switch back and forth.

If I change the code that opens and generates the merge docs to GetObject
for the word application I can copy between the documents but it causes a
host of other issues I have not been able to solve.

How do you move/reference different copies of word running at the same time?

Any help would be greatly appreciated.
 
F

fumei via OfficeKB.com

Try to avoid multiple instances. Word can handle multiple documents quite
well. It i smost likely that you can do whatever it is you are doing with
ONE instance. In which case, the issue of the documents being in separated
instances simply does not arise.


Perhaps if you explain why you have to have separate instances.
 

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