How to get the proper filepath to MyShapes with VBA/VB.NET?

Started by Visisthebest, July 27, 2021, 10:44:45 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Visisthebest

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
Visio 2021 Professional

Paul Herber

string mypath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "My Shapes";


is, I think, the best you can do.
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

Visisthebest

Visio 2021 Professional

JohnGoldsmith

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
John Goldsmith - Visio MVP
http://visualsignals.typepad.co.uk/

Visisthebest

Thank you very much John very good to see you in this forum again! :) :) :)
Visio 2021 Professional

Visisthebest

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.
Visio 2021 Professional

JohnGoldsmith

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
John Goldsmith - Visio MVP
http://visualsignals.typepad.co.uk/

Visisthebest

#7
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

Visio 2021 Professional

Nikolay

You need to use "Application.MyShapesPath" not "Application.StencilPaths".

Visisthebest

Visio 2021 Professional

Visisthebest

Using Application.MyShapesPath now and getting exactly the stencil location I need!
Visio 2021 Professional