Solved

Create macro in excel from VB application

Posted on 2001-08-13
3
178 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now