AutoOpen() doesn't run!

E

eckert1961

Hello,

I have a Word 2003 document that is opened from an Access 2003 function
using the following command.

Set objWord = GetObject("K:\My Documents\Winword\MailMerge\Email
Receipt.doc", "Word.document")
' Make Word visible
objWord.Application.Visible = True

This document has an AutoOpen() macro but it doesn't run when it opens from
Access. Once the doc opens, I can run the macro without any problem. If I
open the document from Word the AutoOpen macro runs.

Any idea of why this is happening?
 
J

Jay Freedman

I'm not sure exactly where the problem is, but this appears to work
better:

Dim objWordApp As Word.Application
Dim objWord As Word.Document

Set objWordApp = CreateObject("Word.Application")
objWordApp.Visible = True
Set objWord = _
objWordApp.Documents.Open(Filename:="C:\temp\openme.doc")

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
B

bryan

Jay,
Is it possible to do this with a template as well and then auto running a
macro to save it as a doc?

Thanks,
Bryan
 
D

Doug Robbins - Word MVP

Use

Set objWord = _
objWordApp.Documents.Add("TemplateName")

and then either have an AutoNew macro in the template that does the saving
or use

objWord.SaveAs "path\filename"

I would opt for the latter.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
B

bryan

Since this template is locked and has formfields I'm assuming then that I
could tap into other systems to the populate the formfields, Correct?

Also,
would it be auto(new) or document(new) ?

Thanks,
Bryan
 
F

Fumei2 via OfficeKB.com

Yes you can action the formfields.

If you are creating a document from a template then use Document_New in the
template.
Since this template is locked and has formfields I'm assuming then that I
could tap into other systems to the populate the formfields, Correct?

Also,
would it be auto(new) or document(new) ?

Thanks,
Bryan
[quoted text clipped - 51 lines]
 
B

bryan

Another question on this topic:
Since I want to be able to tap into other systems to populate textfields, I
need some info up front.
Is it possible to pass in variable(s) on the auto(run)and have them placed in
formfiled(s)?

Once this info is there then I can sql other tables.

Thanks again,
Bryan

Fumei2 via OfficeKB.com said:
Yes you can action the formfields.

If you are creating a document from a template then use Document_New in the
template.
Since this template is locked and has formfields I'm assuming then that I
could tap into other systems to the populate the formfields, Correct?

Also,
would it be auto(new) or document(new) ?

Thanks,
Bryan
[quoted text clipped - 51 lines]
Any idea of why this is happening?
.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201001/1

.
 
B

bryan

Posting again as I do not see this post.....
In order to get info from other systems, I need some info to start the sql
with.

Is it possible to pass in variable(s) when doing this:
Set objWord = objWordApp.Documents.Add("TemplateName")


Thanks,
Bryan


Fumei2 via OfficeKB.com said:
Yes you can action the formfields.

If you are creating a document from a template then use Document_New in the
template.
Since this template is locked and has formfields I'm assuming then that I
could tap into other systems to the populate the formfields, Correct?

Also,
would it be auto(new) or document(new) ?

Thanks,
Bryan
[quoted text clipped - 51 lines]
Any idea of why this is happening?
.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201001/1

.
 
P

Peter Jamieson

I'm not completely sure what you are driving at, but...
Set objWord = objWordApp.Documents.Add("TemplateName")

....I guess it's obvious that the only "variables" in there - are
- objWord
- the mechanism you are using to create the new document
- the template name, and by extension, anything that you can store in
that template that you can then reference using VBA.

In other words, if you want to access /different data/ depending on
which /template/ you base your document on, then what you probably need
to do is store the relevant connection information in your template. One
way to do that would be create Word Document variables in your template
that stored the information necessary to connect to a particular
database and retrieve information from it.

For example, suppose each template had two document variables as follows:
a. DSConnection, storing an OLE DB connection string that would let
you use ADO to open a particular SQL Server database
b. DSCommand, storing a SQL SELECT statement that would let you open
an ADO Recordset

then you could use

objWord.Variables("DSConnection")

to extract the connection string,

objWord.Variables("DSCommand")

to extract the SQL (or whatever) statement, and use ADO to get the data
you need.

That would mean that each /template/ could be used to open a different
data source. But if you need to do something different every time you
create a new document, it isn't clear where the data is that would make
you do thing "X" one time and thing "Y" the next - maybe you could try
to spell that out.

Peter Jamieson

http://tips.pjmsn.me.uk

Posting again as I do not see this post.....
In order to get info from other systems, I need some info to start the sql
with.

Is it possible to pass in variable(s) when doing this:
Set objWord = objWordApp.Documents.Add("TemplateName")


Thanks,
Bryan


Fumei2 via OfficeKB.com said:
Yes you can action the formfields.

If you are creating a document from a template then use Document_New in the
template.
Since this template is locked and has formfields I'm assuming then that I
could tap into other systems to the populate the formfields, Correct?

Also,
would it be auto(new) or document(new) ?

Thanks,
Bryan

Use

[quoted text clipped - 51 lines]
Any idea of why this is happening?
.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201001/1

.
 
B

bryan

Maybe I should use the term arguments rather than parameters.

Currently I have a working template that when users open it has an OCX
Desktop connection to a database which has a file number. With that number I
can then sql other database files and populate the new document.
What I'd like to do is streamline the process and call the template from a
workflow. Within that workflow task I have a file number which I would use to
sql other databases and populate the doc.

Is this possible?

Thanks,
Bryan



Peter Jamieson said:
I'm not completely sure what you are driving at, but...
Set objWord = objWordApp.Documents.Add("TemplateName")

....I guess it's obvious that the only "variables" in there - are
- objWord
- the mechanism you are using to create the new document
- the template name, and by extension, anything that you can store in
that template that you can then reference using VBA.

In other words, if you want to access /different data/ depending on
which /template/ you base your document on, then what you probably need
to do is store the relevant connection information in your template. One
way to do that would be create Word Document variables in your template
that stored the information necessary to connect to a particular
database and retrieve information from it.

For example, suppose each template had two document variables as follows:
a. DSConnection, storing an OLE DB connection string that would let
you use ADO to open a particular SQL Server database
b. DSCommand, storing a SQL SELECT statement that would let you open
an ADO Recordset

then you could use

objWord.Variables("DSConnection")

to extract the connection string,

objWord.Variables("DSCommand")

to extract the SQL (or whatever) statement, and use ADO to get the data
you need.

That would mean that each /template/ could be used to open a different
data source. But if you need to do something different every time you
create a new document, it isn't clear where the data is that would make
you do thing "X" one time and thing "Y" the next - maybe you could try
to spell that out.

Peter Jamieson

http://tips.pjmsn.me.uk

Posting again as I do not see this post.....
In order to get info from other systems, I need some info to start the sql
with.

Is it possible to pass in variable(s) when doing this:
Set objWord = objWordApp.Documents.Add("TemplateName")


Thanks,
Bryan


Fumei2 via OfficeKB.com said:
Yes you can action the formfields.

If you are creating a document from a template then use Document_New in the
template.

bryan wrote:
Since this template is locked and has formfields I'm assuming then that I
could tap into other systems to the populate the formfields, Correct?

Also,
would it be auto(new) or document(new) ?

Thanks,
Bryan

Use

[quoted text clipped - 51 lines]
Any idea of why this is happening?
.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201001/1

.
.
 
P

Peter Jamieson

Currently I have a working template that when users open it has an OCX
Desktop connection

Can you spell out what this is, exactly? I mean, the user opens the
template (or maybe creates a new document based on it). But what is the
OCX in this case (are we talking an OCX control/ActiveX object, or
something else that you know as an OCX)?

Peter Jamieson

http://tips.pjmsn.me.uk

Maybe I should use the term arguments rather than parameters.

Currently I have a working template that when users open it has an OCX
Desktop connection to a database which has a file number. With that number I
can then sql other database files and populate the new document.
What I'd like to do is streamline the process and call the template from a
workflow. Within that workflow task I have a file number which I would use to
sql other databases and populate the doc.

Is this possible?

Thanks,
Bryan



Peter Jamieson said:
I'm not completely sure what you are driving at, but...
Set objWord = objWordApp.Documents.Add("TemplateName")

....I guess it's obvious that the only "variables" in there - are
- objWord
- the mechanism you are using to create the new document
- the template name, and by extension, anything that you can store in
that template that you can then reference using VBA.

In other words, if you want to access /different data/ depending on
which /template/ you base your document on, then what you probably need
to do is store the relevant connection information in your template. One
way to do that would be create Word Document variables in your template
that stored the information necessary to connect to a particular
database and retrieve information from it.

For example, suppose each template had two document variables as follows:
a. DSConnection, storing an OLE DB connection string that would let
you use ADO to open a particular SQL Server database
b. DSCommand, storing a SQL SELECT statement that would let you open
an ADO Recordset

then you could use

objWord.Variables("DSConnection")

to extract the connection string,

objWord.Variables("DSCommand")

to extract the SQL (or whatever) statement, and use ADO to get the data
you need.

That would mean that each /template/ could be used to open a different
data source. But if you need to do something different every time you
create a new document, it isn't clear where the data is that would make
you do thing "X" one time and thing "Y" the next - maybe you could try
to spell that out.

Peter Jamieson

http://tips.pjmsn.me.uk

Posting again as I do not see this post.....
In order to get info from other systems, I need some info to start the sql
with.

Is it possible to pass in variable(s) when doing this:
Set objWord = objWordApp.Documents.Add("TemplateName")


Thanks,
Bryan


:

Yes you can action the formfields.

If you are creating a document from a template then use Document_New in the
template.

bryan wrote:
Since this template is locked and has formfields I'm assuming then that I
could tap into other systems to the populate the formfields, Correct?

Also,
would it be auto(new) or document(new) ?

Thanks,
Bryan

Use

[quoted text clipped - 51 lines]
Any idea of why this is happening?
.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201001/1

.
.
 
D

Doug Robbins - Word MVP

Use the .Result attribute of the formfield to assign a value to it.

--
Hope this helps,

Doug Robbins - Word MVP

Please reply only to the newsgroups unless you wish to obtain my services on
a paid professional basis.

bryan said:
Another question on this topic:
Since I want to be able to tap into other systems to populate textfields,
I
need some info up front.
Is it possible to pass in variable(s) on the auto(run)and have them placed
in
formfiled(s)?

Once this info is there then I can sql other tables.

Thanks again,
Bryan

Fumei2 via OfficeKB.com said:
Yes you can action the formfields.

If you are creating a document from a template then use Document_New in
the
template.
Since this template is locked and has formfields I'm assuming then that
I
could tap into other systems to the populate the formfields, Correct?

Also,
would it be auto(new) or document(new) ?

Thanks,
Bryan

Use

[quoted text clipped - 51 lines]
Any idea of why this is happening?
.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201001/1

.
 
B

bryan

OCX Desktop Control.

Is it possible to pass in args?
If so, how?

Thanks,
Bryan

Peter Jamieson said:
Currently I have a working template that when users open it has an OCX
Desktop connection

Can you spell out what this is, exactly? I mean, the user opens the
template (or maybe creates a new document based on it). But what is the
OCX in this case (are we talking an OCX control/ActiveX object, or
something else that you know as an OCX)?

Peter Jamieson

http://tips.pjmsn.me.uk

Maybe I should use the term arguments rather than parameters.

Currently I have a working template that when users open it has an OCX
Desktop connection to a database which has a file number. With that number I
can then sql other database files and populate the new document.
What I'd like to do is streamline the process and call the template from a
workflow. Within that workflow task I have a file number which I would use to
sql other databases and populate the doc.

Is this possible?

Thanks,
Bryan



Peter Jamieson said:
I'm not completely sure what you are driving at, but...

Set objWord = objWordApp.Documents.Add("TemplateName")

....I guess it's obvious that the only "variables" in there - are
- objWord
- the mechanism you are using to create the new document
- the template name, and by extension, anything that you can store in
that template that you can then reference using VBA.

In other words, if you want to access /different data/ depending on
which /template/ you base your document on, then what you probably need
to do is store the relevant connection information in your template. One
way to do that would be create Word Document variables in your template
that stored the information necessary to connect to a particular
database and retrieve information from it.

For example, suppose each template had two document variables as follows:
a. DSConnection, storing an OLE DB connection string that would let
you use ADO to open a particular SQL Server database
b. DSCommand, storing a SQL SELECT statement that would let you open
an ADO Recordset

then you could use

objWord.Variables("DSConnection")

to extract the connection string,

objWord.Variables("DSCommand")

to extract the SQL (or whatever) statement, and use ADO to get the data
you need.

That would mean that each /template/ could be used to open a different
data source. But if you need to do something different every time you
create a new document, it isn't clear where the data is that would make
you do thing "X" one time and thing "Y" the next - maybe you could try
to spell that out.

Peter Jamieson

http://tips.pjmsn.me.uk

On 21/01/2010 17:40, bryan wrote:
Posting again as I do not see this post.....
In order to get info from other systems, I need some info to start the sql
with.

Is it possible to pass in variable(s) when doing this:
Set objWord = objWordApp.Documents.Add("TemplateName")


Thanks,
Bryan


:

Yes you can action the formfields.

If you are creating a document from a template then use Document_New in the
template.

bryan wrote:
Since this template is locked and has formfields I'm assuming then that I
could tap into other systems to the populate the formfields, Correct?

Also,
would it be auto(new) or document(new) ?

Thanks,
Bryan

Use

[quoted text clipped - 51 lines]
Any idea of why this is happening?
.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201001/1

.

.
.
 
P

Peter Jamieson

I think the simple answer to your question is "no"

I assume that what you are looking for is a way to do something like

OpenTemplate("the template name",thefilenumber)

and have the template's AutoOpen (or whatever) pick up that parameter
and act on it.

However, that's not what you get in the Word environment, because the
procedures that run when you open a template/document or create a
document from a template are initiated by Word, and don't take arguments
(the full range of "how to initiate stuff in Word" can be found at

http://word.mvps.org/FAQs/MacrosVBA/index.htm

)

However, you can
a. use Automation to modify the template/document before the user gets
to work with it
b. call Subs defined in the template/document and pass arguments in
/after/ the document has been created. If you want an "object-oriented
approach where each template has its own processSQL(filenumber) method,
then you can use code like the following:

Sub sample()
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim strDocPath as String
Dim strFileNumber as String
' somewhere in here you need to set up the
' name of the document you want to open
' in strDocPath and the FileNumber in
' strFileNumber

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Open(strDocPath)
' approach 1
objDoc.processSQL strFileNumber
' approach 2
objWord.Run "processSQL2" strFileNumber
objDoc.Close
Set objDoc = Nothing
objWord.Quit
Set objWord = Nothing
End Sub

For approach 1 to work, your document needs to have a Sub called
processSQL defined in the "ThisDocument" container. In that case you can
name the parameters how you like.

For approach 2 to work, your document needs a Sub called "processSQL2",
which can be in Module (you can pin down the location using e.g.

objWord.Run "mymodule.processSQL2" strFileNumber

However, to get arguments through to that sub you have to call them
varg1, varg2 etc., e.g.

Sub processSQL2(varg1 As Variant, varg2 As Variant, etc.

There may be other approaches of which I am not aware, but I hope that
gets you a bit further.

Peter Jamieson

http://tips.pjmsn.me.uk

OCX Desktop Control.

Is it possible to pass in args?
If so, how?

Thanks,
Bryan

Peter Jamieson said:
Currently I have a working template that when users open it has an OCX
Desktop connection

Can you spell out what this is, exactly? I mean, the user opens the
template (or maybe creates a new document based on it). But what is the
OCX in this case (are we talking an OCX control/ActiveX object, or
something else that you know as an OCX)?

Peter Jamieson

http://tips.pjmsn.me.uk

Maybe I should use the term arguments rather than parameters.

Currently I have a working template that when users open it has an OCX
Desktop connection to a database which has a file number. With that number I
can then sql other database files and populate the new document.
What I'd like to do is streamline the process and call the template from a
workflow. Within that workflow task I have a file number which I would use to
sql other databases and populate the doc.

Is this possible?

Thanks,
Bryan



:

I'm not completely sure what you are driving at, but...

Set objWord = objWordApp.Documents.Add("TemplateName")

....I guess it's obvious that the only "variables" in there - are
- objWord
- the mechanism you are using to create the new document
- the template name, and by extension, anything that you can store in
that template that you can then reference using VBA.

In other words, if you want to access /different data/ depending on
which /template/ you base your document on, then what you probably need
to do is store the relevant connection information in your template. One
way to do that would be create Word Document variables in your template
that stored the information necessary to connect to a particular
database and retrieve information from it.

For example, suppose each template had two document variables as follows:
a. DSConnection, storing an OLE DB connection string that would let
you use ADO to open a particular SQL Server database
b. DSCommand, storing a SQL SELECT statement that would let you open
an ADO Recordset

then you could use

objWord.Variables("DSConnection")

to extract the connection string,

objWord.Variables("DSCommand")

to extract the SQL (or whatever) statement, and use ADO to get the data
you need.

That would mean that each /template/ could be used to open a different
data source. But if you need to do something different every time you
create a new document, it isn't clear where the data is that would make
you do thing "X" one time and thing "Y" the next - maybe you could try
to spell that out.

Peter Jamieson

http://tips.pjmsn.me.uk

On 21/01/2010 17:40, bryan wrote:
Posting again as I do not see this post.....
In order to get info from other systems, I need some info to start the sql
with.

Is it possible to pass in variable(s) when doing this:
Set objWord = objWordApp.Documents.Add("TemplateName")


Thanks,
Bryan


:

Yes you can action the formfields.

If you are creating a document from a template then use Document_New in the
template.

bryan wrote:
Since this template is locked and has formfields I'm assuming then that I
could tap into other systems to the populate the formfields, Correct?

Also,
would it be auto(new) or document(new) ?

Thanks,
Bryan

Use

[quoted text clipped - 51 lines]
Any idea of why this is happening?
.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201001/1

.

.
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top