Click Button added via VBA

R

RC - S

I am trying to add a CommandButton via VBA to a new document created by the
VBA. I have tried the Microsoft suggested solution to do this add but as
soon as the CommandButton is loaded with the code...Word Crashes, every time.
When it recovers, the button actually works when it is clicked.

as soon as the F8 button (run manually) is clicked and the code moves to the
last (or any other line i put in it) line and dumps Word.

Here is the code:

Private Sub CommandButton1_Click()
'Add a command button to a new document
Dim doc As Word.Document
Dim shp As Word.InlineShape

Set doc = Documents.Add
Set shp =
doc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")
shp.OLEFormat.Object.Caption = "Click Here"

'Add a procedure for the click event of the inlineshape
'**Note: The click event resides in the This Document module

Dim sCode As String

sCode = "Private Sub " & shp.OLEFormat.Object.Name & "_Click()" & vbCrLf & _
" MsgBox ""You Clicked the CommandButton""" & vbCrLf & _
"End Sub"

doc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString sCode

End Sub

You should be able to clip this into your own doc to see it happen.

if someone can help with the coding or some other means to achieve the
presence of a button on a document my template creates, i would be happy to
hear from you.

Best regards
 
C

Cindy M.

Hi =?Utf-8?B?UkMgLSBT?=,

It's strange...

First time I tried it, it crashed. But after I'd tried a couple of things, then
ran it again, it worked. The only differences in the code that works compared to
what you post is that

1. I have a "Public" procedure
2. I assign the button object a Name, rather than relying on the name Word
assigns the object when creating it.

One of the things I tried changing was to put the assignment of the code string
in a separate procedure of its own. That didn't work: the code wasn't written to
the code module, but it didn't crash, either. After I moved the code back into
where you have it, it just worked. I even stopped and restarted Word (but not
Windows) just to make sure.
I am trying to add a CommandButton via VBA to a new document created by the
VBA. I have tried the Microsoft suggested solution to do this add but as
soon as the CommandButton is loaded with the code...Word Crashes, every time.
When it recovers, the button actually works when it is clicked.

as soon as the F8 button (run manually) is clicked and the code moves to the
last (or any other line i put in it) line and dumps Word.

Here is the code:

Private Sub CommandButton1_Click()
'Add a command button to a new document
Dim doc As Word.Document
Dim shp As Word.InlineShape

Set doc = Documents.Add
Set shp =
doc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")
shp.OLEFormat.Object.Caption = "Click Here"

'Add a procedure for the click event of the inlineshape
'**Note: The click event resides in the This Document module

Dim sCode As String

sCode = "Private Sub " & shp.OLEFormat.Object.Name & "_Click()" & vbCrLf & _
" MsgBox ""You Clicked the CommandButton""" & vbCrLf & _
"End Sub"

doc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString sCode

End Sub

You should be able to clip this into your own doc to see it happen.

if someone can help with the coding or some other means to achieve the
presence of a button on a document my template creates, i would be happy to
hear from you.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
R

RC - S

Many thanks!


Cindy M. said:
Hi =?Utf-8?B?UkMgLSBT?=,

It's strange...

First time I tried it, it crashed. But after I'd tried a couple of things, then
ran it again, it worked. The only differences in the code that works compared to
what you post is that

1. I have a "Public" procedure
2. I assign the button object a Name, rather than relying on the name Word
assigns the object when creating it.

One of the things I tried changing was to put the assignment of the code string
in a separate procedure of its own. That didn't work: the code wasn't written to
the code module, but it didn't crash, either. After I moved the code back into
where you have it, it just worked. I even stopped and restarted Word (but not
Windows) just to make sure.


Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)


This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top