Question Add a Button and associated Macro in MS Word

kimkamp

Member
Joined
Oct 27, 2011
Messages
5
Programming Experience
10+
Hi - new to this forum...

I'd like to be able to add a Button to an existing Word document, and add code associated with that Button - all with vb.NET coding.

It it possible to do this?

Thanks!
 
Code in vb.NET to do this?

Thanks for the response.

It looks like this is to add a Button and Macro INSIDE of Word.

What I'd like to do is write code in vb.NET OUTSIDE of Word that accomplishes the same thing - from vb.NET, programatically add a Button inside a new/existing Word document and add the code that fires when the Button in the new Word document is clicked.

Is this possible? If so, any code samples?

Thanks!
 
Add reference to Word Object Library and write your automation code. The article should give you a rather clear indication about what automation code you have to write to add a button and its associated macro to run when clicked.

Since you're new to Office automation from VB.Net check out this article and other resources: How to automate Word from Visual Basic .NET to create a new document
As you can see it is pretty much the same as the code in the other article I linked to.
 
Thanks, John, for the reply.

Ok, I've figured out how to place a CommandButton in a Word document (pretty cool if you ask me). Now, I'm trying to get the Code into a Module in VBA inside the Word doc - haven't figured that out yet:
doc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString(sCode)

I'm getting an error message that the "...VBComponents cannot be indexed because it has no default property"

Any ideas?

Also, is it possible to move the CommandButton to the right side and top of the document from within vb.NET?

Thank you so much for the code so far - any recommendations for these two questions?

Kim
 
Press the . after VBComponents and see what members you have available, usually collection(indexer) is a short form for collection.Item(indexer) when that property is the default property.
 
Ok, I got it!

With the following code, a CommandButton is placed on the Word Document. The Caption includes the name of the Printer that will be used to print the document. Using this code, I can get the CommandButton object down to the size of a period on a page when the page prints.

Dim sPrinter As String = "My Printer Name"
sCode = "Public Sub " & shp.OLEFormat.Object.Name & "_Click()" & vbCrLf & _
vbTab & "Dim cHeight, cWidth As Integer" & vbCrLf & _
vbTab & "Dim cPrinter As String" & vbCrLf & _
vbTab & "cHeight = Me.CommandButton1.Height" & vbCrLf & _
vbTab & "cWidth = Me.CommandButton1.Width" & vbCrLf & _
vbTab & "Me.CommandButton1.Height = 0.75" & vbCrLf & _
vbTab & "Me.CommandButton1.Width = 0.75" & vbCrLf & _
vbTab & "Options.PrintDrawingObjects = False" & vbCrLf & _
vbTab & "cPrinter = Me.Application.ActivePrinter" & vbCrLf & _
vbTab & "Me.Application.ActivePrinter = " & Chr(34) & sPrinter & Chr(34) & vbCrLf & _
vbTab & "Me.PrintOut" & vbCrLf & _
vbTab & "Me.Application.ActivePrinter = cPrinter" & vbCrLf & _
vbTab & "Me.CommandButton1.Height = cHeight" & vbCrLf & _
vbTab & "Me.CommandButton1.Width = cWidth" & vbCrLf & _
"End Sub"
doc.VBProject.VBComponents.Item("ThisDocument").CodeModule.AddFromString(sCode)

Let me know if you ever figure out a way to: 1) Hide the CommandButton from printing, and 2) Move/set the CommandButton on the RIGHT side of the document.

Kim
 
To build strings like that have a look at StringBuilder class.

1 & 2) You could try a regular shape instead of inline shape. Regular shapes floats and can be places anywhere and has Visible property. You could also instead use a toobar (commandbar).
 
Word.Document has a Shapes collection, and a CommandBars collection if you want to try that as well.
 
Back
Top