Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Create macro in excel from VB application

Posted on 2001-08-13
3
Medium Priority
?
186 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 1200 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

927 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