PC Review


Reply
Thread Tools Rate Thread

Calling a procedure from another workbook

 
 
mccar75287
Guest
Posts: n/a
 
      23rd Nov 2007
Here's my problem.
I have an Excel XLA which is installed on multiple workstations.
The XLA contains VBA code to create a new workbook and import a report text
file into it and format it nicely.

It then adds a custom commandbutton into the open worksheet on the new
workbook and creates a new sub procedure for the on click event of the
command button using VBA code shown here.

Sub ModifyCommandButton1()


Dim ModEvent As CodeModule 'Module to Modified
Dim LineNum As Long 'Line number in module
Dim SubName As String 'Event to change as text
Dim Proc As String 'Procedure string
Dim EndS As String 'End sub string
Dim Ap As String 'Apostrophe
Dim Tabs As String 'Tab
Dim LF As String 'Line feed or carriage return

Ap = Chr(34)
Tabs = Chr(9)
LF = Chr(13)
EndS = "End Sub"

'Your Event Procedure OR SubRoutine
SubName = "Private Sub CommandButton1_Click()" & LF

'Your Procedure
Proc = "Call FlexTools.modInsertIntoMatstats.ProcessReport" & LF

'Use activeWorkbook so that it can act on another open/Active workbook
Set ModEvent = ActiveWorkbook.VBProject.VBComponents("Sheet1").CodeModule
With ModEvent
LineNum = .CountOfLines + 1
..InsertLines LineNum, SubName & Proc & EndS
End With
End Sub




My new workbook ends up with this code in the onclick botton code.
But when I click the button it cannot find my VBA procedure called
ProcessReport which exists in the VBA Addin.

Iv'e found many links on how to call a procedure from another workbook
including the microsoft solution
below but the problem I have is that the workbook is created on the fly so I
cannot create a reference from my workbook to my XLA Addin.


XL2000: How to Use a Custom Function in Another Workbook
http://support.microsoft.com/kb/213645

Is is possoble to create this reference through VBA? If not does anyone have
any idea on how my new workbook can use code in the Addin?

Regards,
Jerry
 
Reply With Quote
 
 
 
 
Chip Pearson
Guest
Posts: n/a
 
      23rd Nov 2007
Try something like

Sub AAA()
Dim WB As Workbook
Dim WS As Worksheet
Dim OLEObj As OLEObject

Dim CodeString As String

Set WB = Application.Workbooks.Add
CodeString = "MsgBox" & Chr(34) & "hello world" & Chr(34)

Set WS = WB.Worksheets(1)
Set OLEObj = WS.OLEObjects.Add("Forms.CommandButton.1")
OLEObj.Name = "MyButton"
' OR
Set OLEObj = WS.OLEObjects("MyExistingOLEButton")

With WB.VBProject.VBComponents(WS.CodeName).CodeModule
.InsertLines .CreateEventProc("Click", "MyButton") + 1, CodeString
End With
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)


"mccar75287" <(E-Mail Removed)> wrote in message
news:78129DD0-B326-4203-814C-(E-Mail Removed)...
> Here's my problem.
> I have an Excel XLA which is installed on multiple workstations.
> The XLA contains VBA code to create a new workbook and import a report
> text
> file into it and format it nicely.
>
> It then adds a custom commandbutton into the open worksheet on the new
> workbook and creates a new sub procedure for the on click event of the
> command button using VBA code shown here.
>
> Sub ModifyCommandButton1()
>
>
> Dim ModEvent As CodeModule 'Module to Modified
> Dim LineNum As Long 'Line number in module
> Dim SubName As String 'Event to change as text
> Dim Proc As String 'Procedure string
> Dim EndS As String 'End sub string
> Dim Ap As String 'Apostrophe
> Dim Tabs As String 'Tab
> Dim LF As String 'Line feed or carriage return
>
> Ap = Chr(34)
> Tabs = Chr(9)
> LF = Chr(13)
> EndS = "End Sub"
>
> 'Your Event Procedure OR SubRoutine
> SubName = "Private Sub CommandButton1_Click()" & LF
>
> 'Your Procedure
> Proc = "Call FlexTools.modInsertIntoMatstats.ProcessReport" & LF
>
> 'Use activeWorkbook so that it can act on another open/Active workbook
> Set ModEvent = ActiveWorkbook.VBProject.VBComponents("Sheet1").CodeModule
> With ModEvent
> LineNum = .CountOfLines + 1
> .InsertLines LineNum, SubName & Proc & EndS
> End With
> End Sub
>
>
>
>
> My new workbook ends up with this code in the onclick botton code.
> But when I click the button it cannot find my VBA procedure called
> ProcessReport which exists in the VBA Addin.
>
> Iv'e found many links on how to call a procedure from another workbook
> including the microsoft solution
> below but the problem I have is that the workbook is created on the fly so
> I
> cannot create a reference from my workbook to my XLA Addin.
>
>
> XL2000: How to Use a Custom Function in Another Workbook
> http://support.microsoft.com/kb/213645
>
> Is is possoble to create this reference through VBA? If not does anyone
> have
> any idea on how my new workbook can use code in the Addin?
>
> Regards,
> Jerry


 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      23rd Nov 2007
