Go Back   PCMech Forums > Help & Discussion > Software Discussion & Support

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 1.00 average. Display Modes
Old 06-03-2003, 08:42 AM   #1
Member (5 bit)
 
marycp's Avatar
 
Join Date: Jan 2002
Location: Monroe, CT
Posts: 30
Pissed Word Macro Programming

I have a word template and want to make it automatically set a field called articleID to the next number that it gets out of a SQL database. I have the code to automatically populate the field with a number but when I try to setup the object connection I get an error at this line:

Set objConn = Server.CreateObject("ADODB.Connection")

The error I get is "Run-time error '424': Object Required"

Can Macros access SQL dbs? I can't store the article ID in the registry because it would only be unqiue to one person instead of a group of people.

Any thoughts?
TIA
Mary
marycp is offline   Reply With Quote
Old 06-03-2003, 09:25 AM   #2
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
Try adding this line to the top of the procedure, if you don't have it already:

Dim objConn as New ADODB.Connection

The problem might be with the Server object. Are you initializing it correctly? It would probably be easier to create the connection on the client side instead of the server side.
doctorgonzo is offline   Reply With Quote
Old 06-03-2003, 09:45 AM   #3
Member (5 bit)
 
marycp's Avatar
 
Join Date: Jan 2002
Location: Monroe, CT
Posts: 30
Quote:
Originally posted by doctorgonzo
Try adding this line to the top of the procedure, if you don't have it already:

Dim objConn as New ADODB.Connection

The problem might be with the Server object. Are you initializing it correctly? It would probably be easier to create the connection on the client side instead of the server side.
That didn't work. I am not at all familiar with VB and got this code from an ASP page. Maybe I should create the connection by the client since the client is running it. How do I create a client object?
marycp is offline   Reply With Quote
Old 06-03-2003, 09:54 AM   #4
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
If you got the code from an ASP page, it probably won't work.

The Connection object needs a ConnectionString to be able to connect to the SQL Server. For example, the ConnectionString for the database I use is this:

"Provider=SQLOLEDB.1;Data Source=SQLSERV1;Initial Catalog=MyDatabase;User ID=MyUser;Password=MyPassword"

You can easily modify this string for your implementaion by changing the name of the server (Data Source), database (Initial Catalog), username (User ID), and password. Then, open a connection by setting the ConnectionString property and using the Open method, like this:

Set objConn = New ADODB.Connection
objConn.ConnectionString = "Provider=SQLOLEDB.1;Data Source=SQLSERV1;Initial Catalog=MyDatabase;User ID=MyUser;Password=MyPassword"
objConn.Open

You don't have to create a connection to work with a recordset. You can pass the ConnectionString when using a Recordset's Open command instead of explicitly creating a connection.
doctorgonzo is offline   Reply With Quote
Old 06-03-2003, 10:02 AM   #5
Member (5 bit)
 
marycp's Avatar
 
Join Date: Jan 2002
Location: Monroe, CT
Posts: 30
Ok this is all helping. But when I put this in my code it said "Compile Error: User defined type not defined"

Do I need to include any libraries?

Sorry for all the questions. It's not my first language.

Thx
marycp is offline   Reply With Quote
Old 06-03-2003, 10:10 AM   #6
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
Yes, make sure you include the Microsoft ActiveX Data Objects library. Version shouldn't really matter.
doctorgonzo is offline   Reply With Quote
Old 06-03-2003, 10:16 AM   #7
Member (5 bit)
 
marycp's Avatar
 
Join Date: Jan 2002
Location: Monroe, CT
Posts: 30
How do you do that? I couldn't find it in the object browser of the microsoft visual basic editor that comes with M$ Word.
marycp is offline   Reply With Quote
Old 06-03-2003, 10:26 AM   #8
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
In the VBA editor, go to Tools and then References. There should be at least one version of the ActiveX Data Objects library available in the list. If not, do a search for files on your PC that start with "msado"
doctorgonzo is offline   Reply With Quote
Old 06-03-2003, 11:51 AM   #9
Member (5 bit)
 
