Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 6
Default Programatically adding macro to Excel - "ThisWorkbook"

I have found references and code on the net which helps me add code in the
form of an Excel Macro. However it shows how to create a new module and then
add functions to it. How to add functions to the thisWorkbook part of the
File.

the link that shows how to programatically add macro o a new module is:
http://support.microsoft.com/kb/303872

i dont want to add it to a new module but to "ThisWorkbook"

Please help
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 9,101
Default Programatically adding macro to Excel - "ThisWorkbook"

Replace these statements in your code

from

// Create an instance of Excel.
oExcel = new Excel.Application();

// Add a workbook.
oBook = oExcel.Workbooks.Add(oMissing);


to
oExcel = new Excel.Application();

oBook = oExcel.Workbooks.open("C:\temp\book1.xls);


I assume you are creating an Excel Application and then opening a workbook.
If you are just adding code to the opened workbook then use this

oBook = Thisworkbook


If you are working in a macro and not C# you need to put a "SET" in front of
each command.

"Aerojade" wrote:

I have found references and code on the net which helps me add code in the
form of an Excel Macro. However it shows how to create a new module and then
add functions to it. How to add functions to the thisWorkbook part of the
File.

the link that shows how to programatically add macro o a new module is:
http://support.microsoft.com/kb/303872

i dont want to add it to a new module but to "ThisWorkbook"

Please help

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 6
Default Programatically adding macro to Excel - "ThisWorkbook"

I am not quite able to understand what you have explained. So am going to try
to be more clear.

I m trying to use a Windows Application written in C# to programmatically
add a macro function into an Excel File. However when i use the following
code in the button click event of my windows application:




Excel.Application oExcel;
Excel.Workbook oBook;
VBIDE.VBComponent oModule;
VBIDE.VBProject oMOD;
Office.CommandBar oCommandBar;
Office.CommandBarButton oCommandBarButton;
String sCode;
Object oMissing = System.Reflection.Missing.Value;

// Create an instance of Excel.
oExcel = new Excel.Application();

// Add a workbook.
oBook = oExcel.Workbooks.Open("C:\\test.xls");
//oModule = oMOD.VBComponents("ThisWorkbook");

// Create a new VBA code module.
oModule =
oBook.VBProject.VBComponents.Add(VBIDE.vbext_Compo nentType.vbext_ct_StdModule);


sCode =
"Private Sub Workbook_BeforePrint(Cancel As Boolean)\r\n" +
" msgbox \"VBA Macro called\"\r\n" +
"Cancel=true\r\n"+
"end sub";
// Add the VBA macro to the new code module.
oModule.CodeModule.AddFromString(sCode);
MessageBox.Show("Macro added");
oExcel.Visible = true;


i find that it executes properly but what it does is... it adds a new module
to the excel file's macro and then writes the function there. However i want
to write my code into the "ThisWorkbook" part of the VBA project because it
is only there that i can control the events occuring in Excel. I know it can
be done usig an addin... but i want to perform it this way instead.

Please Help.
  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 9,101
Default Programatically adding macro to Excel - "ThisWorkbook"

I don't use C# but I think this is the correction

from
// Create a new VBA code module.
oModule =
oBook.VBProject.VBComponents.Add(VBIDE.vbext_Compo nentType.vbext_ct_StdModule);

to
// Create a new VBA code module.
oModule = oBook.VBProject.VBComponents("ThisWorkBook")

"Aerojade" wrote:

I am not quite able to understand what you have explained. So am going to try
to be more clear.

I m trying to use a Windows Application written in C# to programmatically
add a macro function into an Excel File. However when i use the following
code in the button click event of my windows application:




Excel.Application oExcel;
Excel.Workbook oBook;
VBIDE.VBComponent oModule;
VBIDE.VBProject oMOD;
Office.CommandBar oCommandBar;
Office.CommandBarButton oCommandBarButton;
String sCode;
Object oMissing = System.Reflection.Missing.Value;

// Create an instance of Excel.
oExcel = new Excel.Application();

// Add a workbook.
oBook = oExcel.Workbooks.Open("C:\\test.xls");
//oModule = oMOD.VBComponents("ThisWorkbook");

// Create a new VBA code module.
oModule =
oBook.VBProject.VBComponents.Add(VBIDE.vbext_Compo nentType.vbext_ct_StdModule);


sCode =
"Private Sub Workbook_BeforePrint(Cancel As Boolean)\r\n" +
" msgbox \"VBA Macro called\"\r\n" +
"Cancel=true\r\n"+
"end sub";
// Add the VBA macro to the new code module.
oModule.CodeModule.AddFromString(sCode);
MessageBox.Show("Macro added");
oExcel.Visible = true;


i find that it executes properly but what it does is... it adds a new module
to the excel file's macro and then writes the function there. However i want
to write my code into the "ThisWorkbook" part of the VBA project because it
is only there that i can control the events occuring in Excel. I know it can
be done usig an addin... but i want to perform it this way instead.

Please Help.

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
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell Steve Kay Excel Discussion (Misc queries) 2 August 8th 08 01:54 AM
=SUM((C2:C7000="Friday")*(F2:F7000="8:00:00 AM")) not adding chubbybat Excel Discussion (Misc queries) 9 March 5th 08 01:40 PM
How to do the "REDO" action in Excel programatically. Daffo Excel Discussion (Misc queries) 2 December 4th 06 11:41 AM
Macro to concatenate into "B1" B2 thru B"x" based on new data in "Col A" Dennis Excel Discussion (Misc queries) 0 July 17th 06 02:38 PM
Adding A "Timer" to a Macro carl Excel Worksheet Functions 2 February 27th 06 04:01 PM


All times are GMT +1. The time now is 12:03 PM.

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"