Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

changing word code to a VB object

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
Bangerter
Asked:
Bangerter
  • 3
  • 3
  • 3
1 Solution
 
NateTCommented:
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
 
LlandrCommented:
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
 
BangerterAuthor Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
NateTCommented:
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
 
LlandrCommented:
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
 
BangerterAuthor Commented:
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
 
BangerterAuthor Commented:
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
 
NateTCommented:
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
 
LlandrCommented:
Fair enough.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now