Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default How do I create a new commandbutton in a new workbook?

I am using a macro workbook to run commands to create a new workbook and
other stuff. In addition, I want to create VBA codes for a new command button
with codes to "print" all worksheets in this new workbook (not my macro
workbook). My macro workbook will be closed before users can see it. Now they
will have a new workbook and they have the option to print all the worksheets
in there.

Can someone help me with this? Thank you!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default How do I create a new commandbutton in a new workbook?

This example will add a button on the Sheet1 of the new blank workbook it create
But this is a emty workbook so change this to your situation

See also
http://www.cpearson.com/excel/vbe.htm

Sub test()
Dim Wb As Workbook
Dim Ws As Worksheet
Dim Btn As OLEObject
Set Wb = Workbooks.Add
Set Ws = Wb.Worksheets(1)

With Ws
Set Btn = .OLEObjects.Add(ClassType:="Forms.CommandButton.1" , _
Left:=.Range("C3").Left, Top:=.Range("C3").Top, _
Width:=100, Height:=30)
End With
Btn.Object.Caption = "Print workbook"
Btn.Name = "YourPrintButton"
With Wb.VBProject.VBComponents(Ws.CodeName).CodeModule
.InsertLines .CreateEventProc("Click", Btn.Name) + 1, _
"Activeworkbook.printout"
End With
End Sub


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Sally" wrote in message ...
I am using a macro workbook to run commands to create a new workbook and
other stuff. In addition, I want to create VBA codes for a new command button
with codes to "print" all worksheets in this new workbook (not my macro
workbook). My macro workbook will be closed before users can see it. Now they
will have a new workbook and they have the option to print all the worksheets
in there.

Can someone help me with this? Thank you!



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default How do I create a new commandbutton in a new workbook?

Thank you, Ron. That worked like a charm. Just what I needed. Best regards.

"Ron de Bruin" wrote:

This example will add a button on the Sheet1 of the new blank workbook it create
But this is a emty workbook so change this to your situation

See also
http://www.cpearson.com/excel/vbe.htm

Sub test()
Dim Wb As Workbook
Dim Ws As Worksheet
Dim Btn As OLEObject
Set Wb = Workbooks.Add
Set Ws = Wb.Worksheets(1)

With Ws
Set Btn = .OLEObjects.Add(ClassType:="Forms.CommandButton.1" , _
Left:=.Range("C3").Left, Top:=.Range("C3").Top, _
Width:=100, Height:=30)
End With
Btn.Object.Caption = "Print workbook"
Btn.Name = "YourPrintButton"
With Wb.VBProject.VBComponents(Ws.CodeName).CodeModule
.InsertLines .CreateEventProc("Click", Btn.Name) + 1, _
"Activeworkbook.printout"
End With
End Sub


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Sally" wrote in message ...
I am using a macro workbook to run commands to create a new workbook and
other stuff. In addition, I want to create VBA codes for a new command button
with codes to "print" all worksheets in this new workbook (not my macro
workbook). My macro workbook will be closed before users can see it. Now they
will have a new workbook and they have the option to print all the worksheets
in there.

Can someone help me with this? Thank you!




Reply
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
CommandButton to open a Excel workbook aussiegirlone Excel Discussion (Misc queries) 2 March 25th 09 01:34 AM
Commandbutton to Workbook Open Richard Excel Programming 6 February 28th 06 03:43 AM
Delete a Commandbutton from another Workbook jase[_2_] Excel Programming 5 June 3rd 05 03:15 PM
Create simple commandbutton for manual recalculation Bryan Linton Excel Programming 2 August 6th 04 10:30 PM
Close WorkBook with CommandButton Rockee052[_50_] Excel Programming 3 February 23rd 04 04:04 AM


All times are GMT +1. The time now is 02:10 PM.

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

About Us

"It's about Microsoft Excel"