I implement a vsto plugin for Excel. I need to get local path of the Excel document from Teams channel but Application.ActiveWorkbook.FullName
contains url.
I should be able to use IPersistFile.Save(path, false)
and create local copy of the document without changing current working document. Unfortunately, it doesn't work in Excel. It creates a local copy, but current working document is replaced with local copy. If user do some changes to the document then Excel won't synchronize it with Teams/Sharepoint.
Is it a bug in API or I need to do something else to fix it?
Is there a different way to get a copy of the file without changing current working document?
I'm able to reproduce issue if I open Excel document from Teams channel and use vsto add-in with below code. Current working document is replaced with local copy but it shouldn't. I cannot get local copy.
this.Application.WorkbookOpen += ( Excel.Workbook wb ) =>
{
if ( !Uri.TryCreate( Application.ActiveWorkbook.FullName, UriKind.Absolute, out Uri uri ) || uri.Scheme == "file" )
{
Debug.WriteLine( $"It's local file: {Application.ActiveWorkbook.FullName}" );
return;
}
Debug.WriteLine( $"It's online file: {Application.ActiveWorkbook.FullName}" );
var fileName = uri.LocalPath.Split( '/' ).Last();
var extension = fileName.Split( '.' ).Last();
var fileNameWithoutExtension = fileName.Substring( 0, fileName.Length - extension.Length - 1 );
var path = Path.Combine( Path.GetTempPath(), $"{fileNameWithoutExtension}_{Guid.NewGuid()}.{extension}" );
if ( wb is IPersistFile persistFile )
{
persistFile.Save( path, false );
//current working document has been replaces with {path}
Debug.WriteLine( $"Local copy has been created: {path}" );
}
else
{
Debug.WriteLine( $"Unable to create local copy" );
}
};