PDA

View Full Version : [SOLVED:] Word 2003 document numbers change when reading in a new document



johndavidson
08-05-2014, 07:52 AM
I have a macro that picks up a string from one doc and searches for it in another. It works fine as long as there are only two docs open. When a third or subsequent doc is opened it gets confused because Word seems to renumber the Word documents, and what were once docs 1 & 2 may no longer be so numbered. I can't see the pattern to what Word is doing. Previously, I was using ActiveWindow.Next.Activate / ActiveWindow.Previous.Activate but that method also had its problems because of the dynamic renumbering of docs in which Word indulges. Has anyone got any bright ideas? Here's the code snippet that handles the docs.

' First, I open just the two docs, which become numbered as docs 1 & 2
' I then run the macro, with the active doc set to one of the two docs
' It arrives at this point in the code having collected the string from the active doc
' It now needs to set the other doc as the active doc and search for the string
' This is the code that switches from doc 1 to doc 2, or doc 2 to doc 1

' That bit works - except that if I have opened a third document, the active doc from which the string was collected may have changed its doc number, and is no longer doc 1 or 2, and likewise the doc to be searched!
' If the active doc is not doc 1 or 2, then the macro gives up

If Documents.Count = 1 Then ' Must be at least 2 docs open
MsgBox "There is no second document to search."
End
Else
doc1Name = Documents(1).Name
doc2Name = Documents(2).Name

If ActiveDocument.Name = doc1Name Then
Documents(doc2Name).Activate
Else
If ActiveDocument.Name = doc2Name Then
Documents(doc1Name).Activate
Else
MsgBox "Must start in documents 1 or 2" & vbCrLf & vbCrLf & "Document 1: " & doc1Name & vbCrLf & "Document 2: " & doc2Name, , "Find Text or Paragraph"
End
End If
End If
End If

macropod
08-05-2014, 09:14 PM
I suggest you look at this thread: http://www.vbaexpress.com/forum/showthread.php?50335-extract-text-from-a-documents-bookmark-and-insert-it-in-another-document&p=313078&viewfull=1#post313078. That shows the correct way to define & differentiate between a pair of documents for processing. With this approach, it doesn't matter how many extraneous documents might be open. And, just as that macro shows that you can replicate content between documents without selecting anything, activating windows or copying/pasting, so too none of those actions should be required for what you have described.

johndavidson
08-06-2014, 12:33 AM
Thanks. I may be being dim but what I am trying to do is to create a macro that does not require the user to programmatically define the names of the two docs (as happens in the code you are linking to). The macro should work for any pair of docs, whatever their names. It's a one shot macro, operated by a hotkey or toolbar icon etc., that picks up the selected text or short para from a list in doc 1 and locates that text in doc 2, wherever it may be. Manual editing may then ensue on doc. 2 and other docs may be opened, after which the original doc. 1 is again made the focus, and another string/para is picked up and searched for in the original doc. 2.

When only two docs are open at a time, that is no problem, they are numbered docs 1 and 2, and it is easy to flipflop between them. But when a third or subsequent document is open, the two docs originally opened are no longer numbered 1 and 2. Word changes the document numbers. So the question is, how to identify the two docs that were first opened when a third doc has been opened. A userform to select the two docs on each occasion is out of the question, because the idea is to hit a hotkey in doc 1 and immediately find the text in doc2, and making use of a userform each time around would be too time-consuming (there are several thousand strings to be found and their usage checked). I could use document variables to define the two docs, which would be read on subsequent runs of the macro, but I wondered if there was some better way.

Does Word have no way of referring to docs by their number - without Word changing the numbers if another doc is opened? Is there a way of knowing which were the first two docs that were opened in any particular Word session?

Rgds

John Davidson

macropod
08-06-2014, 01:37 AM
So how is your code supposed to know which of, say, a dozen documents you have open is the source and which is the target? Granted, if the macro resides in a document, rather than a template common to both documents, you code can use ThisDocument to be sure of which one that is, but then you can only run the code from that document and it doesn't resolve the issue of how you'd identify any other document.

Yes, Word can refer to the documents by their number but, unless you track the order in which all your documents are opened, you'll never know which number goes with what document - and that number will decrease as previously-opened documents are closed.

johndavidson
08-06-2014, 02:06 AM
Thanks again. My method is for the first two documents opened during a Word session to be understood as the two docs on which the macro will function. When the macro is run, it takes the current active document as the source doc, and finds the string in the other doc. It's up to the user to start in the right document (= find this selected string or whole para in the other doc). I use three monitors so it is easy enough to see what is happening.

But how to keep track of these two docs in subsequent runs of the macro when other docs may have been opened in the meantime? How would one track the order of document opening, and is there an MS page anywhere that describes how Word (2003) reallocates its doc. numbers when a new doc is opened? I suppose also that there is no way of programmatically setting the document numbers (tho' I don't think that would help!)? I think that maybe the only way to do it is to prime the system on the first run of the macro by saving the names of the two docs in document variables, which can then be accessed by name on subsequent runs. Any other ideas would be gratefully accepted!

FYI, I tried cycling through active windows (ActiveWindow.Next.Activate / ActiveWindow.Previous.Activate), which worked better, but suffered from similar ills. Presently, I'm either closing down other docs before re-running the macro or am opening other docs in another instance of WINWORD.exe

Rgds

John Davidson

johndavidson
08-06-2014, 07:38 AM
I was able to to what I wanted by use of a single document variable in the source doc. When there are only two docs., it updates the doc. variable so that whenever there are more than two docs, it still knows where to look. Seems to work OK.

Thanks for the dialogue, which helped me think clearly what I was trying to do.

Rgds

John Davidson