Solved

determine if word document was opened by another document

Posted on 2014-12-16
14
203 Views
Last Modified: 2014-12-16
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?
0
Comment
Question by:thenelson
  • 7
  • 6
14 Comments
 
LVL 14

Expert Comment

by:DrTribos
ID: 40502560
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
 
LVL 39

Author Comment

by:thenelson
ID: 40502663
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
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 40502701
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
 
LVL 14

Expert Comment

by:DrTribos
ID: 40502723
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
 
LVL 14

Expert Comment

by:DrTribos
ID: 40502737
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
 
LVL 39

Author Comment

by:thenelson
ID: 40502871
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
 
LVL 39

Author Comment

by:thenelson
ID: 40502874
Thanks!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Expert Comment

by:DrTribos
ID: 40502890
thanks but i think some of those points ought to have gone to Mr Scan, no?
0
 
LVL 39

Author Comment

by:thenelson
ID: 40502900
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
 
LVL 39

Author Comment

by:thenelson
ID: 40502906
"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
 
LVL 14

Expert Comment

by:DrTribos
ID: 40502919
sure... but why dont you just assign the new doc to a variable...

set docAuto = .....
doc open / new runs
docAuto.variables.add / check...
0
 
LVL 14

Expert Comment

by:DrTribos
ID: 40502924
kewl...
0
 
LVL 39

Author Comment

by:thenelson
ID: 40502954
"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
 
LVL 14

Expert Comment

by:DrTribos
ID: 40502969
Hmm.... nearly 4 am here. Will sleep on it. Open a new question since this one is closed. ..
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now