Solved

determine if word document was opened by another document

Posted on 2014-12-16
14
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

728 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