Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XlCall.Excel(XlCall.xlSheetNm, reference) replaces box brackets with round brackets #651

Open
shlomizeevy opened this issue Nov 6, 2023 · 2 comments

Comments

@shlomizeevy
Copy link

Hi there,

I created an Excel Addin using Excel.Dna.I am using ExcelDna.Integration version 1.6.0.
In a custom ExcelFunction function I am trying to get the calling file name using the following code:

` public string TryGetCallingFileName()
{
var reference = (ExcelReference)XlCall.Excel(XlCall.xlfCaller);
var sheetName = (string)XlCall.Excel(XlCall.xlSheetNm, reference);

        var path1 = XlCall.Excel(XlCall.xlfGetDocument, 2, sheetName);
        var path2 = XlCall.Excel(XlCall.xlfGetDocument, 88, sheetName);

        if (path1 is string && path2 is string)
        {
            var pStr1 = path1.ToString();
            var pStr2 = path2.ToString();
            if (FilesUtils.IsLocalFile(pStr1)) // if the path1 is directory -> it's a local file.
            {
                return Path.Combine(FilesUtils.NormalizeDriveLetter(pStr1), pStr2);
            }

            return $"{pStr1.TrimEnd('/')}/{pStr2}"; // one drive case.
        }

        return path2.ToString(); // path1 returns error. path2 is filename (book2) without extension.
    }`

The issue is that file names that contain box brackets, the brackets are replaced by round brackets.
For example, instead of 'book [2]' I'll get 'book (2)
The issue relies on (string)XlCall.Excel(XlCall.xlSheetNm, reference); which concatenates file name with sheet name.
So for sheet name 'Sheet1' I'll get '[book (2)]Sheet1'.

What is the best solution to overcome this?

Thanks,
Shlomi

@govert
Copy link
Member

govert commented Nov 6, 2023

That's an interesting edge case!

It looks like you can take the workbook name you get back from xlSheetNm as an index into the Application.Workbooks collection, and then call FullName to get the real file name.

?Application.Workbooks("Book (3).xlsx").FullName
C:\Temp\Book [3].xlsx

@govert
Copy link
Member

govert commented Nov 6, 2023

I should say the file name you get back from xlfGetDocument, 88 as an index.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants