Author Topic: How to get the proper filepath to MyShapes with VBA/VB.NET?  (Read 5363 times)

0 Members and 1 Guest are viewing this topic.

Visisthebest

  • Sr. Member
  • ****
  • Posts: 433
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?

Code
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3080
    • Paul Herber's website
Re: How to get the proper filepath to MyShapes with VBA/VB.NET?
« Reply #1 on: July 27, 2021, 06:11:44 AM »
Code
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 and applications for Visio -

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

Visisthebest

  • Sr. Member
  • ****
  • Posts: 433
Re: How to get the proper filepath to MyShapes with VBA/VB.NET?
« Reply #2 on: July 27, 2021, 06:44:58 AM »
Super Paul thank you exactly what I was looking for!
Visio 2021 Professional

JohnGoldsmith

  • Sr. Member
  • ****
  • Posts: 353
    • John Goldsmith's visLog
Re: How to get the proper filepath to MyShapes with VBA/VB.NET?
« Reply #3 on: July 27, 2021, 09:47:13 AM »
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):
Code
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

  • Sr. Member
  • ****
  • Posts: 433
Re: How to get the proper filepath to MyShapes with VBA/VB.NET?
« Reply #4 on: July 27, 2021, 11:11:55 AM »
Thank you very much John very good to see you in this forum again! :) :) :)
Visio 2021 Professional

Visisthebest

  • Sr. Member
  • ****
  • Posts: 433
Re: How to get the proper filepath to MyShapes with VBA/VB.NET?
« Reply #5 on: July 28, 2021, 04: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.
Visio 2021 Professional

JohnGoldsmith

  • Sr. Member
  • ****
  • Posts: 353
    • John Goldsmith's visLog
Re: How to get the proper filepath to MyShapes with VBA/VB.NET?
« Reply #6 on: July 28, 2021, 05: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
John Goldsmith - Visio MVP
http://visualsignals.typepad.co.uk/

Visisthebest

  • Sr. Member
  • ****
  • Posts: 433
Re: How to get the proper filepath to MyShapes with VBA/VB.NET?
« Reply #7 on: July 29, 2021, 05: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

Visio 2021 Professional

Nikolay

  • Hero Member
  • *****
  • Posts: 1072
    • UnmanagedVisio
Re: How to get the proper filepath to MyShapes with VBA/VB.NET?
« Reply #8 on: July 29, 2021, 06:14:46 AM »
You need to use "Application.MyShapesPath" not "Application.StencilPaths".
« Last Edit: July 29, 2021, 06:21:37 AM by Nikolay »

Visisthebest

  • Sr. Member
  • ****
  • Posts: 433
Re: How to get the proper filepath to MyShapes with VBA/VB.NET?
« Reply #9 on: July 29, 2021, 06:25:12 AM »
Thank you Nikolay my mistake!
Visio 2021 Professional

Visisthebest

  • Sr. Member
  • ****
  • Posts: 433
Re: How to get the proper filepath to MyShapes with VBA/VB.NET?
« Reply #10 on: July 29, 2021, 09:09:18 AM »
Using Application.MyShapesPath now and getting exactly the stencil location I need!
Visio 2021 Professional