2012-08-28 29 views
0

我想將集合數據字典從C#代碼傳遞到Excel宏。 我現在面臨的問題是將字典傳遞到宏時出現錯誤。將C#中的集合字典數據傳遞給宏

下面是宏代碼一個簡單的例子存在於Excel的模塊,我使用:

Public Sub dictionaryTest1(dataku As Variant) 

Dim I As Integer 
For Each v In dataku.Keys 
    MsgBox "Name: " & v & " Age: " & dataku.Item(v) 
Next 
End Sub 

我 而下面是我的C#代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Windows.Forms; 
using Excel = Microsoft.Office.Interop.Excel; 
using Microsoft.Office.Core; 
using System.Reflection; 
using System.IO; 
namespace WindowsMacro 
{ 
    public partial class Form1 : Form 
    { 
public Form1() 
    { 
     InitializeComponent(); 
    } 

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

private void button1_Click(object sender, EventArgs e) 
    { 
     Dictionary<string, int> dataku = new Dictionary<string, int>() 
     { 
      {"cat", 2}, 
      {"dog", 1}, 
      {"llama", 3}, 
      {"iguana", 5} 
     }; 

     object oMissing = System.Reflection.Missing.Value; 

     Excel.ApplicationClass oExcel = new Excel.ApplicationClass(); 
     oExcel.Visible = true; 
     Excel.Workbooks oBooks = oExcel.Workbooks; 
     Excel._Workbook oBook = null; 


     //path to excel file at bin folder 
     string executableLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 
     string xslLocation = Path.Combine(executableLocation, "File.xls"); 

     oBook = oBooks.Open(xslLocation, oMissing, oMissing, 
      oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, 
      oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); 

     //Run macros with passing the dictionary data "dataku". 
     RunMacro(oExcel, new Object[] { "dictionaryTest1", dataku }); 
     // 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; 
     GC.Collect(); //Garbage collection. 
    } 
} 
} 

回答

0

你應該更具體的瞭解你得到的錯誤。無論如何,我認爲你的問題在於你將宏傳遞給錯誤類型的對象。即您正在創建未被VBA識別的Dictionary<string, int>

您沒有指定哪種類型是dictionaryTest1宏接受的類型,但我認爲它是Microsoft腳本運行時的字典。如果是這樣的話,你也應該在C#中創建這種類型的對象。這意味着,玉有Microsoft腳本運行時添加到您的.NET項目的引用,然後創建將傳遞給宏這樣的解釋:

 Scripting.Dictionary dataku = new Scripting.DictionaryClass(); 
     dataku.Add("cat", 2); 
     dataku.Add("dog", 1); 
     dataku.Add("llama", 3); 
     dataku.Add("iguana", 5); 
+0

弗朗西斯嗨,我得到消息的錯誤在Excel宏在運行c#腳本。 「運行時錯誤'424':需要的對象」。任何想法如何創建對象字典數據並將其傳遞給它?如果我用你的方式,我得到一些無效的參數,因爲不能從'字符串'轉換爲'參考對象' – hadi

+0

這正是你所得到的類型是錯誤的。我試圖將你的宏複製到一個excel文件中,並將你的代碼複製到一個.NET項目中,這就是我得到的錯誤。然後,我替換了字典的創建和初始化,一切都很好。你從哪裏得到無效的參數錯誤? –

+0

我添加了參考Microsoft腳本運行時版本1.0。 範圍內創建該字典。 Scripting.Dictionary dataku = new Scripting.DictionaryClass(); dataku.Add(「cat」,2); dataku.Add(「cat」,2); dataku.Add(「dog」,1); dataku.Add(「dog」,1); dataku.Add(「llama」,3); dataku.Add(「iguana」,5); 我收到消息「Scripting.IDictionary.Add(ref object,ref object)has some invalid arguments。」 另一條消息是相關的「不能從變量'string'/'int'轉換爲ref'object' – hadi