marycp's Avatar
 
Join Date: Jan 2002
Location: Monroe, CT
Posts: 30
Thanks so much. This works beautiful
marycp is offline   Reply With Quote
Old 06-03-2003, 11:54 AM   #10
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
No problem. If you have any other questions, just post back.
doctorgonzo is offline   Reply With Quote
Old 06-04-2003, 09:43 AM   #11
Member (5 bit)
 
marycp's Avatar
 
Join Date: Jan 2002
Location: Monroe, CT
Posts: 30
Quote:
Originally posted by doctorgonzo
No problem. If you have any other questions, just post back.
OK, this is really confusing. I wrote the Macro and it works for me on my PC and on my laptop. I log in as the same user on both machines. However when anyone else tries to run it they get these two errors one right after the other:

1. Could not open macro storage

2. Compile error: Can't find project or library

I did some research and #1 is usually a sign of a corruption but other people have it and I find that weird. She removed normal.dot but it still doesn't work. I even had her set macro security to low and it doesn't work.

Any ideas?
marycp is offline   Reply With Quote
Old 06-04-2003, 09:50 AM   #12
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
The "Can't find project or library" may mean that type libraries are missing and can't be found. Are the other users trying to run this from their own PCs, or are they simply logging into your PC as different users?
doctorgonzo is offline   Reply With Quote
Old 06-04-2003, 10:24 AM   #13
Member (5 bit)
 
marycp's Avatar
 
Join Date: Jan 2002
Location: Monroe, CT
Posts: 30
They are running them from their own PCs with the same version of word.
marycp is offline   Reply With Quote
Old 06-04-2003, 11:33 AM   #14
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
If possible, go to their PC and go to the VBA macro editor. Go to Tools...References to make sure that all the references are present. Type libraries that are on your PC may not be on theirs; this is especially common with computers that have different versions of Windows. I am guessing that the ADO (ActiveX Data Objects) library is the one that is missing.
doctorgonzo is offline   Reply With Quote
Old 06-04-2003, 11:42 AM   #15
Member (5 bit)
 
marycp's Avatar
 
Join Date: Jan 2002
Location: Monroe, CT
Posts: 30
I'll try that.. She is bringing her laptop in tomorrow for me to look at. Thanks again.
marycp is offline   Reply With Quote
Old 06-05-2003, 07:27 AM   #16
Member (5 bit)
 
marycp's Avatar
 
Join Date: Jan 2002
Location: Monroe, CT
Posts: 30
Ok, the compile error seems to be located on the following line:

Set fField = ActiveDocument.FormFields("ArticleID")

Now fField is not declared above and if I declare it as type "Field" it gives a compile error at this line:

fField.Result = CStr(iDefault)

Is fField part of a default library that most users don't have?

I got the script from M$ web site but of course they don't explain it for the novice vb programmer.

Do you have any idea?

Thanks
marycp is offline   Reply With Quote
Old 06-05-2003, 08:14 AM   #17
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
Try declaring fField as type FormField, not a plain Field.
doctorgonzo is offline   Reply With Quote
Old 06-05-2003, 08:35 AM   #18
Member (5 bit)
 
marycp's Avatar
 
Join Date: Jan 2002
Location: Monroe, CT
Posts: 30
Thanks. I got rid of it by not using fField but simply using ActiveDocument.FormFields("ArticleID") its a little longer to type, but worked.

Now I just have to figure out why everyone but me gets a "Runtime error 5981 Could not open macro storage"....
marycp is offline   Reply With Quote
Old 06-05-2003, 09:10 AM   #19
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
What version of Office are you using?
doctorgonzo is offline   Reply With Quote
Old 06-05-2003, 09:54 AM   #20
Member (5 bit)
 
marycp's Avatar
 
Join Date: Jan 2002
Location: Monroe, CT
Posts: 30
You won't believe this, but I re-created from scratch the entire template and now it works. The template must have been corrupted somehow.

Thanks for all your help!
marycp is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 02:14 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2