Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I make Word macro code work in VB6??

Status
Not open for further replies.

timnicholls

Technical User
Oct 31, 2003
43
0
0
AU
So...all my macros I used in Office Word 2003 I cannot place on a toolbar in Word 2007...the all powerful ribbonUI exists!

Given that, I've used a good tutorial here:

To make a tab and buttons in the actual ribbon itself using VB6. That all works well. But now I place my macros in subs and they don't work??!!

The subs in my original macros hold really basic stuff like


Selection.TypeText Text:="Name"
Selection.TypeParagraph
Selection.TypeText Text:="Address"
Selection.TypeParagraph
Selection.TypeText Text:="Company"
Selection.TypeParagraph

and

Selection.TypeParagraph
Selection.InlineShapes.AddPicture FileName:= _
"C:\signature.jpg" _
, LinkToFile:=False, SaveWithDocument:=True
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeText Text:="Name"
Selection.TypeParagraph
Selection.TypeText Text:="Address"
Selection.TypeParagraph
Selection.TypeText Text:="Company"
Selection.TypeParagraph

Nothing special there...
Just the normal signature and name used at the bottom of letters.

So do I convert this macro code to VB code??
How do I do that???

Thanks

Tim
 
timnicholls

It is called automation.
To start with
Code:
Dim objWord As Object 'Word.Application
Dim objDoc As Object 'Word.Document

Set objWord = CreateObject("Word.Application")
objWord.Documents.Open FileName:="D:\Job Description.doc"
objWord.Visible = True
Set objDoc = objWord.Documents("Job Description.doc")
With objDoc
   .Selection.TypeText Text:="Name"

.....

   .Save
   .Close
End With
Set objDoc = Nothing
objWord.Quit
Set objWord = Nothing

The way both objects are DIMed you don't need to add a reference to Microsoft Word x.0 Library. But if you

Dim objWord As Word.Application
Dim objDoc As Word.Document

you' ll have to. Code is executing faster.But then arises the version problem.

---
BTW, my word automation knowledge, is limited to this.
 
Thanks Jerry.

To make things more explicit here is really the back bone of the code.

This procedure gives me the tab and buttons in the Word 2007 ribbon.
Note the 'onAction' calls a procedure (shown below)

Code:
Public Function GetRibbonXML() As String
   Dim sRibbonXML As String
    
    sRibbonXML = "<customUI xmlns=""[URL unfurl="true"]http://schemas.microsoft.com/office/2006/01/customui""[/URL] >" & _
                "<ribbon>" & _
                "<tabs>" & _
                "<tab id=""CustomTab"" label=""Tim's Toolbar"">" & _
                "<group id=""SampleGroup"" label=""Tim's Macros"">" & _
                "<button id=""Button"" label="" WorkAddress"" size=""large"" onAction=""WorkAddress"" />" & _
                "<button id=""Button1"" label=""HomeAddress"" size=""large"" onAction=""HomeAddress"" />" & _
                "</group >" & _
                "</tab>" & _
                "</tabs>" & _
                "</ribbon>" & _
                "</customUI>"
  
   GetRibbonXML = sRibbonXML
  
End Function


Called procedure

Code:
Sub WorkAddress(control As IRibbonControl)
'
' Address Macro
' Macro created 7/8/2003 by Tim
'
   Selection.TypeText Text:="Name"
   Selection.TypeParagraph
   Selection.TypeText Text:="Address"
   Selection.TypeParagraph
   Selection.TypeText Text:="Company"
   Selection.TypeParagraph

End Sub


The application Word and the document are already open, so do I need automation?
I'd just like to automatically throw some text in at the cursor!?

I thought it's a matter of substituting say:
Selection.TypeText Text:="Name"
Selection.TypeParagraph

with something that actually works in VB...because at the minute the above is not working.

Tim
 
<do I need automation?

Yes, you do. Suppose you look at it this way. How do you go into Word and run your macro? By opening Word and doing it. But how do you do it in VB? By telling VB to open Word and do it. Well, how do you do that? Automation! You want to do it AUTOMATICALLY using VB, right?

<I'd just like to automatically [sic] throw some text in at the cursor!?
Yes, you're getting the point! [LOL] You do that by using automation.

<The application Word and the document are already open
When that's so, you use GetObject instead of CreateObject, but you still have to use Automation in the way that Jerry has explained. Keep in mind that Word is really an object, of type Word.Application. The way that we access that object from VB is to create another object of the same type, and point it to Word application. VB uses its object as a "proxy" of the Word application; we send commands to the VB object, and it knows how to forward those commands to the actual Word application object.

So, that's how it works. Jerry is creating an object in VB that references the Word application, and then using that object to send commands that the application recognizes. Note that these commands can be calls to procedures that you have written.

I would suggest that you first experiment with using CreateObject to open a new instance of word with your document in it. There are less variables in that process. Once you understand how to run your macros that way, then work out how to use GetObject to grab an existing instance of Word.

To get you started, try this. First, put this macro into Word:
Code:
Sub SayHello()
    Documents.Add Template:="Normal", NewTemplate:=False, DocumentType:=0
    Selection.TypeText Text:="Hello, world!"
End Sub

Then go into VB. Start a new Standard EXE project. Set a reference (Use Project/References) to the Word 11.0 Object Library (you may have a different version, if so, use it). Add a command button to the form. Then add this code:
Code:
Option Explicit

Dim WordApp As Word.Application

Private Sub Command1_Click()
Set WordApp = New Word.Application
WordApp.Visible = True
WordApp.Run "SayHello"
End Sub
Run that, and see what you get. The rest is just detail...

HTH

Bob
 
<The rest is just detail...

An important detail, by the way, is Jerry's use of CreateObject and late binding to work around the fact that you're working with different versions of Word.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top