Solved

determine if word document was opened by another document

Posted on 2014-12-16
14
198 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks!
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 14

Expert Comment

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

Author Comment

by:thenelson
Comment Utility
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
Comment Utility
"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
Comment Utility
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
Comment Utility
kewl...
0
 
LVL 39

Author Comment

by:thenelson
Comment Utility
"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
Comment Utility
Hmm.... nearly 4 am here. Will sleep on it. Open a new question since this one is closed. ..
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

744 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

17 Experts available now in Live!

Get 1:1 Help Now