macros using 2 open documents.

M

Miskacee

I have a macro that goes back and forth between two documents. One document
will also be the same name while the other document will always have a
different name. In the past, I was able to create: Windows(2).Activate or
Windows(1).Activate and the macro worked fine flipping back and forth.
However, now active window (1) has become active window (2) and vice versa
for some reason and therefore, the macro won't function properly. I am able
to call the one document that remains static in name but would like to locate
code that would call the other active window, no matter what the name. I
know I use $ in the code but don't remember how to focus on the document that
would have a different name.

The net net, I'm copying information from one document and placing it in
another document which is a 'table' via bookmarks and cross-references.

Thank you for any help you can provide.
 
T

Tony Jollans

You don't normally need or want to be activating windows or working with
windows at all. Set a reference to each document and use those references
....

Dim Doc1 As Document
Dim Doc2 As Document

Set Doc1 = Documents("Same Name")
Set Doc2 = Documents("Different Name")

You don't give enough details for me to be sure how you can definitively set
the references - one of them is probably ActiveDocument to start with - I
don't know about the other.
 
D

Default User

When dealing with opeations between 2 (open(ed)) documents, I never use the
windows or documents collection.
I always use object variables pointing to the correct document, like in
below example:

Dim Doc1 as document
Dim doc2 as document

Set doc1 = Documents.Add("A_Template.dot")
Set doc2 = Documents.Open("SomeFile.doc")

Now I don't have to trace which window is hosting what document.

I can go:
doc1.Tables(1).Cell(1, 1).Range = doc2.Tables(8).Cell(2, 2).Range

instead without having to bother about Windows indexing or documents
indexing.
Another advantage: you won't be bothered screens jumping/focussing from one
doc to the other.

Krgrds,
Perry
 
M

Miskacee

The 2nd document will always be a different name while the first document
will always be the same.
 
T

Tony Jollans

You already said that in your first post but it doesn't provide enough
information for anyone to really help. Is one of your documents the active
document when your macro is invoked? If so, which one? Will you always have
exactly two open documents when your macro is invoked?
 
M

Miskacee

One document is a table layout where the actual information from the employee
word document is stored. The table layout is the 'static' table and the
other document will always be an employee name. The information from the
employee name document is 'loaded' into the variable table - (Word document
with table layout - which will always be used to 'house' the information from
the employee document) via macros/bookmarks and cross-references.
 
T

Tony Jollans

Hi Miskacee,

We seem to be failing to communicate. What your documents are is not
relevant to the technical solution you are asking for.

You previously used Windows(1) and Windows(2) to reference two documents.
That relies on documents (or windows)being opened in a particular order and
is now failing you. You can reference open documents either via the window
or via the document but you must know something about what you're looking
for.

You say one of the documents always has a fixed name so you know what that
is and can hard code a reference to it ..

Set FixedDoc = Documents("FixedDocName")

The question is: how are you going to be able to recognise the other
document and the questions I asked were designed to help me find out how you
might be able to do that.

If you only ever have two documents open you should be able to do something
like this ...

For Each OtherDoc in Documents
If not OtherDoc is FixedDoc Then Exit For
Next

You will then have two document references, FixedDoc and OtherDoc which you
can use.

If your situation is somehow different then a different solution will be
applicable. If so, please come back with details.
 
M

Miskacee

Let me try to explain further.....doc (1) a fixed document that is designed
with a table inside of the document which is used to export information into
an access database. Doc (2) a mary smith.doc which will be Joe blow.doc,
John doe.doc, etc. always a different employee name. In the past I was and
am able to do so now again, reference Windowsactivate(1) or (2) and place
data from document 2, the employee document into document (1), the table. I
was able to repair it by just referring to the active windows. I was trying
to get code to reference the document that will change names. I remember I
used a $ within the line of code but can't find the document with this
information in it. I am calling the 'table' document, the fixed one by just
calling it by name, i.e. import.doc.
 
T

Tony Jollans

If you only have the two documents open, the code I gave you before will get
you a reference to each of the documents - one as FixedDoc, the other as
OtherDoc - change the names to protect the innocent if you like. Is there
some reason you can't use it?

If you must, you can switch between then with ..

FixedDoc.Activate

and

OtherDoc.Activate

but you would be far beter working directly with the document objects.
 
M

Miskacee

Thank you very much Tony, I'll try this way. I'm not that versed on Word and
VB. I believe this will work the same as the other code which I can't find.
 
M

Miskacee

I have the following:
Set FixedDoc = Documents("PMP Import.doc")
Set OtherDoc = Documents("________")

For "Set other doc...." I'm not certain what to put inside the quotes siince
this document's name will change, i.e. employee name.doc. How can I call
this 2nd document since the name of the document will vary?
 
M

Miskacee

by saying 'other doc' how would that point to the employee name.doc document?
In other words, how can I reference the document whose name will change,
i.e. employee name.doc. It will always be another employee name that will be
used to extract this data from the document and put into the 'table' in the
static document, table.doc.

Thanks for any assistance you can provide in order to get this error free.
 
T

Tony Jollans

Using the code I posted earlier ...

Set FixedDoc = Documents("PMP Import.doc")
For Each OtherDoc in Documents
If not OtherDoc is FixedDoc Then Exit For
Next

Does this not work for you?
 
M

Miskacee

Never mind. Thanks. I will try what you suggested and see if this works. I
read further down and realized I didn't print out the section of calling the
documents.

thank you very much for your assistance!
 
M

Miskacee

Whenever I tried your code, I got an error saying object required and then
error on otherdoc.activate.
otherDoc.Activate
Selection.Find.ClearFormatting
With Selection.Find
.Text = "major accountabilities"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
I'm placing the information for opening the PMP Import.doc and ther
OtherDoc.Doc the following way:

'Secondary document (2)
ChangeFileOpenDirectory "C:\Aim compensation\"
Documents.Open FileName:="""PMP Import.doc""",
ConfirmConversions:=False, _
ReadOnly:=True, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto

Set fixeddoc = Documents("PMP Import.doc")
For Each otherDoc In Documents
If Not otherDoc Is fixeddoc Then Exit For
Next


fixeddoc.Activate
Selection.SelectColumn
Selection.Paste

Am I not placing it correctly. It doesn't seem to want to recognize the
other document that is opened. It is asking for the object name I believe.
 
M

Miskacee

This did not work properly. It doesn't like the OtherDoc and wants a
specific object name.
 
T

Tony Jollans

Well, I was going to say ... you must set 'otherDoc' before trying to use
it.
Which is correct ... but it looks like you don't have PMP Import.doc open at
the beginning.

If that is true - and you only have the one document open when you begin -
then this should work ...

'======================
Set otherDoc = ActiveDocument
'======================

Selection.Find.ClearFormatting
With Selection.Find
.Text = "major accountabilities"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
' etc.
' etc.



'Secondary document (2)
ChangeFileOpenDirectory "C:\Aim compensation\"

'========= **
Set fixedDoc = Documents.Open(FileName:="""PMP Import.doc""",
ConfirmConversions:=False, _
ReadOnly:=True, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto)
'
**

Selection.SelectColumn
Selection.Paste

After this you should have fixeddoc and otherdoc properly set.
 
M

Miskacee

whenever the macros gets to: fixedDoc.Activate - I get an error saying
object required. I type in the information you sent me. It is still wanting
an object.
 
M

Miskacee

Here's what I have....:
'Copies database ID to column
Set otherDoc = ActiveDocument

Selection.Find.ClearFormatting
With Selection.Find
.Text = "Database ID:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Copy

'Secondary document (2)
ChangeFileOpenDirectory "C:\Aim compensation\"
Set fixedDoc = Documents.Open(FileName:="""PMP Import.doc""",
ConfirmConversions:=False, _
ReadOnly:=True, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto)

Selection.SelectColumn
Selection.Paste

End Sub
 

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