Why Application.get_Caller(Type.Missing) returns a

2019-08-30 06:00发布

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条回答
ら.Afraid
2楼-- · 2019-08-30 06:18

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.

查看更多
登录 后发表回答