mms6f
May 9, 2006, 6:27pm
#1
I have a Word macro that is run from applescript by the following code:
tell application "Microsoft Word"
do Visual Basic "TestMacro"
end tell
.and that works fine.
However, this database will be accesssed by 3 or 4 users, and I would rather not put the macros on individual computers. If I have to change any of the macros, I would have to keep track and change it on all the computers, so I would rather save the hassle by having the macro in one central place in one place (i.e. the applescript).
Is there a way to enter the text of the macro into applescript? I tried but it told me there was a syntax error (it doesn’t seem to recognize ¶).
Any help would be greatly appreciated.
Thanks!
Martha
Would you post the Visual Basic code? Offhand, I’d guess some characters need to be escaped.
FWIW, this works:
display dialog "¶"
mms6f
May 10, 2006, 12:05pm
#3
Sure! Here it is:
Selection.WholeStory
Selection.Copy
Documents.Add DocumentType:=wdNewBlankDocument
Selection.PasteAndFormat (wdPasteDefault)
Selection.Find.ClearFormatting
With Selection.Find
.Text = "apply:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
Selection.TypeBackspace
Selection.WholeStory
Selection.Range.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph
Selection.Copy
'Close temporary document
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
Thanks for any help, in advance!
Martha
mms6f
May 10, 2006, 1:51pm
#4
This is what I found on another website:
It’s because you’re compiling your code in Script Editor 2.0 which uses Unix
LF line-endings. Since the VB code has to be presented as a string for
AppleScript (‘do Visual Basic’ command takes a string) those LFs have become
coded into the script in the literal string. Word, on the other hand,
expects CR line endings both within text and between lines of VBA code.
(Word differs from Windows text files.) You can see it’s complaining about
all those ‘vbLf’ characters - that’s a constant for LF character (i.e.
Chr(10)). It wants CRs instead. So what you need to do is use the ‘return’
AppleScript constant to replace the LFs with CRs. Instead of saying ‘as
string’ (it already is a string), try this:
set xVBcode2 to "
–VB Code goes here… making sure all quotation marks have a slash before them → "
"
set xVBcode2 to my ConvertForVB(xVBcode2)
tell application “Microsoft Word”
– no need to activate
do Visual Basic xVBcode2
end tell
on ConvertForVB(vbText)
set theLines to paragraphs of vbText
set AppleScript’s text item delimiters to {return}
set vbText to theLines as string
set AppleScript’s text item delimiters to {“”}
return vbText
end ConvertForVB(vbText)
Use that handler every time you need to use ‘do Visual Basic’. It’s easier
to write and to read than putting ’ & return & ’ in run-on text without line
endings.
—It seems to be working quite nicely. I hope this helps someone else as well!
Thanks.
Martha