Solved

changing word code to a VB object

Posted on 1998-09-14
9
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Technology Partners: 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!

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

632 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