Solved

changing word code to a VB object

Posted on 1998-09-14
9
172 Views
Last Modified: 2010-04-30
i have a bunch of macros in Word 7.0 that i need to convert to a .dll via VB5. as an object i will then call these macros through a Word97 template. if i understand right i can pretty much copy the wordbasic code straight to the vb compiler and compile my code and make it a .dll (someone correct me if im wrong). then inside word i need to do code something like:

dim WordObj as object

set WordObj = createobject("MyProject.MyModule")
call WordObj.MySub

right now in the code of MySub all i have is:

public sub changeColor
      selection.font.colorindex = wdblue
end sub

i compiled it as a .dll, i then registered it using regsvr32 My.dll, i went into the tools pull down menu in word then to references and i found my .dll, i set the reference check box to on.

when i try to run the macro set up to execute my code i get a runtime error 424 oject required.

i really need to solve this problem thenks to anyone that can help.
0
Comment
Question by:Bangerter
  • 3
  • 3
  • 3
9 Comments
 

Expert Comment

by:NateT
ID: 1434879
You need to set a reference to msword in your vb dll.  Without it you will get the object required error.
Just as a side note:
  If you want to step through the vb code you can just run the vb proj and then open up words vb editor and you can still set the reference to the dll.
0
 
LVL 2

Expert Comment

by:Llandr
ID: 1434880
The code in your DLL is wrong (due to referencing). I think you have done it right otherwise.
Try this:

public sub changeColor
  MsgBox "Before"
      selection.font.colorindex = wdblue
  MsgBox "After"
end sub

And check if you get both the before and after message. I think that you will have a problem referencing the Word object Selection without doing what NateT said (so I am rephrasing and posting as an answer).
0
 
LVL 2

Author Comment

by:Bangerter
ID: 1434881
how do i set a reference to word in my .dll?

Llandr the code you gave is identical to mine except for the msgboxes unless there is something i am missing
0
 

Expert Comment

by:NateT
ID: 1434882
In vb go to project..references and then the reference dialog pops up.  You should see a check box for "Microsoft Word 8.0 object library". Check that, hit ok and then run the dll.  Go to word and set the ref to your dll, run your code behind the doc and it will work just fine.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:Llandr
ID: 1434883
Ok. The intetion was to make you see that the error occurs in the DLL, not because something was missing in the function call.
0
 
LVL 2

Author Comment

by:Bangerter
ID: 1434884
how do i set a reference to word in my .dll?

Llandr the code you gave is identical to mine except for the msgboxes unless there is something i am missing
0
 
LVL 2

Author Comment

by:Bangerter
ID: 1434885
i did end up getting it working before i read these 2 most recent comments, i did the referencing in code:

dim wordobj as object
set wordobj = getobject(, "word.application")
wordobj.selection.font.colorindex = 3
    'i had to use a number here instead of the word pre-defined      'color constants
set wordobj = nothing

NateT does the code i have do the same thing as referencing the Word 8.0 object library like you suggested? is there a good reference manual that will tell me the vb equivilant to the wordbasic commands?

Llandr these points should really go to NateT because he is the one who originated the possible solution.
0
 

Accepted Solution

by:
NateT earned 100 total points
ID: 1434886
The code you have apparently does the same thing as setting an explicit reference to words object model (I'm not totally sure cause I haven't done it that way before), but there is a downside...because there is no explicit reference the obj model for word doesn't come up with vb's intellisense.  If you set the reference to word you will get the "vb equivilant" commands to pop up as you are coding.  I can't think of any books, but you should be able to get by using the object model.  If you come across something you cant figure out just email me at NTownsley@Darlingii.com and we can work it out.
0
 
LVL 2

Expert Comment

by:Llandr
ID: 1434887
Fair enough.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

930 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now