Adding text to stencil master shapes VBA for visio

Started by lachieW, August 03, 2022, 06:45:21 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

lachieW

Hi

Surrogate

Hi!
Quote from: lachieW on August 03, 2022, 06:45:21 AMNow the problem I have is that aslong as the shape has already been created and I assign the ID it works fine.
we cant change shape's ID

Yacine

Using hard coded ID numbers does not work.
You need to get a handle on the newly created shape.
Luckily the "drop" function returns just this.
dim shp as shape
set shp = ...drop...

then
Set vsoCharacters1 = shp.Characters
...
Yacine

lachieW

Dim shp As Shape
   

    Application.Windows.ItemEx("DIAGRAMS - Copy.vsd  [Compatibility Mode]").Activate
    set shp = Application.ActiveWindow.Page.Drop Application.Documents.Item("Network Diagrams_Sept302021 - Copy.vsd").Masters.ItemU("Router.18"), 3.213718, 8.592643

    Dim vsoCharacters1 As Visio.Characters
    Set vsoCharacters1 = Application.ActiveWindow.Page.Shapes.shp.Characters
    vsoCharacters1.Begin = 0
    vsoCharacters1.End = 0
    vsoCharacters1.Text = "10.10.10.10" & Chr(10) & ""

I am getting a syntax error on the set shp (2nd line). Am I missing something?

Surrogate

Quote from: lachieW on August 03, 2022, 11:11:56 PMI am getting a syntax error on the set shp (2nd line). Am I missing something?
Document Network Diagrams_Sept302021 - Copy.vsd" is open ?

Yacine

#5
there's a second error (additional to surrogate's answer)


shp stands for the previous "Application.ActiveWindow.Page.Shapes.ItemFromID(2)"

As analogy, the previous one would say: "I mean the the guy living in Germany, the town is Cologne, in the 5th city district, Street #7, House 3, Apartment 2, the first son in the Family".
The second definition says "It's Bob!"
Thus your formula becomes : Set vsoCharacters1 = shp.Characters (without the whole address)
Yacine

Yacine

#6
Just saw that you haven't added parenthesis to the argument of the drop function.
VBA has the weird behavior that sub calls are written without and function calls with parenthesis, regardless of the function itself.
So you write
    pageObject.Drop masterObj, x, y
or
    set shp = pageObject.Drop(masterObj, x, y)

PS:
I think the empty string after the Chr(10) is probably not needed.
vsoCharacters1.Text = "10.10.10.10  / 24" & Chr(10) & ""


PPS:
Just to add a general debugging method to the previous.
When you have a complex function as your previous one, try to identify, then split the terms in different lines. The debugger will stop the code in the right line, making it easier to you to identify the error.

    "set shp = Application.ActiveWindow.Page.Drop Application.Documents.Item("Network Diagrams_Sept302021 - Copy.vsd").Masters.ItemU("Router.18"), 3.213718, 8.592643"

In this formula there are a lot of dots defining either items of a collection or a function. How would you know which is which? Unfortunately you don't from reading the formula alone. You're entry point for understanding the object model is MS's page https://docs.microsoft.com/en-us/office/vba/api/overview/Visio/object-model .

In it you'll learn that "drop" is the function of interest and "Application.ActiveWindow.Page." translates to "ActivePage".

"Application.Documents.Item("Network Diagrams_Sept302021 - Copy.vsd").Masters.ItemU("Router.18")" Translates to one particular master in one particular stencil.

So the stencil is "Application.Documents.Item("Network Diagrams_Sept302021 - Copy.vsd")"
Its masters collection is "Masters"
and getting a particular master is .ItemU("Router.18")

You'll then spend some thoughts on where to split. Unfortunately I can't really formulate a rule other then "set a cut" and look if it helped.

"Desiccate" the formula only as far as needed for the debugging, you'll concatenate back once the error found and corrected.

Also try to generalize code as much as possible, keeping hard coded values at a place where you'll easily find them later. (Network Diagrams_Sept302021 - Copy.vsd, Router.18, 3.213718, 8.592643). Use variables.

Write the DIMs yourself, but here's what a somehow cleaner code could look like.

functionget_master(tStencil as string, tMaster as string) as master
  'check if master is open and get a handle on handle on it
  'check if the stencil has a master with name = tMaster
  'assign it
  get_master = oMaster

In your main code you would then write:
  tStencil = "Network Diagrams_Sept302021 - Copy.vsd"
  tMaster = "Router.18"
  x = ...
  y = ... 'What ever you chose to determine x and y

set shp = activepage.drop(get_master(tStencil, tMaster, x, y)

set chars = shp.Characters

... and so on.

HTH



Yacine