0

Im playing around with VBA code from this microsoft help site that adds shapes to a visio document. However I can't get the code to work as shown on the website. Here is the website with the code: https://msdn.microsoft.com/en-us/library/cc160744.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

Here is what I have currently:

Imports Microsoft.Office.Interop
Module Module1
    Public Property Application As Object

    Sub Main()
        Dim names As New List(Of String)()
        Application.Documents.Add("")

        Dim visioDocs As Visio.Documents
        Dim visioStencil As Visio.Document = visioDocs.OpenEx("Basic Shapes.vss", CShort(Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visOpenDocked))

        Dim visioPage As Visio.Page = Application.ActivePage

        Dim visioRectMaster As Visio.Master = visioStencil.Masters("Rectangle")
        Dim visioRectShape As Visio.Shape = visioPage.Drop(visioRectMaster, 4.25, 5.5)
        visioRectShape.Text = "Rectangle text."

        Dim visioStarMaster As Visio.Master = visioStencil.Masters("Star 7")
        Dim visioStarShape As Visio.Shape = visioPage.Drop(visioStarMaster, 2.0, 5.5)
        visioStarShape.Text = "Star text."

        Dim visioHexagonMaster As Visio.Master = visioStencil.Masters("Hexagon")
        Dim visioHexagonShape As Visio.Shape = visioPage.Drop(visioHexagonMaster, 7.0, 5.5)
        visioHexagonShape.Text = "Hexagon text."
    End Sub

End Module

I get a null reference exception failed to handle at the end of the Application.Documents.Add("") line. I've tried many solutions via google but have not been able to get rid of the error. Any ideas on how to get this to run?

Mathieu Guindon
  • 65,145
  • 8
  • 95
  • 208
  • There doesnt appear to be an instance created for `Application`. `Object` is probably not the desired type for it since System.Object has no Documents collection property. – Ňɏssa Pøngjǣrdenlarp Jun 16 '16 at 19:22
  • Exactly. You are supposed to at least start Visio to make this code work. Something like Application = new Application() should do. – Nikolay Jun 17 '16 at 21:13

0 Answers0