marking rubric table with vba working under Word 2004 (works in Wi

P

Peter

I wishfully underestimated the issues involved in getting a VBA applications
from the windows world to work under Mac Word 2004.

The following link is to an automated marking rubric. It is a table with
each row being a performance standard and the associated mark under a certain
criteria. The final column is used to store the mark the person is awarded.

The Windows interface is that the person clicks a button to show a floating
toolbar then clicks in a row for the assessed standard then clicks "Select
Standard" on the floating toolbar. This then shades the row and moves the
mark from the standard (in the first column) to the last column. There are
other buttons to "Unselect Standard" and also add and rescale the marks.

The document is stored at
http://www.usq.edu.au/users/evansp/browse/eMarking_Assistant/Marking_Rubric_unlocked.doc

The code is also shown below.

My questions are:
* how do I show the floating toolbar without usign a button in the document
* the floating toolbar should be modeless and how can I show it in this way
if .show vbmodeless throws an error on the Mac
* is there a clear description of the differences between Mac VBA and
WIndows VBA for Word.


Option Explicit


Public Sub ShowMarkingRubricToolbar()
'show the marking rubric toolbar
Dim UFrm As MarkingRubric
Set UFrm = New MarkingRubric
With UFrm
.Show
End With
End Sub

Sub addRescale()
On Error GoTo errorHandler
Selection.Tables(1).Select
'need to update the fields twice as easlier fields are based on values
in later fields
Selection.Fields.Update
Selection.Fields.Update
Exit Sub

errorHandler:
Debug.Print "Error # " & Str(Err.Number) & Err.Description
If Err.Number = 5941 Then
MsgBox "You must place your cursor in the marking rubric table you
want to update.", vbOKOnly
Else
MsgBox "Error # " & Str(Err.Number) & " " & Err.Description & "
Please send details to (e-mail address removed)", vbOKOnly
End If
End Sub

Sub selectStandard()
On Error GoTo errorHandler
Dim theresult As String
Dim theStandard As String
Dim theMark As String
Selection.HomeKey Unit:=wdRow
Selection.EndKey Unit:=wdRow, Extend:=True
theStandard = Selection.Text
theMark = Left(theStandard, (InStr(theStandard, " ") - 1))
If Not IsNumeric(theMark) Then
theresult = MsgBox("This standard does not have a mark as the first
word. Do you want to continue", vbYesNo)
If theresult = vbNo Then
Exit Sub
End If
End If
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = wdColorLightGreen
If IsNumeric(theMark) Then
Selection.EndKey Unit:=wdRow
Selection.MoveLeft Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=theMark
End If

Exit Sub
errorHandler:
Debug.Print "Error # " & Str(Err.Number) & Err.Description
If Err.Number = 4605 Then
MsgBox "You must place your cursor in the row of the standard you
want to select.", vbOKOnly
Else
MsgBox "Error # " & Str(Err.Number) & " " & Err.Description & "
Please send details to (e-mail address removed)", vbOKOnly
End If
End Sub

Sub unselectStandard()
On Error GoTo errorHandler
Dim theStandard As String
Dim theMark As String
Selection.HomeKey Unit:=wdRow
Selection.EndKey Unit:=wdRow, Extend:=True
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = wdColorWhite
Selection.EndKey Unit:=wdRow
Selection.MoveLeft Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=0

Exit Sub
errorHandler:
Debug.Print "Error # " & Str(Err.Number) & Err.Description
If Err.Number = 4605 Then
MsgBox "You must place your cursor in the row of the standard you
want to unselect.", vbOKOnly
Else
MsgBox "Error # " & Str(Err.Number) & " " & Err.Description & "
Please send details to (e-mail address removed)", vbOKOnly
End If
End Sub
 
P

Peter

After a little more investigation I discovered how I could use function keys
to initiate the macros e.g.

Application.CustomizationContext = ThisDocument
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyF5),
KeyCategory:=wdKeyCategoryCommand, Command:="SelectStandard"

This means that if you are on mac you can use the function keys and if you
are on windows you can use either the floating toolbar or function keys.

The completed automated marking rubric application is downloadable from
http://www.baker-evans.com/community/mod/forum/discuss.php?d=179

Feel free to distribute to anyone who might be interested,

PeterEvans
 
J

John McGhie

Hi Peter:
My questions are:
* how do I show the floating toolbar without usign a button in the document

The problem is that the "toolbar" is NOT a toolbar, it's a "UserForm". If
it WERE a toolbar, you could Show/Hide it happily on either the Mac or the
PC. A simple macro would show or hide it automatically.

Below is my standard "Toolbars" macro. Note that it is called by the "Auto"
events Execute, Open, Close, and New. Note that it also relies on three
string variables being defined and set to the names of the toolbars you
want, in a Public Constants module.

* the floating toolbar should be modeless and how can I show it in this way
if .show vbmodeless throws an error on the Mac

You can't. VBModeless is not supported in Mac VBA. Use a toolbar :)
* is there a clear description of the differences between Mac VBA and
WIndows VBA for Word.

Yes: "Some PC VBA will work on the Mac, and some will not. To find out
which is which, compile and run the code on the target platform."

Sorry to be facetious, but that's as clear as it gets. Fourth-generation
languages such as VBA always "promise" that you can "Write once, run
anywhere." Sadly, industry experience is "Write once, TEST EVERYWHERE."

Generally, Word 2000 code will run in Word 2004. Anything higher MAY
exhibit problems. Not even Microsoft can tell you for sure, because many of
the errors will appear only at runtime, and the number of possible
combinations approaches infinity.

While you are at it, Set a variable to that Table by name, then you can
address the rows, columns, and cells by number. Much more robust than
moving the selection and praying nobody changed the table on you or stuck
another table into the document :)

Public Sub SetToolbars()
' Brings up Customised toolbars
' Copyright 19 May 2003 by John McGhie
' McGhie Information Engineering Pty Ltd

' This macro tests for the presence of customised toolbars available
' to the active document, and displays them if present.
' Otherwise, it displays the personal customised toolbars.

' Constants provide a simple way of changing toolbar names in a single place
' The following four strings are all you need to change
' when moving from customer site to customer site

Dim aToolbar As CommandBar
Dim tbStandard As CommandBar
Dim tbFormatting As CommandBar
Dim tbCustStd As CommandBar
Dim tbCustFormatting As CommandBar
Dim ToolRowIndex As Integer
Dim MessageString As String


If Application.Documents.Count > 0 Then
' The user can leave us in a state where there are no active documents. If
' they do, we must not try to manipulate toolbars, otherwise we get an
error.

#If Mac = False Then
Application.EmailOptions.EmbedSmartTag = False ' Gets rid of smart tags when
the registry is locked
With ActiveDocument
If .EmbedSmartTags = True Then .EmbedSmartTags = False
If .EmbedLinguisticData = True Then .EmbedLinguisticData = False
End With
#End If

Set tbStandard = Application.CommandBars(DefaultStandard)
If tbStandard.Enabled = False Then tbStandard.Enabled = True

Set tbFormatting = Application.CommandBars(DefaultFormatting)
If tbFormatting.Enabled = False Then tbFormatting.Enabled = True

For Each aToolbar In Application.CommandBars
' First, enumerate the CommandBars collection to make sure the
' customer toolbars are available. If they're not, the code
' would emit a runtime error we want to trap and handle.

With aToolbar
' If the toolbar is already visible, it will not be enabled
' If it is not enabled, the code throws an undefined error in Wd97
If .Enabled = True And .Protection = msoBarNoProtection Then
Select Case .Name
Case CustomerStandard
Set tbStandard = aToolbar
Case CustomerFormatting
Set tbFormatting = aToolbar
Case "Reviewing"
.Visible = False
.Enabled = True
Case "Web"
.Visible = False
.Enabled = False
Case Else
aToolbar.Visible = False
End Select
End If
End With
Next aToolbar

' Now we display whatever we found

With tbStandard
.Visible = True
.RowIndex = 2 ' Docking order (Doesn¹t work on the Mac)
.Left = 0
End With

With tbFormatting
.Visible = True
.RowIndex = 3 ' Docking order
.Left = 0
End With
End If

GoTo alldone ' Exit the macro without running the error code

ErrorNotFound: ' Error handler
MessageString = "One or more of the customised toolbars are missing. " _
& vbLf & vbLf & "Ask Help Desk to restore them, or disable the macro. "
' Be kind, tell the user which ones are missing :)
If tbStandard = "" Then MessageString = MessageString & vbLf & vbLf &
DefaultStandard & " is missing."
If tbFormatting = "" Then MessageString = MessageString & vbLf & vbLf &
DefaultFormatting & " is missing."
MsgBox MessageString, vbExclamation + vbOKOnly, "Toolbar Macro"

alldone:

End Sub


--
Don't wait for your answer, click here: http://www.word.mvps.org/

Please reply in the group. Please do NOT email me unless I ask you to.

John McGhie, Microsoft MVP, Word and Word:Mac
Sydney, Australia. mailto:[email protected]
 
P

Peter

Thanks for your input.

The reason I used a userForm rather than a toolnbar is so it works exacly
the same in Word 2003 and 2007 on Windows.

Yes, "author once and use anywhere" is stretched a little beyond twanging
point.

Win users can use the floating userform or function keys and Mac users can
use the function keys. The main goal is to prevent the Userform form from
generating the "Can't exit Design mode..." error even though it is not called
on the Mac.

The latest version is at:
http://www.baker-evans.com/community/mod/forum/discuss.php?d=179

Peter
 
J

John McGhie

Hi Peter:

Not much use sending me to the latest version if you've locked the code :)

All I can confirm is that "Yes, it does, indeed, produce an error message"
:)

Cheers


Can't exit Design mode..

--
Don't wait for your answer, click here: http://www.word.mvps.org/

Please reply in the group. Please do NOT email me unless I ask you to.

John McGhie, Microsoft MVP, Word and Word:Mac
Sydney, Australia. mailto:[email protected]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top