VBA to Control Viso from Access

Started by NeilClark, February 15, 2018, 06:49:19 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Dupes

Thanks for replying,
stencil are build in to visio itself

Project is basically to create a cabinet diagram with multiple components placed inside the cabinet.
So i have created templates for individual components. depending on the design of the cabinet and the components which will select using forms in ms access i wish to copy the these specific components from template to the final visio drawing

i am struggling on how to do this in MS Access VBA

Surrogate

Try something like this
Sub bbb()
Dim FName As String, VisioApp As Object
Set VisioApp = GetObject(, "Visio.Application")

If VisioApp Is Nothing Then
    Set VisioApp = CreateObject("Visio.Application")
    If VisioApp Is Nothing Then
       MsgBox "Can't connect to Visio"
       Exit Sub
    End If
End If
Dim td As Object ' target visio document
Dim tp As Object ' active page in target document
Set td = VisioApp.Documents.OpenEx("c:\Test\change shape issue.vsd", visOpenRW) ' define your own target doc
Set tp = VisioApp.ActiveWindow.Page
Dim ts As Object ' define stencil
Dim tm As Object 'define master-shape for drop
Set ts = VisioApp.Documents("BASFLO_M.vssx") ' define your desired external stencil
Set tm = td.Masters.ItemU("Circle") ' define your own desired master shape
tp.Drop tm, 0, 0 ' instead "0,0" you need  assign you real coordinates each time
' Do something else
End Sub


Dupes

Thanks for your help,

I am getting an error at the following line:

Set tm = td.Masters.ItemU("Square")

Error says "Object cannot be found"

i am using following stencil:
Set ts = VisioApp.Documents("basic_u.vss")

Any idea, thanks again

Surrogate

#18
Quote from: Dupes on April 09, 2018, 02:45:23 PMI am getting an error at the following line:

Set tm = td.Masters.ItemU("Square")

Error says "Object cannot be found"
This code try find master shape named "Square" in your target document (td). If your document don't have this master in it Document stencil you get error !

Quote from: Dupes on April 09, 2018, 02:45:23 PMi am using following stencil:
Set ts = VisioApp.Documents("basic_u.vss")
This code works only if this stencil already open
Please try this modified code
Sub bbb()
Dim FName As String, VisioApp As Object
Set VisioApp = GetObject(, "Visio.Application")

If VisioApp Is Nothing Then
    Set VisioApp = CreateObject("Visio.Application")
    If VisioApp Is Nothing Then
       MsgBox "Can't connect to Visio"
       Exit Sub
    End If
End If
Dim wn As Object, chk As Boolean
chk = False
Dim td As Object ' target visio document
Dim tp As Object ' active page in target document
Set td = VisioApp.Documents.OpenEx("c:\Test\change shape issue.vsd", visOpenRW) ' define your own target doc
Set tp = VisioApp.ActiveWindow.Page
Dim ts As Object ' define stencil
Dim tm As Object 'define master-shape for drop
For Each wn In VisioApp.Windows ' iterate all open windows in current Visio session
If wn.document.Name = "BASIC_M.vssx" Then chk = true
Next
' define external stencil (vssx document) as variable named ts
If chk = false Then ' if no found opened stencil, open this stencil
Set ts = VisioApp.Documents.OpenEx("basic_m.vssx", visOpenRO + visOpenDocked)
Else
Set ts = VisioApp.Documents("basic_m.vssx") ' define opened stencil as variable named ts
End If
' define master shape from extertan stencil (ts)
Set tm = ts.Masters.ItemU("Circle") ' define your own desired master shape
tp.Drop tm, 0, 0 ' instead "0,0" you need  assign you real coordinates each time
' Do something else
End Sub

Dupes

Awsm
I worked like a charm, thanks  :)

is it also possible to resize the droped shape?

Surrogate

Replace this code
tp.Drop tm, 0, 0 ' instead "0,0" you need  assign you real coordinates each time
' Do something else

with that code
dim shp as object
set shp.= tp.Drop (tm, 0, 0) ' instead "0,0" you need  assign you real coordinates each time
shp.cells("Height") = 7 ' set height to 7 in
shp.cells("Width") = 40  ' set width to 40 in


Dupes


JM


So this is exactly what I have been working on (on and off) for a few years, and have a _lot_ of code to do this (with probably lots and lots of things that you don't necessarily care about).  I am happy to help round out/guide your effort depending on where you need help. 

The 'DynamicDrawings' tools I have been building is used to visualize system-to-system interactions (think Enterprise Architecture interface diagrams).  The general method I use is to connect to an Access database (from Visio, not the other way around) and open 3 queries:  1 for containers, 1 for shapes, and 1 for links (in that order).

Connecting to Access from Visio is pretty gross (in my view), because I have (at times) spent long hours trying to debug a problem only to find that the SQL I have written isn't kosher for access through ADO. 

The queries have some generic columns to help me define the visual attributes (such as 'shapetype' and 'theme'), and all the translation happens in the Access SQL queries.  e.g. a field like  'SYSTEM_STATUS' is used to determine the shapetype (the stencil to use).

The Shapes query would need to identify which container it belongs in.  Likewise, the Links query needs to identify the source/destination shapes that the line should connect to.

For my purposes, I have a lot of additional code to assign shapes to layers and a bunch of code to change how the layers are visualized (since turning layers on and off via Visio's standard methods don't work the way I want since shapes will be in more than one layer).  To make this all work, I dynamically add/modify the shapesheet data for the shapes I drop on the page.

I can provide some stripped down snippets if you need, just tell me which parts.


Dupes

Is it also possible to link data from table to visio? really?
Like can we also define attributes linked to access database?

Surrogate

#24
This video about build-in Visio Data linking.
Also you can create your custom solution, or use solutions from 3rd developers (like as JM in this thread)

JM

Quote from: Surrogate on April 12, 2018, 09:19:49 AM
This video about build-in Visio Data linking
Also you can create your custom solution, or use solutions from 3rd developers (like as JM in this thread)

So one other comment - I started down the road of trying to link data using Visio's built-in mechanisms.  I found there was too much 'special' behavior I wanted, that I ended up coding the data portion myself.  (e.g. whether or not I wanted all fields in all shapes, or just some fields in some shapes based off of certain values..)

I also found I wanted to control what shapes were created and I wanted to tweak the shapesheet cells dynamically.. thus I ended up at a custom solution..