You are on page 1of 18

Office Automation

Week 9
Lab Lecture
CS 101
Microsoft or Open Office
Microsoft Open Office
• Visual Studio • Programmable
– programming languages – in multiple languages,
• Office Suite including Python
– Office word processor • Suite
– Excel spreadsheet – Similar functionality
– PowerPoint presentation – Not as many bells and
– whistles
Access database
– Interoperable with MS files.
– Outlook, Visio, Publisher…
Four Levels of Use
• Macros
– For doing repetitive work
• Controls
– For adding menus, option buttons, …
• Editing Macros
– For loops, if-then conditions in macros
• Programming
– For doing anything you like
Macros
Macro Security
High is for
dummies.

Low is for
dumb people.
Record New Macro
• Give macro a
name and
optional
description.
• Assign to
Toolbar or
Keyboard.
• Store in
document only,
or for all
documents.
Record New Macro
• The "Recorder" has a Stop and
Pause button.
• Every Keystroke, Button Press,
Menu selection, and Mouse Click
is being recorded.
• To replay, you can use
– Alt-F8 (with the name of the macro)
– Keystroke
– Button Press
Controls in Excel
• View the Control
Toolbox (right click
on toolbars)
• Enter Design Mode
(can be confusing if
in wrong mode), to
put controls on
page.
• Exit Design Mode
by the same button.
Now Design
• Create a scrollbar
• View its properties
• Set
– LinkedCell
– Min, Max
– LargeChange
– SmallChange
– Value
– Delay
In Excel
• Most controls have LinkedCell, making
them very easy to use.
• Textbox, ToggleButton, OptionButton,
CheckBox, ListBox, ComboBox
• In other programs, you have to program to
get at the values of these controls.
Automagic Programming
• Record a macro and then enter VB Editor
(Alt F-11).
• You enter an editor similar to VB, with a
program already programmed, which you
can run, step through, breakpoint, etc.
• Even if you don't understand it exactly,
you can modify it by a bit of guesswork
• F1 for help (in the VB Editor) tells you
more about itself.
Objects
• Objects are things like cells, characters,
fonts, graphs, ranges of cells or sheets in
a spreadsheet or document.
• They are arranged like directories, but use
a "dot" rather than "\".
• "Application" is like the root directory.
– Application.ActiveWorkBook
– Application.ActiveWorkBook.ActiveSheet
Collections
• Application.WorkSheets is a collection.
We don't need to keep repeating Application. If we
don't say anything, it is understood, so
• WorkSheets is the same as
Application.WorkSheets.
• WorkSheets(1) is the first sheet.
• WorkSheets("Sheet1") is the same.
• ActiveSheet is the active sheet, which is the first
sheet of the workbook at the start.
Looping over Collections
Dim ws as WorkSheet
For Each ws in WorkSheets
MsgBox ws.Name
Next wx
Ranges and Cells
Both of the following do the same thing
MsgBox WorkSheets("Sheet1").Range("C1").Value
MsgBox WorkSheets(1).Cells(1,3).Value

• ActiveCell.Cells(3,3) refers to the cell that is 2 cells to the


right and two below the selected cell.

• Selection is the collection of cells that is currently selected.

For each c in Selection


c.Value = 5
Next c
Word
The following collections are defined for any range
(including ActiveDocument)
• ActiveDocument.Sections
• ActiveDocument.Paragraphs
• ActiveDocument.Words
• ActiveDocument.Characters
You can refer to ranges by character numbers, or
selection is also a range.
• Selection
• Range(20,50)
The With command
With Selection.Font This is the same as
.Name = "Times New Roman"
.Size = 12 Selection.Font.Name = "Times …"
.Bold = False Selection.Font.Size = 12
.Italic = False Selection.Font.Bold = False
.Underline = wdUnderlineNone
.StrikeThrough = False • Anything starting with a "." has the
.DoubleStrikeThrough = False With part attached to its start.
.Outline = False • You do not have to repeat the
.Emboss = False same stuff again and again.
.Shadow = False • Much like the "Current directory" in
a directory hierarchy.
.Hidden = False
.AllCaps = False
.Engrave = False
.End With
Try something.
For Each c in ActiveDocument.Characters
n = 1
c.Font.Size = n
n = n + 1
Next c

You might also like