Solved

Create macro in excel from VB application

Posted on 2001-08-13
3
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month4 days, 13 hours left to enroll

636 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