VBA visio question!

Started by woodman, September 16, 2010, 04:14:47 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

woodman

I am trying to insert a tabel to visio by VBA, Who can check the following VBA code:
Sub ChgText()

        For ii = 0 To 6
            Set vsoLine = ActivePage.DrawLine(27 / 25.4, (17 + ii * 5) / 25.4, 457 / 25.4, (17 + ii * 5) / 25.4)
           
        Next ii
       
        For jj = 0 To 12
            Set vsoLine = ActivePage.DrawLine((97 + jj * 30) / 25.4, 47 / 25.4, (97 + jj * 30) / 25.4, 12 / 25.4)
        Next jj
       
        Application.ActiveWindow.Page.Drop Application.Documents.Item("PEANNT_M.VSS").Masters.ItemU("8pt. text"), 12 / 25.4, 5 / 25.4
       
        Set vsoTxt = ActivePage.Shapes.ItemU("8pt. text")
        Set vsoTxt1 = ActivePage.Drop(vsoTxt, 43 / 25.4, 42.5 / 25.4)
        vsoTxt.Text = "No."
         
        Set vsoTxt1 = ActivePage.Drop(vsoTxt, 43 / 25.4, 37.5 / 25.4)
        vsoTxt.Text = "Name"
       
        Set vsoTxt1 = ActivePage.Drop(vsoTxt, 43 / 25.4, 32.5 / 25.4)
        vsoTxt.Text = "Class"
         
End Sub

aledlund

A couple of thoughts
a.) always turn on option explicit so that vba will check to make sure everything needed is defined
b.) always add onerror code to tell you when it is broke
c.) consider using the new visio sdk which now includes vba examples
d.) bracket your code samples when posting

hth,
al





Option Explicit


Sub ChgText()

        On Error GoTo ErrHandler
       
        Dim ii As Integer
        Dim jj As Integer
        Dim vsoLine As Visio.Shape
        Dim vsoTxt As Visio.Master
        Dim vsoTxt1 As Visio.Shape
       
        Dim strStencil As String
        strStencil = "PEANNT_M.VSS"
        Dim strShape As String
        strShape = "8pt. Text"
       
        Dim vsoApp As Visio.Application
        Set vsoApp = Application
        Dim vsoStencil As Visio.Document
       
       
        For ii = 0 To 6
            Set vsoLine = vsoApp.ActivePage.DrawLine(27 / 25.4, (17 + ii * 5) / 25.4, 457 / 25.4, (17 + ii * 5) / 25.4)
        Next ii
       
        For jj = 0 To 12
            Set vsoLine = vsoApp.ActivePage.DrawLine((97 + jj * 30) / 25.4, 47 / 25.4, (97 + jj * 30) / 25.4, 12 / 25.4)
        Next jj
       
        ' open the stencil
        Set vsoStencil = vsoApp.Documents.OpenEx( _
            strStencil, visOpenDocked)

        ' Get the master on the stencil by using its name.
        Set vsoTxt = vsoStencil.Masters.Item( _
            strShape)

        Set vsoTxt1 = vsoApp.ActivePage.Drop(vsoTxt, 43 / 25.4, 42.5 / 25.4)
        vsoTxt1.Text = "No."
         
        Set vsoTxt1 = ActivePage.Drop(vsoTxt, 43 / 25.4, 37.5 / 25.4)
        vsoTxt1.Text = "Name"
       
        Set vsoTxt1 = ActivePage.Drop(vsoTxt, 43 / 25.4, 32.5 / 25.4)
        vsoTxt1.Text = "Class"
        Exit Sub

ErrHandler:
        MsgBox Err.Description
         
End Sub




woodman

#2
Thanks a lot!

When i run the code, It's a error shows as '-2032465660 (86db0904)', 'Object name not found' on line 'Set vsoTxt = vsoStencil.Masters.Item(strShape)'.

The Version of my Office Visio is Professional 2003(11.3216.8172)


aledlund

It's probably complaining about the 'shape' that you asked for (8pt text). Since the code does run on my system,  I would first check to see that the stencil you asked for (peannt_m.vss) is on your system and can be opened. It should have opened and docked with your drawing when the macro ran. To test this, while your drawing with the macro is open, open the stencil manually and look to see that the master shape is in it. Drag a copy of the shape onto the drawing and then re-run the macro.
al

woodman

#4
It's OK now.

I have changed the line [Set vsoTxt = vsoStencil.Masters.Item(strShape)] to Set vsoTxt = vsoStencil.Masters.ItemU(strShape).

Thank you very much!