Visio Guy

Visio Discussions => Programming & Code => Topic started by: Daniel Rose on July 28, 2011, 03:52:27 PM

Title: Compiler error in MACRO Code for VISIO 2007
Post by: Daniel Rose on July 28, 2011, 03:52:27 PM
To all,

I am using VISIO 2007.  I copied VB code from the article called "Fit all Pages to Window in a VISIO Document".  When I compiled it, I received the error dialog box with the message "Compiler Error:  Syntax Error".

The statement that was highlighted was:

Private Function m_uiGetActiveDrawingWindow() As Visio.Window

I am hoping that somebody might be able to help this NEWBIE!


Here is the code that I copied:

Option Explicit
Const MESSAGE_CAPTION$ = "Fit All Pages"

Public Sub SetAllPagesToFit()

  '// Get the active window, if it is a drawing window:
  Dim win As Visio.Window
  Set win = m_uiGetActiveDrawingWindow()
  If (win Is Nothing) Then Exit Sub

  Dim doc As Visio.Document
  Dim pg As Visio.Page, pgOrig As Visio.Page

  Set pgOrig = win.Page
  Set doc = pgOrig.Document

  '// Fit each window. -1 is the zoom value for
  '// fit-to-page:
  For Each pg In doc.Pages
   win.Page = pg
   win.Zoom = -1
Next

  '// Return the window to the original page:
  win.Page = pgOrig

End Sub

Private Function m_uiGetActiveDrawingWindow() As Visio.Window

  '// Checks the active window to see if it is
  '// a drawing window. If not, an error is presented
  '// to the user.
  Set m_uiGetActiveDrawingWindow = Nothing
  Dim win As Visio.Window
  If (Visio.ActiveWindow Is Nothing) Then

    Call MsgBox("This code requires an active drawing " & _
                "window to function properly!", , _
                 MESSAGE_CAPTION$)
     Exit Function

  End If

  Set win = Visio.ActiveWindow
  If (win.Type <> Visio.VisWinTypes.visDrawing) Then

    Call MsgBox("The active window is not a drawing " & _
                "window. Please make sure the active " & _
                "window is a drawing window!", _<br /> , MESSAGE_CAPTION$)

    Exit Function

  End If

  Set m_uiGetActiveDrawingWindow = win

End Function
Title: Re: Compiler error in MACRO Code for VISIO 2007
Post by: Paul Herber on July 28, 2011, 04:03:34 PM
A major problem is the the VB you have is a different language to the VBA required for macros.
Title: Re: Compiler error in MACRO Code for VISIO 2007
Post by: Jumpy on July 29, 2011, 06:43:06 AM
Another issue may be, that the code was posted on a webside and got somehow corrupted:
For example, I don't think that "_<br />" or "&amp" are VB or VBA.
Title: Re: Compiler error in MACRO Code for VISIO 2007
Post by: AndyW on July 29, 2011, 06:59:45 AM
The only issue is the html fragments that you have copied in the code. So thats the <br />, &amp, &lt, &gt. And as Paul pointed out about language differences, VBA is a subset of VB, so whilst very similar there can be a few minor differences.
Title: Re: Compiler error in MACRO Code for VISIO 2007
Post by: Jumpy on July 29, 2011, 07:51:56 AM
A "repaired" version for VBA (VB may be a little different as mentioned by Paul and Andy)


Option Explicit
Const MESSAGE_CAPTION$ = "Fit All Pages"

Public Sub SetAllPagesToFit()

  '// Get the active window, if it is a drawing window:
  Dim win As Visio.Window
  Set win = m_uiGetActiveDrawingWindow()
  If (win Is Nothing) Then Exit Sub

  Dim doc As Visio.Document
  Dim pg As Visio.Page, pgOrig As Visio.Page

  Set pgOrig = win.Page
  Set doc = pgOrig.Document

  '// Fit each window. -1 is the zoom value for
  '// fit-to-page:
  For Each pg In doc.Pages
   win.Page = pg
   win.Zoom = -1
Next

  '// Return the window to the original page:
  win.Page = pgOrig
End Sub


Private Function m_uiGetActiveDrawingWindow() As Visio.Window
  '// Checks the active window to see if it is
  '// a drawing window. If not, an error is presented
  '// to the user.
  Set m_uiGetActiveDrawingWindow = Nothing
  Dim win As Visio.Window
  If (Visio.ActiveWindow Is Nothing) Then

    Call MsgBox("This code requires an active drawing " & _
                "window to function properly!", , _
                 MESSAGE_CAPTION$)
     Exit Function

  End If

  Set win = Visio.ActiveWindow
  If (win.Type <> Visio.VisWinTypes.visDrawing) Then

    Call MsgBox("The active window is not a drawing " & _
                "window. Please make sure the active " & _
                "window is a drawing window!", , MESSAGE_CAPTION$)

    Exit Function

  End If

  Set m_uiGetActiveDrawingWindow = win

End Function


Hope I didn't forget sth.
Title: Re: Compiler error in MACRO Code for VISIO 2007
Post by: wapperdude on July 29, 2011, 02:16:48 PM
If I were a betting man, I'd say this article is the source of the code, Fit All Pages to Window in a Visio Document, http://www.visguy.com/2011/04/13/fit-all-pages-to-window-in-a-visio-document/

It is VBA code -- according to the article.   :)

Wapperdude
Title: Re: Compiler error in MACRO Code for VISIO 2007
Post by: Daniel Rose on August 09, 2011, 06:21:49 PM
You are correct.  That is exactly where I got the code.  I since was able to fix the code.  The reference to MSGBOX was wrong.  So, it was highlight the previous statement of course.