Why Application.get_Caller(Type.Missing) returns a negative integer?

╄→гoц情女王★ 提交于 2019-12-12 04:43:41

问题


There's something weird about get_Caller(Type.Missing) method. It returns a negative integer, -2146826265, instead of the Range object as it should.

Has anyone come across this issue before? Why is that and how should I solve it?

Thanks.

Excel.Range range = (Excel.Range) application.get_Caller(System.Type.Missing);

The above code would fail if I try to explicitly user type Excel.Range. The error message says, 'Cannot convert type 'int' to 'Microsoft.Office.Interop.Excel.Range'.

EDIT:

The intention of getting caller of the cell is to pass it to my following function:

private string getResultFromResultSheet(Excel.Range originalSheetRange, Excel.Worksheet resultSheet)
        {
            string DataResult = "";
            try
            {
                string os_currentAddress = originalSheetRange.get_Address(Type.Missing, Type.Missing, Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing);
                Excel.Range currentRRange = null;

                currentRRange = resultSheet.get_Range(os_currentAddress, Type.Missing);
                if (currentRRange != null)
                {
                    if (string.IsNullOrEmpty(Convert.ToString(currentRRange.Value)))
                        DataResult = "";
                    else
                        DataResult = Convert.ToString(currentRRange.Value);
                }
            }
            catch (Exception ex)
            {
            }
            return DataResult;
        }

With the return value from that function, I can pass it back to UDF and display it in the original cell. Is there any better way to implement the function?


回答1:


Review the table at the end of this MSDN Library article about the Application.Caller property. You've discovered the value of the #REF! error. Google 'excel error 2023' for additional info. I kinda doubt you can use this property, given that the caller is your C# program.



来源:https://stackoverflow.com/questions/7887360/why-application-get-callertype-missing-returns-a-negative-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!