Solved

determine if word document was opened by another document

Posted on 2014-12-16
14
216 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 15

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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 15

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 15

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
 
LVL 15

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 15

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 15

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 15

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 …
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

807 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