Convert VBA macro to Applescript

N

Numulguy

Version: 2008 I don't know if this is even possible. I have an existing VBA macro I use on Word for Windows and I need to get the equivalent functionality running on Word 2008 for Mac. This code needs to show/hide logos in the headers and will be used when the author decides to print onto letterhead or not.

The basic task breakdown is:
1. Toggle the value of an existing boolean custom document property
2. Search all graphic objects in the headers and if they have a specific text value, make the visibility of the graphic match the custom document propery.

The VBA code which does this is as follows.
Sub ToggleHeaderImages()
  'Macro by Andrew Lockton
  'Toggles the visibility of the logo image in the header
  Dim aShape As Shape, bShow As Boolean, sPropName As String
  sPropName = "ShowLogos"
  If Not funCustPropExists(sPropName) Then
    ActiveDocument.CustomDocumentProperties.Add Name:=sPropName, _
        LinkToContent:=False, Value:=True, Type:=msoPropertyTypeBoolean
  End If
  bShow = Not ActiveDocument.CustomDocumentProperties(sPropName)
  'All shapes in headers are stored in the first section irrespective of where they are viewed
  For Each aShape In ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes
    If aShape.AlternativeText = "HeaderImage" Then aShape.Visible = bShow
  Next aShape
  'reset the value of the custom property
  ActiveDocument.CustomDocumentProperties(sPropName) = bShow
End Sub
Function funCustPropExists(aProp As String) As Boolean
  Dim aCustProp As Variant
  funCustPropExists = False
  For Each aCustProp In ActiveDocument.CustomDocumentProperties
    If aCustProp.Name = aProp Then funCustPropExists = True
  Next aCustProp
End Function

Is this something that Applescript is capable of? Can anyone point me in the right direction to create this script?
 
P

Peter Jamieson

I have done very little Applescript, almost all of it with Word, but try
to avoid it because
a. it really is very difficult to work with and
b. it is particularly difficult to work out whether any given thing is
possible or not - for example, to add a new custom document property, at
some point I would hope to be able to do

make new custom document property with properties

but Applescript tells me "Microsoft Word got an error: Can’t make class
custom document property". Is that because it /is/ impossible to create
a new custom document property in Applescript, or is it because I simply
have not worked out exactly how to tell Applescript how to do it? At the
moment I cannot find a single example on the web that shows how to do
it, which leaves me with the strong impression that it cannot be done.
With any luck, now I've said that, someone will pop up and tell us how
to do it :)

Without that, part of your existing design probably isn't going to be
feasible. As for the Shapes and updates, sorry, can't tell you that
either off the top of my head, but I already know that dealing with
headers and footers in Applescript is not much like the same as doing it
in VBA and that some things do not appear to work at all. If you want to
pursue that part a bit further, we can but try, and perhaps someone with
more Applescript experience will chip in.

That said, I have found the following 4 things either essential or
extremely useful for working with Applescript and Word:
a. a good applescript book. I use the Hanaan Rosenthal book by Apress.
b. Microsoft's developer information at
http://www.microsoft.com/mac/developers/default.mspx
c. a transition guide at
http://www.mactech.com/vba-transition-guide/index-001.html
d. Apple's web-based Applescript materials


Peter Jamieson

http://tips.pjmsn.me.uk
 
J

John McGhie

Wait for Office 2011 at the end of this year: it will have a full
implementation of VBA back.

Cheers


Version: 2008 I don't know if this is even possible. I have an existing VBA
macro I use on Word for Windows and I need to get the equivalent functionality
running on Word 2008 for Mac. This code needs to show/hide logos in the
headers and will be used when the author decides to print onto letterhead or
not.

The basic task breakdown is:
1. Toggle the value of an existing boolean custom document property
2. Search all graphic objects in the headers and if they have a specific text
value, make the visibility of the graphic match the custom document propery.

The VBA code which does this is as follows.
Sub ToggleHeaderImages()
'Macro by Andrew Lockton
'Toggles the visibility of the logo image in the header
Dim aShape As Shape, bShow As Boolean, sPropName As String
sPropName = "ShowLogos"
If Not funCustPropExists(sPropName) Then
ActiveDocument.CustomDocumentProperties.Add Name:=sPropName, _
LinkToContent:=False, Value:=True, Type:=msoPropertyTypeBoolean
End If
bShow = Not ActiveDocument.CustomDocumentProperties(sPropName)
'All shapes in headers are stored in the first section irrespective of where
they are viewed
For Each aShape In
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes
If aShape.AlternativeText = "HeaderImage" Then aShape.Visible = bShow
Next aShape
'reset the value of the custom property
ActiveDocument.CustomDocumentProperties(sPropName) = bShow
End Sub
Function funCustPropExists(aProp As String) As Boolean
Dim aCustProp As Variant
funCustPropExists = False
For Each aCustProp In ActiveDocument.CustomDocumentProperties
If aCustProp.Name = aProp Then funCustPropExists = True
Next aCustProp
End Function

Is this something that Applescript is capable of? Can anyone point me in the
right direction to create this script?

This email is my business email -- Please do not email me about forum
matters unless you intend to pay!
 
N

Numulguy

Thanks for the feedback on this request, Peter and John.

Its a shame that Word 2008 doesn't have the macro capability of the earlier versions but I guess that MS have realised that by now. And the good news is that I don't need to learn applescript...

Thanks
Andrew
 
P

Peter Jamieson

BTW, I originally meant to be more careful not to give the impression
that there's anything wrong with Applescript in itself.

The problem is primarily in the difference in approach between
applescript and VBA and the resulting differences between the
applescript dictionary for Word and the object model that VBA works with.

Other than that, applescript is certainly "different" but learning it is
like learning any other language, and I have found that even a small
amount of time spent learning its basics has paid dividends.

Peter Jamieson

http://tips.pjmsn.me.uk
 
J

John McGhie

Oh, Microsoft knew the impact of the VBA loss very well, long before they
made the decision to remove VBA from Office 2008.

This was one of those "Faster, Cheaper, Better, Sooner: pick any three!"
decisions. Every software project is. For any software project, you choose
how much of each of those four things you can bake into the recipe.

With any large and complex piece of software, the software company does not
have complete control over any of those four things. Generally, the
customers will tell you what the maximum price can be, Sales will tell you
how much "Better" they need, and Marketing will tell you what date "Sooner"
is. "Faster" can always be had at the expense of one of the others: in this
case, a performance improvement of about 50% was possible if they removed
support for the Power PC.

I was among the customers Microsoft discussed the impact of this decision
with before they made it. They knew, very clearly, what the impact would
be. They made the best decision they could under the circumstances.

Office 2008 has been the best-selling version of Mac Office ever, so I guess
the people have voted and the result shows that Microsoft got the decision
right.

Personally, I derive a large part of my income from VBA customisation in
Word, so you can imagine the impact this had on my business. But the other
alternatives were even more unpalatable.

Cheers

Thanks for the feedback on this request, Peter and John.

Its a shame that Word 2008 doesn't have the macro capability of the earlier
versions but I guess that MS have realised that by now. And the good news is
that I don't need to learn applescript...

Thanks
Andrew

This email is my business email -- Please do not email me about forum
matters unless you intend to pay!
 

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