2

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" );
}
};
Daniel
  • 68
  • 5

1 Answers1

0

I believe you can use:

Excel.Workbook.SaveCopyAs(destinationPath) 

instead of:

IPersistFile.Save(destinationPath,false); 

This should basically provide the same effect—and is working with Excel.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Doron
  • 1
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jul 28 '23 at 05:41