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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW TO AUTO LAUNCH IN WORD

Status
Not open for further replies.

UKboy

Programmer
Jun 27, 2002
30
GB
Hello

I have written an wee VB application but cannot figure out how to get it to auto open and run when the word file is open. any suggestions?

the version of word is word 2002

[rockband]

 
Hi,

Run you procedure in the Document_Open event. Open the VB Project explorer and view code for the ThisDocument Object.

:)

Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Open you Document

alt+F11 to view VB Editor

Ctrl+R to view the Project Explorer

Right click ThisDocument Object in Project Explorer and select View Code

In code window select Object DropDown and select Document

In code window select Procudure DropDown and select Open

ergo...
Code:
Private Sub Document_Open()

End Sub


Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Have added in the text to the project explorer part.

If I right click on this document in Microsoft word objects I see

Private Sub Document_Open()

End Sub

Saved the doc and have enabled macros I still have to ALT +F11 to get it to into VB to select the run tab???



[rockband]

 
Well, have you called your procedure from the Document_Open event?
Code:
Private Sub Document_Open()
YourProcedureNameShouldBeInsertedRightInHereInOrderForTheOpenEventToExecuteYourCode
End Sub


Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
You started by stating...
I have written an wee VB application
This is YourProcedureName



Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Ok let me start again.

the doc is called test1

here is the below text that forms the script.

Ive tried putting I have written an wee VB application in the open script which when runs opens debugger with yellow highlights over the words 'Private Sub Document_Open()'

sorry about this


Private Sub CommandButton1_Click()
form1.Caption = TextBox1.Text



End Sub

Private Sub Calculate_Click()
output1.Text = ""
output2.Text = ""
output3.Text = ""
output1 = input2.Text
limoutput = input1.Text

output2.Text = Left(input3.Text, 1) * 14
If Right(input3, 1) = 1 Then output3.Text = Val(output3.Text) + 8
If Right(input3, 1) = 3 Then
output3.Text = Val(output3.Text) + 88
output2.Text = Val(output2.Text) + 1

End If
If Right(input3, 1) = 2 Then output2.Text = Val(output2.Text) + 1

output3.Text = Val(output3.Text) + Val(input4.Text)

output3.Text = Hex(output3.Text)
output2.Text = Hex(output2.Text)
End Sub

Private Sub inout3_Change()

End Sub

Private Sub EQU_Click()

End Sub

Private Sub Frame1_Click()

End Sub

Private Sub input1_Change()

End Sub

Private Sub input3_Change()

End Sub

Private Sub Label1_Click()

End Sub

Private Sub Label2_Click()

End Sub

Private Sub Label6_Click()

End Sub

Private Sub TextBox1_Change()

End Sub

Private Sub TextBox2_Change()

End Sub

Private Sub Label3_Click()

End Sub

Private Sub Label4_Click()

End Sub

Private Sub Limandbyte_Click()

End Sub

Private Sub limoutput_Change()

End Sub

Private Sub output3_Change()

End Sub

Private Sub UserForm_Click()

End Sub

[rockband]

 
In your "wee VB application" what procedure starts it off? That's what has to be called from the open event.

Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
UKBoy,

1. Skip is correct - you have to have something calling your procedure.

2. From the number of subs you put in your post it appears rather bloated. Are you actually using all of them? I rather doubt it. Why do you have things like Label1_Click() - do you really have some logic that requires a procedure if a user clicks a label? Are you really using a procedure if the user clicks the frame?? If not, get rid of them.

3. Having a CommandButton1_Click() procedure implies this is a VBA UserForm. Is this correct? If so, best practices usually (uh...always) has objects like a commandbutton named for what it does. Like "OK", "InputData" - this is the name of button, not the displayed text on the button. I notice that the procedure on the commandbutton click event takes the text of a textbox and makes it the caption of the form. Surely this is not all it is supposed to do - is it?

4. If it is a UserForm, you must call it with the .Show method, like:

Code:
Private Sub Document_Open()
Form1.Show
End Sub

This will load the UserForm on the document being opened. Note you can also use the AutoOpen sub in a module, other than ThisDocument. Again though, name your form something useful.

I do not want to be rude, or step on your toes, but try using Help.

Gerry
 
Oh, and just one other point. I have noticed that Skip seems to usually put these things in the ThisDocument module. I must point out that Document_Open in ThisDocument works on a file. However, it there is a Document_Open event in a template and you invoke the template to make a new file, that does NOT fire the Document_Open event. This is because, technically, the document has not been opened, it has been created. Once you save the file, and reopen it, then yes it fires.

The AutoOpen Sub however does fire from a template call. Not only that, but it fires BEFORE the Document_Open event. Thus, if you want something to fire even when you are making a new file using a template, you need to use AutoOpen rather than Document_Open.

Remember though that this can be a double edged sword. You can have essentially two (2) opening events and if they conflict....

I always use AutoOpen because it is possible to pick up information from the document (before it "opens") and pass that as arguments into the Document_Open procedure. For example comparing current user (the one opening the file) to the author and logging it if they are different.


Gerry
 
When you invoke the template to make a new file, then the Document_New event is trigged.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
True, but the original suggestion was to use the Document_Open event...not Document_New; plus the requirement was for a procedure to run when the document opens. So if you put it in Document_New of the template, yes it will fire when the template makes the new file - but it won't fire when the new document is opened. You could put it a template's Document_New, and Document_Open. That would cover it, but this seems silly to me. Why put it in twice?

My point being was that it you really want to have a procedure to run when a document is opened, regardless of whether it is an existing document, or a newly created one from a template, AutoOpen fits the bill. And that there may be some use for using it as the Word automacros fire before any procedures in ThisDocument.

Gerry
 
What about just naming the macro
AutoOpen
It will run anytime the document is opened.
 
Which is why I rarely use either Document_Open or Document_New. AutoOpen fires first, and therefore can be used for instructions prior to the actual doc_open event. I find it (generally, but not always) the better place to put code.

In particular, if the requirements call for any new instance of Word - and especially if that instance is going to run something in the background - it is far better to do it as early binding. For example, something that calls for a new blank document, and operations within that document. It is better to use AutoOpen and do that upfront before the calling document is even "fully" open.

Blah blah blah..sorry, too much coffee.


Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top