Solved

changing word code to a VB object

Posted on 1998-09-14
9
169 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

708 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

17 Experts available now in Live!

Get 1:1 Help Now