Solved

changing word code to a VB object

Posted on 1998-09-14
9
176 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
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!

 

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
 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

679 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