Re: Click Button added via VBA
- From: Cindy M. <C.Meister-C@xxxxxxxxxx>
- Date: Wed, 11 Oct 2006 11:35:02 +0200
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 :-)
.
- Follow-Ups:
- Re: Click Button added via VBA
- From: RC - S
- Re: Click Button added via VBA
- Prev by Date: Re: Can I call some 'wait' function in VBA?
- Next by Date: Re: How can I prevent implicit view changes?
- Previous by thread: strange excel crach with macro repetition
- Next by thread: Re: Click Button added via VBA
- Index(es):
Relevant Pages
|