: [b][red]This message was edited by hurby at 2006-5-18 2:17:25[/red][/b][hr] : Hello, : : can anyone tell me how i can add a button to a menubar like "Mail Merge". But i would not use an AddIn. : : PS: excuse my english, i`m german : : Thanks Hurby : : :
Hi, do you need it once, in your MS Word or you want to do this in your code? Try this code [code]Public Sub AddNewCommandBar() Dim cb As CommandBar, cbb As CommandBarButton Set cb = Application.CommandBars.Add("MyCommandBar", msoBarTop) Set cbb = cb.Controls.Add(msoControlButton) cbb.OnAction = "DoSomething" [green]'cbb.Picture = Use LoadPicture to load some![/green] cb.Visible = True cbb.Visible = True End Sub
Public Sub DoSomething() MsgBox "Hello" End Sub[/code]Don't forget, that if you call AddNewCommandBar, it adds new.. (and do not remove older, you've may created few minutes ago..
your code works but the function "DoSomething()" is for MS-Word a Makro and so it can`t get found. I think the Objects need "WithEvents" but i dont know how without the Office-OLB. Have you an idea?
: Hello, : : your code works but the function "DoSomething()" is for MS-Word a Makro and so it can`t get found. I think the Objects need "WithEvents" but i dont know how without the Office-OLB. Have you an idea? : : Hurby :
You want the menubar button to raise some function in your external application? This would be more complicated.. Much more complicated, if I want to be exact :-/ VB (VBA) has very limited capabilities of real programming. My first idea was to create some event in your xls (you can create/modify vb source codes of xls you are controlling) and raise this event from DoSomething() [code] Public Event MyOwnEvent() Public Sub DoSomething() RaiseEvent MyOwnEvent End Sub[/code].. but you can't handle this event in your app.. dead end. When you use WithEvents, you can handle only native object events, like NewWorkbook, Open, Close etc... (It's because VB compiler does not know signature of your specific class) My second idea was to use AddressOf operator to send address of your handlig function to your xls, but... you can fix recieving this value as long variable, but you can not call this pointer to function Not in VB. So, possible solutions may be: 1) Use Excel addin (but you said you can not use it, right?) 2) Implant the body of your function into XLS file via Workbook.VBProject property - but I don't think this would be realizable.. This would work only if your function is short and does not use anything outside it (I guess it do) and so on.. 3) You can raise some native excel event (for example by adding new worksheet with specific name and removing it) and in WorkbookNewSheet test the name and if it's correct, DoSomethingElse. But this would be slow because of adding/removing sheet, you can check some other events you could use.. 4) (Few second old idea) You can create handler for system messages, send to your xls hWnd of your application and in xls, inside DoSomething use SendMessage API function to notify your main application.
1) and 2) were just unfinished ideas 3) can work, but it is not very "clear" method. And it may be slow also 4) more difficult (calling in xls is easy but catching your message is little compicated). This is clear and fast method.
Comments
: Hello,
:
: can anyone tell me how i can add a button to a menubar like "Mail Merge". But i would not use an AddIn.
:
: PS: excuse my english, i`m german
:
: Thanks Hurby
:
:
:
Hi, do you need it once, in your MS Word or you want to do this in your code?
Try this code
[code]Public Sub AddNewCommandBar()
Dim cb As CommandBar, cbb As CommandBarButton
Set cb = Application.CommandBars.Add("MyCommandBar", msoBarTop)
Set cbb = cb.Controls.Add(msoControlButton)
cbb.OnAction = "DoSomething"
[green]'cbb.Picture = Use LoadPicture to load some![/green]
cb.Visible = True
cbb.Visible = True
End Sub
Public Sub DoSomething()
MsgBox "Hello"
End Sub[/code]Don't forget, that if you call AddNewCommandBar, it adds new.. (and do not remove older, you've may created few minutes ago..
Hope this helps
[blue][b][italic][size=4]P[/size]avlin [size=4]II[/italic][/size][/b][/blue]
[purple]Don't take life too seriously anyway you won't escape alive from it![/purple]
i hope it works, can test it first on monday. Am i correct when i belive that i must create a reference to the Office - ObjectLibary?
Hurby
your code works but the function "DoSomething()" is for MS-Word a Makro and so it can`t get found. I think the Objects need "WithEvents" but i dont know how without the Office-OLB. Have you an idea?
Hurby
:
: your code works but the function "DoSomething()" is for MS-Word a Makro and so it can`t get found. I think the Objects need "WithEvents" but i dont know how without the Office-OLB. Have you an idea?
:
: Hurby
:
You want the menubar button to raise some function in your external application? This would be more complicated..
Much more complicated, if I want to be exact :-/
VB (VBA) has very limited capabilities of real programming.
My first idea was to create some event in your xls (you can create/modify vb source codes of xls you are controlling) and raise this event from DoSomething()
[code]
Public Event MyOwnEvent()
Public Sub DoSomething()
RaiseEvent MyOwnEvent
End Sub[/code].. but you can't handle this event in your app.. dead end.
When you use WithEvents, you can handle only native object events, like NewWorkbook, Open, Close etc... (It's because VB compiler does not know signature of your specific class)
My second idea was to use AddressOf operator to send address of your handlig function to your xls, but... you can fix recieving this value as long variable, but you can not call this pointer to function
So, possible solutions may be:
1) Use Excel addin (but you said you can not use it, right?)
2) Implant the body of your function into XLS file via Workbook.VBProject property - but I don't think this would be realizable.. This would work only if your function is short and does not use anything outside it (I guess it do) and so on..
3) You can raise some native excel event (for example by adding new worksheet with specific name and removing it) and in WorkbookNewSheet test the name and if it's correct, DoSomethingElse. But this would be slow because of adding/removing sheet, you can check some other events you could use..
4) (Few second old idea) You can create handler for system messages, send to your xls hWnd of your application and in xls, inside DoSomething use SendMessage API function to notify your main application.
1) and 2) were just unfinished ideas
3) can work, but it is not very "clear" method. And it may be slow also
4) more difficult (calling in xls is easy but catching your message is little compicated). This is clear and fast method.
Hope this helps
[blue][b][italic][size=4]P[/size]avlin [size=4]II[/italic][/size][/b][/blue]
[purple]Don't take life too seriously anyway you won't escape alive from it![/purple]