Visio Guy

Visio Discussions => Programming & Code => Topic started by: Visisthebest on July 27, 2021, 10:44:45 AM

Title: How to get the proper filepath to MyShapes with VBA/VB.NET?
Post by: Visisthebest on July 27, 2021, 10:44:45 AM
For use in a Visio VSTO with VB.NET, I use this code from StackOverflow to open the file dialog to let the user select the right stencil.

I do want the file dialog to open in the MyShapes directory of the user currently using Visio, but how do I get this file path from Visio?


Friend Function OpenNewStencilFileDialog() As String

        Dim fd As OpenFileDialog = New OpenFileDialog()
        Dim strFileName As String

        fd.Title = "Open the BowTie Stencil you want to use!"
        fd.InitialDirectory = "C:\"
        fd.Filter = "Visio Macro-Enabled Stencil Files (*.vssm)|*.vssm|Visio Stencil Files (*.vssx)|*.vssx"
        fd.FilterIndex = 2
        fd.RestoreDirectory = True

        If fd.ShowDialog() = DialogResult.OK Then
            strFileName = fd.FileName
        Else
            strFileName = ""
        End If

        Return strFileName

    End Function
Title: Re: How to get the proper filepath to MyShapes with VBA/VB.NET?
Post by: Paul Herber on July 27, 2021, 11:11:44 AM
string mypath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "My Shapes";


is, I think, the best you can do.
Title: Re: How to get the proper filepath to MyShapes with VBA/VB.NET?
Post by: Visisthebest on July 27, 2021, 11:44:58 AM
Super Paul thank you exactly what I was looking for!
Title: Re: How to get the proper filepath to MyShapes with VBA/VB.NET?
Post by: JohnGoldsmith on July 27, 2021, 02:47:13 PM
Hi,
There's also an Application property that reflects the My Shapes path (along with other paths from the File Locations dialog).
Here's some C# (but it's the same properties in VB.NET and VBA):
void Main(){
    var vApp = MyExtensions.GetRunningVisio();
    Console.WriteLine(vApp.MyShapesPath);
    Console.WriteLine(vApp.DrawingPaths);
    Console.WriteLine(vApp.TemplatePaths);
    Console.WriteLine(vApp.StencilPaths);
    Console.WriteLine(vApp.HelpPaths);
    Console.WriteLine(vApp.AddonPaths);
    Console.WriteLine(vApp.StartupPaths);   
}
MyShapesPath returns a single path, while the others are a string array split on a semi-colon character.
Best regards
John
Title: Re: How to get the proper filepath to MyShapes with VBA/VB.NET?
Post by: Visisthebest on July 27, 2021, 04:11:55 PM
Thank you very much John very good to see you in this forum again! :) :) :)
Title: Re: How to get the proper filepath to MyShapes with VBA/VB.NET?
Post by: Visisthebest on July 28, 2021, 09:29:43 AM
John, in Visual Studio I see that Application.StencilPaths is a string, but if I debug.print from the VSTO I actually get an empty string.

I might be using this incorrectly, but this is what I get.
Title: Re: How to get the proper filepath to MyShapes with VBA/VB.NET?
Post by: JohnGoldsmith on July 28, 2021, 10:04:57 AM
Hi,
Does anything display in the Stencils textbox of your File Locations dialog?  If that is empty then the property will be too.
Best regards
John
Title: Re: How to get the proper filepath to MyShapes with VBA/VB.NET?
Post by: Visisthebest on July 29, 2021, 10:24:05 AM
John I am not sure where to find this, but if from the stencils pane (see screenshot) I go to open stencil, it opens in the right location, the My Shapes directory in Documents.

Because I use OneDrive, the exact path is:

C:\Users\visisthebest\OneDrive\Documents\My Shapes

Title: Re: How to get the proper filepath to MyShapes with VBA/VB.NET?
Post by: Nikolay on July 29, 2021, 11:14:46 AM
You need to use "Application.MyShapesPath" not "Application.StencilPaths".
Title: Re: How to get the proper filepath to MyShapes with VBA/VB.NET?
Post by: Visisthebest on July 29, 2021, 11:25:12 AM
Thank you Nikolay my mistake!
Title: Re: How to get the proper filepath to MyShapes with VBA/VB.NET?
Post by: Visisthebest on July 29, 2021, 02:09:18 PM
Using Application.MyShapesPath now and getting exactly the stencil location I need!