Struggling when transforming VBA code to Python using win32com module

Started by AhmedAl, July 21, 2019, 12:55:15 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Yacine

Yacine

AhmedAl

Hi everyone,

Right now, I'm trying to inset a stencil in an empty visio file.
I'm hesitating between literally copying and pasting this stencil in my empty visio (my preferred option) or reproducing the shape by myself directly in my visio (I consider this as my last option).

I tried to the copy/paste option using macro recording but it didn't work in python.
Below the recorded macro, the stencil is in the attachements:

Sub Macro1()

    'Enable diagram services
    Dim DiagramServices As Integer
    DiagramServices = ActiveDocument.DiagramServicesEnabled
    ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150

    Application.ActiveWindow.Page.Paste

    'Restore diagram services
    ActiveDocument.DiagramServicesEnabled = DiagramServices

End Sub


Did anyone try this copy/paste procedure ?

Thanks  :)

Paul Herber

You can't copy and paste stencils into a Visio document, you need to use the ActiveDocument.Open or .OpenEx methods.
Electronic and Electrical engineering, business and software stencils for Visio -

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

Yacine

Hi Ahmed,

I think you want to drop a master from a stencil on your drawing, isn't it?

The recorder would produce:
Sub Makro1()

    'Enable diagram services
    Dim DiagramServices As Integer
    DiagramServices = ActiveDocument.DiagramServicesEnabled
    ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150

    Application.Windows.ItemEx("Zeichnung1").Activate
    Application.ActiveWindow.Page.Drop Application.Documents.Item("C:\Users\  ...  \Downloads\Gabarit1.vssx").Masters.ItemU("Gd Op.E-V.112"), 9.027862, 7.677165

    'Restore diagram services
    ActiveDocument.DiagramServicesEnabled = DiagramServices

End Sub

Most of the code is useless, you need to strip it down.

    'Enable diagram services
    Dim DiagramServices As Integer
    DiagramServices = ActiveDocument.DiagramServicesEnabled
    ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150


    Application.Windows.ItemEx("Zeichnung1").Activate
    Application.ActiveWindow.Page.Drop Application.Documents.Item("C:\Users\  ...  \Downloads\Gabarit1.vssx").Masters.ItemU("Gd Op.E-V.112"), 9.027862, 7.677165

    'Restore diagram services
    ActiveDocument.DiagramServicesEnabled = DiagramServices


It's the blue text you were after (drop)

The red code translates simplier to "activepage"

The green code is the name of your stencil - with or without path, depending on whether you saved it to your stencils folder or somewhere else.

The purple code is the name of the master to drop

and finally the 2 numbers are the coordinates where to preform the drop

You can google all these parameters.

In VBA you will get:
ActivePage.Drop Application.Documents.Item(stencil_name).Masters.ItemU(master_name), x, y

In Python you would just modify the parantheses, and because you have some preparation work, there won't be directly an activepage - you would have stored it in variable.
visio_page.Drop(visio_app.Documents.Item(stencil_name).Masters.ItemU(master_name), x, y)
Yacine

AhmedAl

Thanks Yacine & Paul.

Actually, a stencil has exactly the characteristics of a shape i.e. you can modify its background/foreground color, dimensions, color ... (beginner's discoveries ;D )

Here is the small script for stencils:

objVdProc = objVapp.Documents.Add("")
objVdProc.Pages(1).Name = "PdG"

stencilpath = "C:\\Users/........................\\Gabarit1.vssx"
objVapp.Documents.Open(stencilpath)

objVapp.Windows.ItemEx("Dessin1").Activate
Forme= objVdProc.Pages("PdG").Drop(objVapp.Documents.Item("Gabarit1.vssx").Masters.ItemU("Gd Op.E-V.112"), 8.46875, 8.06664)

Forme.CellsSRC(constants.visSectionObject, constants.visRowXFormOut, constants.visXFormWidth).FormulaU = "117.7644 pt"
Forme.CellsSRC(constants.visSectionObject, constants.visRowXFormOut, constants.visXFormHeight).FormulaU = "127.5781 pt"

Forme.CellsSRC(constants.visSectionObject, constants.visRowXFormOut, constants.visXFormPinX).FormulaU = "609.75 pt"
Forme.CellsSRC(constants.visSectionObject, constants.visRowXFormOut, constants.visXFormPinY).FormulaU = "555.0 pt"

Forme.CellsSRC(constants.visSectionObject, constants.visRowFill, constants.visFillForegnd).FormulaU = "THEMEGUARD(RGB(%s,%s,%s))" %(R_Fond1, V_Fond1, B_Fond1)
Forme.CellsSRC(constants.visSectionObject, constants.visRowFill, constants.visFillPattern).FormulaU = "1"


wapperdude

Think you're missing Paul's point, and, you're mixing terms.

Stencil is a collection of shapes.
Shapes are the objects that get placed upon a drawing page.  They may be 2-D shapes, e.g,. square, circle, or 1-D shapes, e.g., lines, connectors, or other items, e.g., picture, Excel worksheet.

So, when you you use those terms interchangeably, gets quite confusing for both us and for you.

For example, your code line: Forme= objVdProc.Pages("PdG").Drop(objVapp.Documents.Item("Gabarit1.vssx").Masters.ItemU("Gd Op.E-V.112"), 8.46875, 8.06664) is dropping  shape GD O.E-V.112, from stencil Gabarit1.vssx.  And, yes, you can do some shape editing while it still resides on the stencil.

Hope this clears up the difference in the terms.  It'll make communication easier and less confusing.


Visio 2019 Pro

Yacine

Yacine

Remko

Application.Documents.Item("C:\Users\  ...  \Downloads\Gabarit1.vssx")

How would one go about to change the path so it points to the Document Stencil? (Master?)

so that when you rename the visio file its still works

Paul Herber

The document stencil doesn't exist as a stencil entity that you can create a path to.
You can access it via the Masters object.
Electronic and Electrical engineering, business and software stencils for Visio -

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

Remko

I cant find out how. im not a programmer.

this is my current working code:

ThisDocument.Application.Windows(ThisDocument.Index).Activate
Application.ActiveWindow.Page.Drop Application.Documents.Item("Template.vsdm").Masters.ItemU("Titleblock_01"), 0, 0#


How would it look when one take out the Template.vsdm and how then call on the master?