CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2007
    Posts
    68

    Writing into Excel VBA from Dot net code

    I have a Dot Net application which launches MS Access. I am able to write the values into excel also.
    But I need to add Macro for the excel to calculate, save etc.

    Normally the VBA editor opens up in Excel when Alt-F11 is pressed.
    There are a few Subroutines inside the VBA editor as follows:
    Private Sub Worksheet_Calculate()
    End Sub

    Private Sub Worksheet_Change(ByVal Target As Range)
    End Sub

    Now from my dot net code I want to write into these methods. How can I do that. May be I need to create a separate function also inside the VBA editor.

    Thanks in advance.

  2. #2
    Join Date
    May 2002
    Location
    Boston
    Posts
    67

    Re: Writing into Excel VBA from Dot net code

    Hi,

    I'm not sure if this is what you're looking for. If you just want run a macro in a saved wookbook try this...

    Code:
    Dim ExcelApp As Excel.Application
            Dim MySheet As Excel.Worksheet
            Dim MyBook As Excel.Workbook
    
            Try
    
                ExcelApp = New Excel.Application
    
                ExcelApp.UserControl = True
                ExcelApp.Visible = True
    
                ExcelApp.Workbooks.Open("C:\Temp\Book1.xlsm", False, True)
    
    
                MyBook = ExcelApp.ActiveWorkbook
    
                MySheet = MyBook.Worksheets(1)
                MySheet.Activate()
    
                ExcelApp.Application.Run("Book1.xlsm!Macro1")
    
            Catch ex As Exception
    
            End Try

    Curt

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured