Author Topic: Compiler error in MACRO Code for VISIO 2007  (Read 5035 times)

0 Members and 1 Guest are viewing this topic.

Daniel Rose

  • Newbie
  • *
  • Posts: 5
Compiler error in MACRO Code for VISIO 2007
« on: July 28, 2011, 10:52:27 AM »
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

Paul Herber

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2954
    • Paul Herber's website
Re: Compiler error in MACRO Code for VISIO 2007
« Reply #1 on: July 28, 2011, 11:03:34 AM »
A major problem is the the VB you have is a different language to the VBA required for macros.
Electronic and Electrical engineering, business and software stencils and applications for Visio -

https://www.paulherber.co.uk/

Jumpy

  • Hero Member
  • *****
  • Posts: 1061
Re: Compiler error in MACRO Code for VISIO 2007
« Reply #2 on: July 29, 2011, 01: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.

AndyW

  • Sr. Member
  • ****
  • Posts: 320
    • PC Mimic Diagram
Re: Compiler error in MACRO Code for VISIO 2007
« Reply #3 on: July 29, 2011, 01: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.
Live life with an open mind

Jumpy

  • Hero Member
  • *****
  • Posts: 1061
Re: Compiler error in MACRO Code for VISIO 2007
« Reply #4 on: July 29, 2011, 02:51:56 AM »
A "repaired" version for VBA (VB may be a little different as mentioned by Paul and Andy)

Code
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.
« Last Edit: July 29, 2011, 02:54:20 AM by Jumpy »

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3832
  • Ideas Visio-lized into solutions
Re: Compiler error in MACRO Code for VISIO 2007
« Reply #5 on: July 29, 2011, 09:16:48 AM »
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
Visio 2019 Pro

Daniel Rose

  • Newbie
  • *
  • Posts: 5
Re: Compiler error in MACRO Code for VISIO 2007
« Reply #6 on: August 09, 2011, 01: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.