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 27
Sub ReplaceEverywhere(FindText As String, ReplaceText as String)
Dim oSection As Section
Dim oShape As Shape
Dim oHF As HeaderFooter
ReplaceInRange FindText, ReplaceText, ActiveDocument.Content
For Each oShape In ActiveDocument.Shapes
If oShape.TextFrame.HasText Then
ReplaceInRange FindText, ReplaceText, oShape.TextFrame.TextRange
End If
Next oShape
For Each oSection In ActiveDocument.Sections
For Each oHF In oSection.Headers
ReplaceInRange FindText, ReplaceText, oHF.Range
For Each oShape In oHF.Shapes
If oShape.TextFrame.HasText Then
ReplaceInRange FindText, ReplaceText, _
oShape.TextFrame.TextRange
End If
Next oShape
Next oHF
For Each oHF In oSection.Footers
ReplaceInRange FindText, ReplaceText, oHF.Range
For Each oShape In oHF.Shapes
If oShape.TextFrame.HasText Then
ReplaceInRange FindText, ReplaceText, oShape.TextFrame.TextRange
End If
Next oShape
Next oHF
Next oSection
End Sub
Sub ReplaceInRange(FindText As String, ReplaceText as String, _
oRange as Range)
With oRange.Find
.Format = False
.Text = FindText
.Replacement.Text = ReplaceText
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With
End Sub
Here is the exact AppleScript equivalent:
on ReplaceEverywhere(findText, replaceText)
local theShape, theRange, theHeaderFooters, theHeaderFooter
tell application "Microsoft Word"
--first find and replace in the body (main story) of document
set theRange to text object of active document
my ReplaceInRange(findText, replaceText, theRange)
--now in all shapes
set allShapes to (every shape of active document)
repeat with theShape in allShapes
set theTextFrame to (text frame of theShape)
< Previous Page Next Page>
- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine