Search code examples
c#visual-studiosharepointvisioopenfiledialog

C# OpenFileDialog: The filename, directory name, or volume label syntax is incorrect


I need to programmatically open a document from Sharepoint in Visio. But when I navigate to the network folder, select a document and click on open, I get the following error:

The filename, directory name, or volume label syntax is incorrect enter image description here

When searching for the error, I found the following documentation: https://msdn.microsoft.com/en-us/library/ms832054.aspx. So I guess that the file name contains illegal characters. I tried to use the FileOk event to overwrite the validation of the fileName:

public void openFile() {
    OpenFileDialog sf = new OpenFileDialog();
    sf.FileOk += openFileDialog_FileOk;
    if (sf.ShowDialog() == DialogResult.OK)
    {
        var app =(Microsoft.Office.Interop.Visio.Application)context.Application;
        app.Documents.Open(sf.FileName);
    }
}

private void openFileDialog_FileOk(object sender, CancelEventArgs e)
{
    var sfd = sender as OpenFileDialog;
    var file = new FileInfo(sfd.FileName);
    if (file.Name.Contains('#'))
        e.Cancel = true;
}

but the event does not fire. Using the standard Visio interface it is possible to open files from Sharepoint but the file dialog looks a bit different: enter image description here

How can I get a similar file dialog? And so my questions is: how can I programmatically open a Visio document from Sharepoint (network folder)?


Solution

  • Since Visio does not provide app.GetOpenFilename API, you are out of luck. But you could use another office application for the same. Like Excel for example:

    var excel = new Excel.Application();
    var fileName = excel.GetOpenFilename();
    excel.Quit();
    
    var visio = new Visio.Application();
    visio.Documents.Open(fileName);
    

    which provides a "similar dialog" and "normal url", that is understood by Visio API without any issues.

    The problem probably is that Visio API does not understand UNC DAV file path format with @SSL part, that is provided by the default "built-in" OpenFileDialog (or may be something else as well). Check what is the value of the .FileName returned by the default dialog. BTW, to prevent error messages, it's enough to set sf.CheckFileExists = false, maybe that will be enough.