0
votes

I have a Visio stencil document with some shapes and I want to add a shape contained inside it to my document. Based on this example I was able to do it, the only issue is how to get rid of the dock panel which appears when opening stencil using Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visOpenDocked flag.

So after import I close the opened stencil document but the dock panel stays. Maybe I could close it programmatically too, but then I should consider complicated logic with tracking wheater this was opened or not to keep UI unchanged if the user opened this panel previously etc.

My question is there another option to import a shape from a stencil or a workaround for this panel and stencil document opening options (for instance to open stencil document hidden for user and close it afterwards silently)

            // Microsoft.Office.Interop.Visio.Application Application
            var documents = Application.Documents;
            var document = documents.Add("");
            var page = Application.ActivePage;
            var visioStencil = documents.OpenEx(
                @"c:\Users\user\Desktop\stencil.vssx",
                (short)Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visOpenDocked);
            var masters = visioStencil.Masters;

            for (var i = 1; i <= masters.Count; ++i)
            {
                var item = masters.get_ItemU(i);
                var name = item.Name;

                if (name == "Master.2")
                {
                    page.Drop(item, 10, 10);
                    break;
                }
            }

            visioStencil.Close();
2

2 Answers

1
votes

You can open the stencil document in a 'hidden' state and also use the Masters.Drop method to add directly to the target masters collection like this:

var targetDoc = vApp.Documents.Add("");
var sourceDoc = vApp.Documents.OpenEx(
    @"c:\Users\user\Desktop\stencil.vssx",
    (short)Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visAddHidden);
var sourceMasters = sourceDoc.Masters;

for (var i = 1; i <= sourceMasters.Count; ++i)
{
    var sourceMaster = sourceMasters[i];
    if (sourceMaster.Name == "Master.2")
    {
        targetDoc.Masters.Drop(sourceMaster, 10, 10);
        break;
    }
}

sourceDoc.Close();

Note that the if the target document already contains a master of the same name Visio will create a new master and append a number on the end. Also, bear in mind that Name and NameU may be different so you might want to match on the latter instead.

1
votes

No need to loop through all the shapes in the stencil. You can access the shape by name:

targetDoc.Masters.Drop(sourceMasters["Master.2"], 10, 10);