Have you tried Application.Run?

Application.Run "myAddin.xla!myFunction"

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"mccar75287" <(E-Mail Removed)> wrote in message
news:78129DD0-B326-4203-814C-(E-Mail Removed)...
> Here's my problem.
> I have an Excel XLA which is installed on multiple workstations.
> The XLA contains VBA code to create a new workbook and import a report
> text
> file into it and format it nicely.
>
> It then adds a custom commandbutton into the open worksheet on the new
> workbook and creates a new sub procedure for the on click event of the
> command button using VBA code shown here.
>
> Sub ModifyCommandButton1()
>
>
> Dim ModEvent As CodeModule 'Module to Modified
> Dim LineNum As Long 'Line number in module
> Dim SubName As String 'Event to change as text
> Dim Proc As String 'Procedure string
> Dim EndS As String 'End sub string
> Dim Ap As String 'Apostrophe
> Dim Tabs As String 'Tab
> Dim LF As String 'Line feed or carriage return
>
> Ap = Chr(34)
> Tabs = Chr(9)
> LF = Chr(13)
> EndS = "End Sub"
>
> 'Your Event Procedure OR SubRoutine
> SubName = "Private Sub CommandButton1_Click()" & LF
>
> 'Your Procedure
> Proc = "Call FlexTools.modInsertIntoMatstats.ProcessReport" & LF
>
> 'Use activeWorkbook so that it can act on another open/Active workbook
> Set ModEvent = ActiveWorkbook.VBProject.VBComponents("Sheet1").CodeModule
> With ModEvent
> LineNum = .CountOfLines + 1
> .InsertLines LineNum, SubName & Proc & EndS
> End With
> End Sub
>
>
>
>
> My new workbook ends up with this code in the onclick botton code.
> But when I click the button it cannot find my VBA procedure called
> ProcessReport which exists in the VBA Addin.
>
> Iv'e found many links on how to call a procedure from another workbook
> including the microsoft solution
> below but the problem I have is that the workbook is created on the fly so
> I
> cannot create a reference from my workbook to my XLA Addin.
>
>
> XL2000: How to Use a Custom Function in Another Workbook
> http://support.microsoft.com/kb/213645
>
> Is is possoble to create this reference through VBA? If not does anyone
> have
> any idea on how my new workbook can use code in the Addin?
>
> Regards,
> Jerry



 
Reply With Quote
 
mccar75287
Guest
Posts: n/a
 
      23rd Nov 2007
Hi Chip,
I've tried the example you gave me but perhaps my request was not clear
enough.
The example you gave me executes code that is placed on the on click event
of the new button. In your example a messagebox. What I am trying to
accomplish is to execute code contained in my XLA by pushing that button.

The code I gave below already does what your example does in that it
populates the code event for the button. This I already have working.
The issue is that it reads like "Call
FlexTools.modInsertIntoMatstats.ProcessReport" where FlexTools is my project
name (XLA) , modInsertIntoMatstats is the module name within the XLA and
ProcessReport is the procedure.

It seems the newly created workbook cannot find my XLA procedure.

"Chip Pearson" wrote:

> Try something like
>
> Sub AAA()
> Dim WB As Workbook
> Dim WS As Worksheet
> Dim OLEObj As OLEObject
>
> Dim CodeString As String
>
> Set WB = Application.Workbooks.Add
> CodeString = "MsgBox" & Chr(34) & "hello world" & Chr(34)
>
> Set WS = WB.Worksheets(1)
> Set OLEObj = WS.OLEObjects.Add("Forms.CommandButton.1")
> OLEObj.Name = "MyButton"
> ' OR
> Set OLEObj = WS.OLEObjects("MyExistingOLEButton")
>
> With WB.VBProject.VBComponents(WS.CodeName).CodeModule
> .InsertLines .CreateEventProc("Click", "MyButton") + 1, CodeString
> End With
> End Sub
>
>
> --
> Cordially,
> Chip Pearson
> Microsoft MVP - Excel, 10 Years
> Pearson Software Consulting
> www.cpearson.com
> (email on the web site)
>
>
> "mccar75287" <(E-Mail Removed)> wrote in message
> news:78129DD0-B326-4203-814C-(E-Mail Removed)...
> > Here's my problem.
> > I have an Excel XLA which is installed on multiple workstations.
> > The XLA contains VBA code to create a new workbook and import a report
> > text
> > file into it and format it nicely.
> >
> > It then adds a custom commandbutton into the open worksheet on the new
> > workbook and creates a new sub procedure for the on click event of the
> > command button using VBA code shown here.
> >
> > Sub ModifyCommandButton1()
> >
> >
> > Dim ModEvent As CodeModule 'Module to Modified
> > Dim LineNum As Long 'Line number in module
> > Dim SubName As String 'Event to change as text
> > Dim Proc As String 'Procedure string
> > Dim EndS As String 'End sub string
> > Dim Ap As String 'Apostrophe
> > Dim Tabs As String 'Tab
> > Dim LF As String 'Line feed or carriage return
> >
> > Ap = Chr(34)
> > Tabs = Chr(9)
> > LF = Chr(13)
> > EndS = "End Sub"
> >
> > 'Your Event Procedure OR SubRoutine
> > SubName = "Private Sub CommandButton1_Click()" & LF
> >
> > 'Your Procedure
> > Proc = "Call FlexTools.modInsertIntoMatstats.ProcessReport" & LF
> >
> > 'Use activeWorkbook so that it can act on another open/Active workbook
> > Set ModEvent = ActiveWorkbook.VBProject.VBComponents("Sheet1").CodeModule
> > With ModEvent
> > LineNum = .CountOfLines + 1
> > .InsertLines LineNum, SubName & Proc & EndS
> > End With
> > End Sub
> >
> >
> >
> >
> > My new workbook ends up with this code in the onclick botton code.
> > But when I click the button it cannot find my VBA procedure called
> > ProcessReport which exists in the VBA Addin.
> >
> > Iv'e found many links on how to call a procedure from another workbook
> > including the microsoft solution
> > below but the problem I have is that the workbook is created on the fly so
> > I
> > cannot create a reference from my workbook to my XLA Addin.
> >
> >
> > XL2000: How to Use a Custom Function in Another Workbook
> > http://support.microsoft.com/kb/213645
> >
> > Is is possoble to create this reference through VBA? If not does anyone
> > have
> > any idea on how my new workbook can use code in the Addin?
> >
> > Regards,
> > Jerry

