Solved

Create macro in excel from VB application

Posted on 2001-08-13
3
180 Views
Last Modified: 2010-05-02
Hi All

I am trying to create a excel macro from VB.
In a VB application i create an excel sheet, workbook
and worksheet.

I would very much like to create a macro. This could
fx. be a simple macro that sets the font size.

After saving the sheet (from VB), I would like to be able to see the macro in excel (alt-F8).

I can see that excel.application in VB has a Sub called MacroOptions, where it is possible to set the ShortCutKey.
There must be some way, to create the macro!?
0
Comment
Question by:eli_frandsen
  • 2
3 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 300 total points
ID: 6380752
   With wkbk
        .VBProject.VBComponents.Add vbext_ct_StdModule ' = 1 if you don't have the library referenced
        .VBProject.VBComponents("Module1").CodeModule.AddFromString "Public Sub Macro1()" & vbLf & "Msgbox " & Chr(34) & "Hello" & Chr(34) & vbLf & "End Sub" & vbLf
    End With

This assumes that wkbk is an excel.workbook object that you have already instantiated.

The code simply adds a standard module to the workbook and adds the text:

Public Sub Macro1()
Msgbox "Hello"
End Sub

To the module created. When you save the workbook this macro will still be there.
0
 
LVL 1

Author Comment

by:eli_frandsen
ID: 6380804
Hello TimCottee

I just knew there had to be a way. And I had thought of
modules but could not quite figure out how to insert them.

Thank you very much, You saved my day.

Regards
Eli Frandsen
0
 
LVL 1

Author Comment

by:eli_frandsen
ID: 6380805
Hello TimCottee

I just knew there had to be a way. And I had thought of
modules but could not quite figure out how to insert them.

Thank you very much, You saved my day.

Regards
Eli Frandsen
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question