determine if word document was opened by another document

continuation of this question

Word 2003:
Currently I have code in document 1 to open a second document based on a template:
Documents.Add "" has a Sub Document_New() routine.

I would like to run one set of code in if 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
       do this instead
   end if
End Sub

Any way to do that?
LVL 39
Who is Participating?
GrahamSkanConnect With a Mentor RetiredCommented:
You could try setting a time flag in the registry and testing it to make sure that it isn't an old setting:
Sub NewDocFromDoc2()
    Dim doc As Document
    SaveSetting "MyApp", "Opening", "Time", CStr(Now)
    Set doc = Documents.Add("C:\Users\Graham Skan\Documents\Doc2.dotm")
End Sub

Open in new window

Sub Document_New()
Dim dt As Date
dt = CDate(GetSetting("MyApp", "Opening", "Time", "1/1/1900"))
    If DateDiff("s", dt, Now) < 2 Then
        MsgBox "Opened from Doc1"
    End If
End Sub

Open in new window

thenelsonAuthor Commented:
Neat idea. I guess I could use DeleteSetting after GetSetting in the second document and then I wouldn't need to check the time.
Yes, that would work as well.
thenelsonAuthor Commented:
So this is what I did and it is working fine:

Calling document:
SaveSetting "Script", "Open", "Ptname", ActiveDocument.Bookmarks("PatientsName").Range.Text
SaveSetting "Script", "Open", "DOB", Format(ActiveDocument.Bookmarks("DOB").Range.Text, "mm/dd/yyyy")

Documents.Add ("")

Open in new window
Dim strName As String, strDOB As String

strName = GetSetting("Script", "Open", "Ptname")
If Len(strName & "") Then
    strDOB = GetSetting("Script", "Open", "DOB")
    DeleteSetting "Script", "Open"
    'the other stuff
End If

Open in new window

thenelsonAuthor Commented:
Thanks, works great.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.