Moving from Microsoft Office VBA to AppleScript:
MacTech's Guide to Making the Transition
Introduction
|
Table of Contents
Page Prev and Page Next buttons at bottom of the page.
|
April, 2007
Page 48
Worksheets can be renamed by setting their name property:
ActiveSheet.Name = "New Sheet Name"
In AppleScript:
set name of active sheet to "New Sheet Name"
This can cause an error if the sheet name is invalid, or a sheet already has that name. One workaround in VBA might look like this:
Const sNAME = "New Sheet Name"
On Error Resume Next
ActiveSheet.Name = sNAME
If ActiveSheet.Name <> sNAME Then MsgBox "Re-name failed"
On Error GoTo 0
In AppleScript, this will do it:
tell application "Microsoft Excel"
try
set name of active sheet to "New Sheet Name"
on error
display dialog "Re-name failed." buttons {"Cancel"} with icon 0
end try
end tell
In this case, it does not fail silently, but creates a true error you can trap.
Rename a worksheet with a cell value
Set a worksheet name to the value of a cell:
On Error Resume Next
With ActiveSheet
.Name = .Range("A1").Text
End With
On Error GoTo 0
In AppleScript:
try
set name of active sheet to string value of range "A1"
end try
Headers and footers are set in VBA using the PageSetup object of a worksheet:
ActiveSheet.PageSetup.LeftHeader = Format(Date, “dd mmmm yyyy”)
You can also use the CenterHeader, RightHeader, LeftFooter, CenterFooter, and RightFooter properties. Those translate unsurprisingly to AppleScript as center header, right header, left footer and right footer of the page setup object property of worksheet. (Note "object" at the end of the property name, but not so in the class name page setup to which it refers: more on that later.) To set multiple headers and footers:
Public Sub PathAndFileNameInFooter()
Dim wsSht As Worksheet
For Each wsSht In ActiveWindow.SelectedSheets
wsSht.PageSetup.LeftFooter = ActiveWorkbook.FullName
Next wsSht
End Sub
In AppleScript:
repeat with wkSht in (get selected sheets of active window)
set left footer of page setup object of wkSht to ¬
< Previous Page Next Page>
- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine