determine if word document was opened by another document

Word 2003:
Currently I have code in document 1 to open a second document based on a template:
Documents.Add "document2.dot"

document2.dot has a Sub Document_New() routine.

I would like to run one set of code in document2.dot if document2.dot is opened by double clicking on it but run a different set of code if it is opened from document 1.  Document 1 could have any name.

for example:
Sub Document_New()
   If opened from document 1 then
       do this
   else
       do this instead
   end if
End Sub

Any way to do that?
LVL 39
thenelsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DrTribosCommented:
Set wd = New Word.Application
Set wdDoc = _
    wd.Documents.Open("C:\My Documents\doc1.doc")
If wdDoc.UserControl = True Then
    MsgBox "This document was created or opened by the user."
Else
    MsgBox "This document was created programmatically."
End If
0
thenelsonAuthor Commented:
I tried placing the code you suggested in the Sub Document_New() routine and got: "Word cannot save this file because it is already open elsewhere."

I then tried:
if Activedocument.UserControl then
and
if Project.ThisProject.UserControl then

but they both returned the same whether opened by double clicking or from the code of document 1.
0
GrahamSkanRetiredCommented:
I think the UserControl property is more about opening a document from a different application. I would set and test a document variable:

Sub NewDocFromDoc1()
    Dim doc As Document
    
    Set doc = Documents.Add("document2.dot")
    doc.Variables.Add "FromDoc1", True
End Sub

Open in new window


Function FromDoc1(doc As Document) As Boolean
Dim var As Variable
For Each var In doc.Variables
    If var.Name = "FromDoc1" Then
        FromDoc1 = var.Value
        Exit For
    End If
Next var
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

DrTribosCommented:
Graham I think you are correct - it looked promising....

Here is the rest of my test code that suggests this approach will not work
Option Explicit

Private Sub Document_Open()
  Call test
End Sub

Private Sub document_new()
  Call test
End Sub

Sub test()
  Dim wddoc As Document
  Set wddoc = ActiveDocument
  If wddoc.UserControl = True Then
      MsgBox "This document was created or opened by the user."
  Else
      MsgBox "This document was created programmatically."
  End If
End Sub

Sub MacroNewDoc()
  Dim wddoc As Document
  Set wddoc = Documents.Add(ThisDocument.Path & Application.PathSeparator &   ThisDocument.Name)
  wddoc.Activate
End Sub

Open in new window

0
DrTribosCommented:
I agree with Graham, you need to add some code to Doc 1 that sets a flag... now I must ask - is this Doc 1 a document or a template... you should not put code in documents!

That said, if you need to make this determination on documents that were created before now (i.e. yesterday) then I don't think there is a way.  If you need this for documents created tomorrow then set a flag as Graham suggests :-)

Cheers,
0
thenelsonAuthor Commented:
doc 1 is a template.

Using document variables is perfect as I also want to pass some variables to document 2.
Here is what I ended up with:
template 1:
Dim doc As Document
Set doc = Documents.Add(GetMainFolder & "WorkingFolder\Templates\Prescription form.dot")
doc.Variables.Add "PtName", strPtName
doc.Variables.Add "DOB", strDOB

Open in new window

document2.dot:
Dim strName As String, strDOB As String

If ActiveDocument.Variables.Count = 2 Then
    strName = ActiveDocument.Variables("PtName").Value
    strDOB = ActiveDocument.Variables("DOB").Value
    ActiveDocument.Variables("PtName").Delete
    ActiveDocument.Variables("DOB").Delete
Else
    'the other stuff
End if

Open in new window

0
thenelsonAuthor Commented:
Thanks!
0
DrTribosCommented:
thanks but i think some of those points ought to have gone to Mr Scan, no?
0
thenelsonAuthor Commented:
Oops, Using document variables doesn't work.
Sub Document_New()  of document2.dot runs before the
   doc.Variables.Add
lines run so document2.dot still doesn't know it was opened by template 1.
0
thenelsonAuthor Commented:
"thanks but i think some of those points ought to have gone to Mr Scan, no?"
another oops - I meant to give him the points as he was first with an answer.
0
DrTribosCommented:
sure... but why dont you just assign the new doc to a variable...

set docAuto = .....
doc open / new runs
docAuto.variables.add / check...
0
DrTribosCommented:
kewl...
0
thenelsonAuthor Commented:
"sure... but why dont you just assign the new doc to a variable..."
I'm not sure what you mean.

But I have a timing issue:

In template 1:
    ...
    Set doc = Documents.Add("document2.dot")

document2.dot opens and Sub Document_New() in document2.dot runs:

    Sub Document_New()  
        If ActiveDocument.Variables.Count = 2 Then

document2.dot does not yet have variables so the if statement fails

Then in template 1, the following lines run:
    doc.Variables.Add ...
    doc.Variables.Add ...
0
DrTribosCommented:
Hmm.... nearly 4 am here. Will sleep on it. Open a new question since this one is closed. ..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.