Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execute different code if manually opened or automated 2

Status
Not open for further replies.

MajP

Technical User
Aug 27, 2005
9,382
US
I am automating Word from access and then run a procedure in the word template
I would like to do different things if the template is opened manually or automated from Access.

The code works but not both ways. What I would like to do is if the user manually opens the template it will run the code in the autonew. However, if the template is automated from Access I do not want it to run the code in the Autonew. When opening from access the Word application is not visible.

According to MS
MSDN said:
Application.usercontrol:
True if the application is visible or if it was created or started by the user.
False if you created or started the application programmatically by using the CreateObject or GetObject functions, and the application is hidden. Read/write Boolean.

The problem is when I automate from Access the word template still assumes it was opened by the user (usercontrol = true).


Here is the code in Access calling the Word app
Code:
 Set wdApp = CreateObject(Class:="Word.Application")
        wdApp.WindowState = wdWindowStateMaximize
        
        'Open the Document
        wdApp.Documents.Add strPath
        'wdApp.Documents.Open FileName:=strPath
        wdApp.Run "CalledFromAccess", GBL_Start_Date, GBL_End_Date, CurrentProject.Path & "\TaskTrackerShadow.accdb"
        wdApp.Visible = True

Here is the code that runs if called from access. Unfortunately this does not run until after the AutoNew
Code:
Public Sub CalledFromAccess(startDate As Date, endDate As Date, path As String)
  glblStartDate = startDate
  glblEndDate = endDate
  strDBpath = path
  ControllingRoutine
End Sub


In the word template I have an autonew macro that runs when the template opens. The if check always returns true opened by the user or opened from ACCESS.

Code:
Public Sub AutoNew()
  '[b] User control does not work. It is always true [/b]
   If Application.UserControl Then
      frmSelectRange.Show
      MsgBox "Pick your database from the file browser."
      strDBpath = fGetFileName()
      ControllingRoutine
  End If
End Sub

Any thoughts? Is there another way to bypass the autonew if automated? Other workarounds?
 
Any thoughts?
As clearly stated in the VBA help, in word VBA UserControl is always True.

another way to bypass the autonew
Use the Open method instead of Add and then the SaveAs

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
word vba help said:
If Word is visible to the user, or if you call the UserControl property of a Word Application or Document object from within a Word code module, this property will always return True
You call the code from within word, in this case you could test [tt]Application.Visible[/tt] tnstead.

combo
 
Thanks. I swear I read it about 5 times and just skipped just over the note looking for other things.
 
Combo,
I basically used your idea, but used a different property of the application object. I hid the document onlybecause I thought that was needed for the usercontrol, but I prefer to leave it visible. So Instead I set the "caption" property (the thing that shows next to the name of the document 'Document 1 - Microsoft Word'). Then I check the caption in the autonew. After the code is run in Access I set the caption back to default. This property does not persist so if the code fails in Access it reverts back to the default when you open Word.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top