>

 
Reply With Quote
 
mccar75287
Guest
Posts: n/a
 
      23rd Nov 2007
Hi Bob,
I've also tried that using "Application.Run "FlexTools.xla!ProcessReport"
and it doesnt work either. It reports Runtime error 424 Object Required.

"Bob Phillips" wrote:

> Have you tried Application.Run?
>
> Application.Run "myAddin.xla!myFunction"
>
> --
> HTH
>
> Bob
>
> (there's no email, no snail mail, but somewhere should be gmail in my addy)
>
> "mccar75287" <(E-Mail Removed)> wrote in message
> news:78129DD0-B326-4203-814C-(E-Mail Removed)...
> > Here's my problem.
> > I have an Excel XLA which is installed on multiple workstations.
> > The XLA contains VBA code to create a new workbook and import a report
> > text
> > file into it and format it nicely.
> >
> > It then adds a custom commandbutton into the open worksheet on the new
> > workbook and creates a new sub procedure for the on click event of the
> > command button using VBA code shown here.
> >
> > Sub ModifyCommandButton1()
> >
> >
> > Dim ModEvent As CodeModule 'Module to Modified
> > Dim LineNum As Long 'Line number in module
> > Dim SubName As String 'Event to change as text
> > Dim Proc As String 'Procedure string
> > Dim EndS As String 'End sub string
> > Dim Ap As String 'Apostrophe
> > Dim Tabs As String 'Tab
> > Dim LF As String 'Line feed or carriage return
> >
> > Ap = Chr(34)
> > Tabs = Chr(9)
> > LF = Chr(13)
> > EndS = "End Sub"
> >
> > 'Your Event Procedure OR SubRoutine
> > SubName = "Private Sub CommandButton1_Click()" & LF
> >
> > 'Your Procedure
> > Proc = "Call FlexTools.modInsertIntoMatstats.ProcessReport" & LF
> >
> > 'Use activeWorkbook so that it can act on another open/Active workbook
> > Set ModEvent = ActiveWorkbook.VBProject.VBComponents("Sheet1").CodeModule
> > With ModEvent
> > LineNum = .CountOfLines + 1
> > .InsertLines LineNum, SubName & Proc & EndS
> > End With
> > End Sub
> >
> >
> >
> >
> > My new workbook ends up with this code in the onclick botton code.
> > But when I click the button it cannot find my VBA procedure called
> > ProcessReport which exists in the VBA Addin.
> >
> > Iv'e found many links on how to call a procedure from another workbook
> > including the microsoft solution
> > below but the problem I have is that the workbook is created on the fly so
> > I
> > cannot create a reference from my workbook to my XLA Addin.
> >
> >
> > XL2000: How to Use a Custom Function in Another Workbook
> > http://support.microsoft.com/kb/213645
> >
> > Is is possoble to create this reference through VBA? If not does anyone
> > have
> > any idea on how my new workbook can use code in the Addin?
> >
> > Regards,
> > Jerry

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Procedure to write a procedure to personal macro workbook... Sam Microsoft Excel Worksheet Functions 0 19th Mar 2008 12:15 AM
Add worksheet to workbook of calling procedure Alan Beban Microsoft Excel Programming 11 15th Dec 2007 06:01 AM
How to jump from a Form procedure to a Workbook or Module procedure? T. Erkson Microsoft Excel Programming 4 25th Jan 2007 07:15 PM
Run procedure in Personal.xls which needs name of calling workbook =?Utf-8?B?cmNtb2RlbHI=?= Microsoft Excel Programming 4 21st Sep 2005 11:16 PM
Calling a Personal.XLS Sub from anther workbook's 'This Workbook' Sheet Activate Jack Gillis Microsoft Excel Discussion 2 21st Mar 2005 11:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:22 PM.