LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.word.vba.general,microsoft.public.excel.programming,microsoft.public.excel,microsoft.public.office.developer.vba
external usenet poster
 
Posts: 11
Default adding code to command button in word from excel VBA

Hi-

I am creating a document in word with excel VBA. I figured out how to
add a command button to word from excel VBA, but I can not figure out
how to add code to that command button for when the command button is
clicked on. I do not want to use a userform. I know how to add code
to the button i create in an excel document, but I can't seem to add
code to a button in word, using excel VBA. Here is the code I have
created so far. I wrote messages in the code to tell you what i was
doing and which part i am having problems with. I have been working on
this for several days and would appreciate any help you can give me.
THank you!


Dim Wdoc As Word.document
Dim Wapp As Word.Application
Dim rng As Word.Range
Dim shp As Word.InlineShape

'this created a word document
Set Wapp = CreateObject("Word.Application")
Wapp.Visible = True
Wapp.Documents.Add
Set Wdoc = Wapp.ActiveDocument

Set rng = Wdoc.Paragraphs.Last.Range

'this created my command button and named it
Set shp =
rng.document.InlineShapes.AddOLEControl(ClassType: ="Forms.Commandbutton.1",
_
Range:=Wdoc.Paragraphs.Last.Range)

shp.OLEFormat.Object.Caption = "Add Orbit"
shp.OLEFormat.Object.Name = "OrbitButton"

'this is the part that doesn't work. I am trying to add code for when
it is clicked on. This is one of
'the methods i tried.
With
rng.document.VBProject.VBComponents(ActiveDocument .CodeName).CodeModule
.InsertLines .CreateEventProc("Click",
shp.OLEFormat.Object.Name) + 1, _
"Msgbox ""You Clicked The Button"" "
End With


'i also tried changing the previous code to this and it still did not
work.

With
thisWorkbook.VBProject.VBComponents(ActiveSheet.Co deName).CodeModule
.InsertLines .CreateEventProc("Click",
shp.OLEFormat.Object.Name) + 1, _
"Msgbox ""You Clicked The Button"" "
End With

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
command button a word document in excel Krishnakanth Excel Worksheet Functions 2 April 11th 08 03:31 PM
Adding Code to Command Button Added At Run Time Kevin E. Excel Programming 2 October 18th 06 04:11 PM
Adding Code to Command Button at Run Time Kevin E. Excel Programming 1 September 20th 06 12:25 AM
adding a command button to an excel cell? betelguese Excel Discussion (Misc queries) 1 March 22nd 05 03:09 AM
Adding Code to an Existing Command Button with VBA jasoncw[_2_] Excel Programming 1 February 15th 05 11:15 PM


All times are GMT +1. The time now is 07:47 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"