2013-01-09 54 views
6

Tôi đang tìm cách chạy macro, hãy gọi Macro01 từ WorkSheet01 trên WorkSheet02.Chạy Macro Excel qua C#: Chạy macro từ sổ làm việc này sang sổ làm việc khác?

Sử dụng Microsoft.Office.Interop.Excel Namespace Tôi đã mở một WorkSheet01.

public void Main_CodedStep() 
    { 
     // Object for missing (or optional) arguments. 
     object oMissing = System.Reflection.Missing.Value; 

     // Create an instance of Microsoft Excel 
     Excel.ApplicationClass oExcel = new Excel.ApplicationClass(); 

     // Make it visible 
     oExcel.Visible = true; 

     // Open Worksheet01.xlsm 
     Excel.Workbooks oBooks = oExcel.Workbooks; 
     Excel._Workbook oBook = null; 
     oBook = oBooks.Open("C:\\Users\\Admin\\Documents\\Worksheet01.xlsm", oMissing, oMissing, 
      oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, 
      oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); 
    } 

Sau đó, tôi sử dụng tập lệnh tự động để lấy báo cáo. Báo cáo này được mở qua lời nhắc tải xuống của IE và không phải là Interop.

vấn đề này được đưa ra khi tôi cố gắng để chạy các macro qua C# (Tôi đã thực hiện một Excel.ApplicationClass mới(); chỉ nên biên soạn, tôi tin rằng đây là một trong những sai lầm của tôi.)

public void FirstMacro_CodedStep() 
    { 
     // Create an instance of Microsoft Excel 
     Excel.ApplicationClass oExcel = new Excel.ApplicationClass(); 
     Console.WriteLine("ApplicationClass: " + oExcel); 

     // Run the macro, "First_Macro" 
     RunMacro(oExcel, new Object[]{"Worksheet01.xlsm!First_Macro"}); 

     //Garbage collection 
     GC.Collect(); 
    } 

    private void RunMacro(object oApp, object[] oRunArgs) 
    { 
     oApp.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs); 
    } 

Khi phương pháp này chạy nó chạy macro từ Worksheet01 trên Worksheet01 thay vì Worksheet02. Ngoài ra, nó đang tìm kiếm bảng tính trong My Documents nên tôi đã chuyển nó sang để xem điều gì sẽ xảy ra.

Tóm tắt:

  1. mở Worksheet01
  2. Via kịch bản nhận được và mở một báo cáo (Worksheet02) từ MSIE
  3. Run Macro01 từ Worksheet01 trên Worksheet02

Resources:

http://support.microsoft.com/kb/306683

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.aspx

Đối với những người muốn thử nó thêm video này vào sử dụng của bạn dùng các directives:

using System.Reflection; 
using Microsoft.Office.Core; //Added to Project Settings' References from C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14 - "office" 
using Excel = Microsoft.Office.Interop.Excel; //Added to Project Settings' References from C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14 - "Microsoft.Office.Interop.Excel" 
+0

Vui lòng kiểm tra này một http://stackoverflow.com/questions/27166341/run-microsoft-office-macro-using-c-sharp –

Trả lời

5

Tôi tìm thấy một giải pháp mà tôi muốn chia sẻ. Trước tiên, tôi đã xóa bit mà tôi đã mở Worksheet01. Sau đó, tôi đã có tập lệnh tự động lưu tệp .CSV vào Tài liệu của tôi. Sau đó tôi đã sử dụng mã tôi phải mở Worksheet01 để mở tệp đã tải xuống. Điều quan trọng ở đây là Worksheet01 nằm trong thư mục Documents với Worksheet02. Cuối cùng tôi đã sử dụng mã để chạy macro từ Worksheet01, chạy trên Worksheet02.

public void WebTest_CodedStep() 
    { 
     // Object for missing (or optional) arguments. 
     object oMissing = System.Reflection.Missing.Value; 

     // Create an instance of Microsoft Excel 
     Excel.ApplicationClass oExcel = new Excel.ApplicationClass(); 

     // Make it visible 
     oExcel.Visible = true; 

     // Define Workbooks 
     Excel.Workbooks oBooks = oExcel.Workbooks; 
     Excel._Workbook oBook = null; 

     // Get the file path 
     string path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 
     path = path + "\\Worksheet02.csv"; 

     //Open the file, using the 'path' variable 
     oBook = oBooks.Open(path, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); 

     // Run the macro, "First_Macro" 
     RunMacro(oExcel, new Object[]{"Worksheet01.xlsm!First_Macro"}); 

     // Quit Excel and clean up. 
     oBook.Close(false, oMissing, oMissing); 
     System.Runtime.InteropServices.Marshal.ReleaseComObject (oBook); 
     oBook = null; 
     System.Runtime.InteropServices.Marshal.ReleaseComObject (oBooks); 
     oBooks = null; 
     oExcel.Quit(); 
     System.Runtime.InteropServices.Marshal.ReleaseComObject (oExcel); 
     oExcel = null; 

     //Garbage collection 
     GC.Collect(); 
    } 

    private void RunMacro(object oApp, object[] oRunArgs) 
    { 
     oApp.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs); 
    } 
+1

RunMacro chỉ hoạt động nếu tệp macro nằm trong thư mục Tài liệu của người dùng. Bất kỳ nỗ lực nào để cung cấp đường dẫn đầy đủ đến tệp ở nơi khác dẫn đến thông báo lỗi "macro không được tìm thấy hoặc macro đã bị hủy kích hoạt". – waka

+0

Có, tôi đã thực hiện một số lần thử và cách này để gọi macro (với InvokeMember) chỉ hoạt động khi macro của tôi là phisically trong thư mục "Tài liệu của tôi". Cố gắng xác định vị trí khác (với macro được sao chép ở vị trí đó) không hoạt động. Tôi cũng đã thử với lớp Microsoft.Office.Interop.Excel.Application, phương thức Run và nó chỉ hoạt động trong cùng điều kiện: macro filel phisically trong thư mục "My Documents". – EAmez

0

Tôi chạy này C# mã VSTO để gọi một VBA Macro, đây là cú pháp tôi sử dụng:

this.Application.Run("mymacro"); 

Edit:

Macro là Workbook rộng, có lẽ bạn cần phải thực hiện Sheet2 bảng tính hoạt động trước khi chạy macro, ví dụ:

foreach (Worksheet worksheet in workbook.Sheets.ComLinq<Worksheet>()) 
{ 
    if (worksheet.Name == "Sheet2") worksheet.Activate(); 
} 
+0

Xin lỗi, tôi đã không nhận ra ước đặt tên của tôi có một chút sai lầm. Những gì tôi gọi là Worksheet01 và Worksheet02 là hai tập tin khác nhau (một là một xlsm và một khác csv). – kirbycope

Các vấn đề liên quan