2014-10-07 9 views
9

के साथ स्वचालन त्रुटि Excel कार्यपुस्तिका लोड करने का प्रयास करते समय मुझे अजीब व्यवहार मिलता है।सशर्त संकलन

मेरे पास एक इंटर-एडइन है, जिसे COM इंटरऑप के साथ .NET में लिखा गया है। इसका मुख्य रूप से मेरा खुद का रिबन-टैब बनाने, मेनू से कार्यपुस्तिका लोड करने और कुछ प्रोजेक्ट प्रशासन करने के लिए उपयोग किया जाता है।

जब मैं किसी कार्यपुस्तिका के दो तरीके का उपयोग कर खोलने का प्रयास करें, मैं अलग परिणाम प्राप्त:

सबसे पहले, जब मैं भीतर ऐड सब कुछ ठीक काम करता है से वर्कबुक (Excel 2003-संस्करण) लोड। रिबन के बटन-इवेंट से, ऐड-इन के सार्वजनिक फ़ंक्शन openWorkbook को एक्सेल वर्कबुक लोड करने के लिए application.workbooks.open(...) का उपयोग किया जाता है।

इस तरह, कार्यपुस्तिका बिना किसी त्रुटि के खुलती है।

दूसरा, जब मैं की तरह कोड का उपयोग कर VBA के भीतर से ऐड-फ़ंक्शन कॉल करने के लिए प्रयास करें:

संकलन त्रुटि

स्वचालन त्रुटि

:

Set addIn = Application.COMAddIns("WMExcelAddin1") 
Set automationObject = addIn.Object 
automationObject.openWorkbook (filename) 

मैं एक त्रुटि संदेश मिलता

और आईडीई पहली घटना में बंद हो जाता है कार्यपुस्तिका-मॉड्यूल में से एक में सशर्त संकलन, की तरह लग रही इस प्रकार है:

#const ebind = 0 
[...] 
sub proc1() 

    #if ebind = 1 then   ' IDE Stops here 
      [...] 
    #else 
      [...] 
    #end if 

end sub 

मैं एक ही प्रभाव के साथ संख्या के बजाय बूलियन डेटाप्रकार का उपयोग करने की कोशिश की।

मैं अपने wits 'अंत में हूँ।

+0

आपने वीबीए को अपनी कक्षाओं और विधियों का खुलासा कैसे किया? [इस तरह कुछ?] (http://davecra.com/2013/02/01/how-to-expose-methods-in-your-vsto-add-in/) –

+0

[vba4all] (http: // stackoverflow .com/उपयोगकर्ता/2140173/vba4all), हाँ - बिल्कुल आपके लिंक में वर्णित है। (VB.NET \t में हालांकि सी # में नहीं)। – DrMarbuse

+1

क्या आपने vba प्रोजेक्ट में अपने एडिन का संदर्भ सेट किया है? – ZAT

उत्तर

1

स्वचालन मोड Excel does not load add-ins by default में। एक्सेल इन शर्तों का उपयोग कर ऐड-इन्स लोड करता है जिसमें ऐड-इन्स संकलित किए गए थे। ऑटोमेशन मोड के दौरान एड-इन कार्यों के लिए, किसी को उस ऐड-इन के आधार पर किसी भी कार्यपुस्तिका को लोड करने से पहले इसे लोड करने के लिए मजबूर होना चाहिए। नीचे मैं अपने असली प्रोजेक्ट (कुछ संस्करणों के साथ) से कोड उदाहरण प्रदान करता हूं जो जेस्क्रिप्ट में इस लोडिंग अनुक्रम को लागू करता है। टिप्पणियां चरणों की व्याख्या करें।

function run_excel() { 
    dbg_log("run_excel"); 
    g_xla_addin = null; 
    g_xla = null; 
    try { 
    g_add_ins = get_excel_app().AddIns; 
    dbg_log("finding add_in.xlam"); 

    //Searching for the installed add-in like Application.COMAddIns("WMExcelAddin1") 
    g_xla_addin = find_addin(g_add_ins, "add_in.xlam"); 
    } catch(v_err) { 
     throw new error(
     "xla_loading" 
     , CR_xla_loading 
     , 'Unexpected error occurred while determining if add_in.xlam is installed.' 
     , v_err 
    ); 
    } 
    if (g_xla_addin == null) throw new error(
     "xla_loading" 
    , CR_xla_not_installed 
    , "MS Excel addin is not installed." 
    ); 
    try { 
    dbg_log("opening add_in.xlam"); 

    //In the example, the add-in has the name of its workbook 
    try { g_xla = g_excel.Workbooks(g_xla_addin.Name); } catch(e) {} 
    if (g_xla == null) { 
     g_excel.AutomationSecurity = 1; // 1 == msoAutomationSecurityLow 

     //Loading the add-in. The add-in also handles `OpenWorkbook` at this time. 
     g_xla = g_excel.Workbooks.Open(
     g_xla_addin.FullName //FileName 
     , 2  //UpdateLinks 
     , true //ReadOnly 
     , null //Format 
     , null //Password 
     , null //WriteResPassword 
     , true //IgnoreReadOnlyRecommended: not display the read-only recommended message 
     , 2  //Origin: xlWindows 
     , null //Delimiter 
     , null //Editable 
     , null //Notify 
     , null //Converter 
     , false //AddToMru: don't add this workbook to the list of recently used files 
     , true //Local: saves files against the language of Microsoft Excel. 
     , 0  //CorruptLoad: xlNormalLoad 
    ); 
     hide_excel(); //To speed up and not interfere with user actions 
    } 
    } catch(v_err) { 
    throw new error(
     "xla_loading" 
    , CR_xla_loading 
    , 'Unexpected error occurred while loading add_in.xlam:\n' 
    , v_err 
    ); 
    } 

    //Now the Add-In is loaded, so the VBA engine knows its API, and the workbook referencing to it are loaded fine. 
    try { 
    g_sig_cat_wbk = g_excel.Workbooks.Open(
     g_sig_cat_fn //FileName 
    , 2  //UpdateLinks 
    , true //ReadOnly 
    , null //Format 
    , null //Password 
    , null //WriteResPassword 
    , true //IgnoreReadOnlyRecommended: not display the read-only recommended message 
    , 2  //Origin: xlWindows 
    , null //Delimiter 
    , null //Editable 
    , null //Notify 
    , null //Converter 
    , false //AddToMru: don't add this workbook to the list of recently used files 
    , false //Local: saves files against the language of Microsoft Excel. 
    , 0  //CorruptLoad: xlNormalLoad 
    ); 

//Calling on the loaded workbook the target macro from the loaded add_in.xlam 
    vba_ret(g_excel.Run(g_xla_addin.Name+"!my_addin.prepare_sig_import", g_sig_cat_wbk.Name)); 
    } catch(v_err) { 
     throw new error(
     "sig_cat_loading" 
     , CR_sig_cat_loading 
     , 'Error occured while loading catalog of signals:\n' 
     , v_err 
    ); 
    } 
    finally { 
    g_sig_cat_wbk.Close(false); 
    g_sig_cat_wbk = null; 
    } 
    dbg_log("run_excel done"); 
} 
संबंधित मुद्दे