VBA library documentation that will get for the final exam (keep in
mind that not all library functions may have been covered during a
particular semester)
|
- Accessing the following VBA objects:
|
| |
- Application:
the MS-Office program running
- ActiveDocument:
current MS-Word document
- Selection:
currently selected text
|
- Useful methods and attributes of the
Application object
|
| |
- Application.Windows.Count:
number of open Word windows
- Application.CapsLock:
a Boolean to indicate whether caps lock is turned on or off
|
- Useful methods and attributes of the
ActiveDocument object (complex uses of the object are
included - out of order - after this list0
|
| |
- ActiveDocument.ActiveWindow.Caption = "<Caption>":
change caption title in the Word window
- ActiveDocument.Application:
the application associated with the document
- ActiveDocument.Checkspelling:
run spell checker
- ActiveDocument.Close (<wdPromptToSaveChanges>
or <wdDoNotSaveChanges> or <wdSaveChanges>):
close current Word window and apply the effect of specified constant
-
ActiveDocument.Comments.Count:
the number of comments inserted into the document
- ActiveDocument.CountNumberedItems:
total number of bulleted and numbered items
- ActiveDocument.DeleteAllComments:
delete all comments from the active document
- ActiveDocument.FullName:
name & save path of the active document
- ActiveDocument.HasPassword:
a Boolean to indicate if the active document is password protected
- ActiveDocument.InlineShapes.Count:
number of inserted inline shapes in the document
- ActiveDocument.InlineShapes(i).height :
height of inline shape # 'i'
- ActiveDocument.InlineShapes(i).width :
width of inline shape # 'i'
- ActiveDocument.InlineShapes.count :
number of inline shapes in the active document
- ActiveDocument.Name:
name of the active document
- ActiveDocument.password = "<Password>"
: sets the password for the
active document to the value specified
- ActiveDocument.Path:
save path of the active document
- ActiveDocument.Printout:
prints the active document
- ActiveDocument.Save:
saves the current document
- ActiveDocument.Saved:
a Boolean to indicate if the active document has been saved since
last changed
- ActiveDocument.SaveAs2("<filename>"):
save and rename document
- ActiveDocument.Select:
select all the text in the active Word document
- ActiveDocument.SendMail:
sends email with the active document as an attachment
- ActiveDocument.Shapes.Count: number
of primitive shapes in the document
- ActiveDocument.Shapes(i).Delete :
delete shape # 'i'
- ActiveDocument.Shapes(i).height :
height of shape # 'i'
- ActiveDocument.Shapes(i).width :
width of shape # 'i'
- ActiveDocument.Shapes.count :
number of shapes in the active document
- ActiveDocument.Shapes(i).Fill.ForeColor =
e.g. vbRed, vbBlue, vbGreen, vbYellow, vbBlack, vbWhite, vbBlack
:
sets Shape # 'i' to the specified
color
- ActiveDocument.SpellingChecked:
a Boolean to indicate if the active document has been spell checked
since last changed
- ActiveDocument.SpellingErrors.Count:
the number of spelling mistakes in the active document
- ActiveDocument.Tables(i)
- Allows access to one table from the collection of tables.
- 'i'
is a positive integer (i.e. >= 1)
- ActiveDocument.Tables.Count: number
of tables in the active document
- ActiveDocument.Tables(i).Sort(<Boolean>):
'i'
is a positive integer, sorts the table specified by 'i'
in the active document,
the Boolean specifies if it is true that the table’s first row is
header or title row which is not sorted
- ActiveDocument.Words.Count:
number of words in the MS-Word document
- ActiveDocument.Words.Comments.Count:
number of annotation comments in the MS-Word document
|
- ActiveDocument:
find and replace text
ActiveDocument.Content.Find.Execute FindText:="<text>",
ReplaceWith:="<text>", _
Replace:= <wdReplaceAll>, MatchCase:=
<Boolean>
|
- ActiveDocument:
find and replace font effects
(turn on/off)
With ActiveDocument.Content.Find
.Font.<Font effect> = <Boolean>
With .Replacement
.Font.<Font effect> = <Boolean>
End With
.Execute Replace:=wdReplaceAll
End With
|
- ActiveDocument:
find and replace text styles in
a document
With ActiveDocument.Content.Find
.Style = "<Search style>"
With .Replacement
.Style = "<Replacement style>"
End With
.Execute Replace:=wdReplaceAll
End With
|
- Counting the number of instances of a
particular word in a document
With ActiveDocument.Content.Find
Do While .Execute(FindText:=<"searchWord">,
Forward:=True, _
MatchWholeWord:=<True or False>) = True
<Do something when each instance of search word is found>
Loop
End With
|
- Useful methods and attributes of the
Selection object
|
| |
- Selection.Copy:
copy selected text
- Selection.ClearFormatting:
remove formatting effects (e.g. bold) from selected text
- Selection.Delete:
deletes selected text
- Selection.EndKey Unit:= <wdStory>:
move selection to the end of the document
- Selection.Expand:
expand selection to next grouping size of text
- Selection.Font.Bold = <Boolean>
: turn bolding on or off
- Selection.Font.Bold = wdToggle:
toggle bolding
- Selection.Font.Name = <Font name>
: change font type
- Selection.Font.Size = <Size>
: change font size
- Selection.Font.ColorIndex = <Word constant
for color e.g. wdBlack, wdBlue, wdBrightGreen, wdDarkBlue, wdDarkRed
wdDarkYellow, wdGray25, wdGray50, wdGreen, wdPink, wdRed, wedTeal,
wedTurquoise, wdViolet, wdWhite, wdYellow)
- Selection.Font.Italic = <Boolean>
: turn italics on or off
- Selection.Font.Italic = wdToggle:
toggle italics
- Selection.Font.Underline = <Boolean>
: turn underlining on or off or change the type of
underlining (wdUnderlineNone,
wdUnderlineNone,
wdUnderlineSingle,
- Selection.HomeKey Unit:= <wdStory>:
move selection to the top of the document
- Selection.InsertFile(<filename>):
replace selection with the text from the specified file
- Selection.MoveLeft:
move cursor left
- Selection.MoveRight:
move cursor right
- Selection.ParagraphFormat.Alignment = <wdAlignParagraphCenter
or wdAlignParagraphLeft or wdAlignParagraphRight>: to center, left
or right align a paragraph (respectively)
- Selection.ParagraphFormat.SpaceAfter = <#
points e.g. 0, 3, 6 etc.): Adds extra
spacing to the end of paragraph
- Selection.ParagraphFormat.LeftIndent =
InchesToPoints(# inches): # inches for
left indenting of a paragraph
- Selection.ParagraphFormat.RightIndent =
InchesToPoints(# inches): # inches for
right indenting of a paragraph
- Selection.PasteAndFormat (<wdFormatOriginalFormatting
or wdFormatPlainText>): copied text is pasted with the
specified formatting
- Selection.text = <"Selected text">
: writes the selected text to
the document by setting the text attribute directly
- Selection.Type :
returns information about the type of information selected (if any:
< wdSelectionIP - none> <wdSelectionNormal
- text selection> <wdSelectionShape - graphical shape
selected> )
- Selection.TypeText(<"Selected text">)
: writes the selected text to
the document through the method TypeText()
|
- Common and useful collections
|
| |
- Documents(): the collection of
documents currently opened InlineShapes():
includes all the external images inserted into a Word document
- Lists(): all the lists in a Word
document
- Shapes(): all the primitive shapes
in a Word document
- InlineShapes(): all the images
inserted into a Word document
- Tables(): all the tables in a Word
document
|
- Useful methods and attributes of the
Documents collection (the Shapes,
InlineShapes and
Tables collections can be accessed via the
ActiveDocument collection)
|
| |
- Documents.close(<wdPromptToSaveChanges>
or <wdDoNotSaveChanges> or <wdSaveChanges>): closes
all of the open Word documents Close
all open Word documents and apply the effect of specified constant
- Documents.count: returns a positive
integer that is equal to the number of documents currently opened.
- Documents.Item(i):
- Allows access to one document from the collection of
documents.
- 'i'
is a positive integer (i.e. >= 1)
- Documents.Open (<"location and name">):
opens the document with the specified location and name and adds it
to the documents collection
- Documents(i).PrintOut: print the
document accessed through the index
- Documents(i).Save: save the
document accessed through the index
- Documents(i).SaveAs (<"name">):
save the document (accessed through the index) under another name
|
|
|
| |
- CreateObject("<Name of MS-Office
application>"): starts up the Office application
specified in the brackets. Dir("<Path to a folder>") :
returns the name of a document under the specified path
- Dir("<Path to a document>") :
returns the name of the document under the specified path
- Dir : after the path to a folder
has been specified with a previous call to Dir,
this call returns the name of the next document in the specified
folder
- Len(<string>): this function
returns the number of characters in the specified string
- <Name of an excel
spreadsheet variable>.workbooks.Open("<Path
and name of an Excel spreadsheet/worksheet>"):
opens up the Excel spreadsheet at the specified location.
- <Name of an excel
spreadsheet variable>.Range("<Cell
in a spreadsheet>").Value:
Returns as a string the contents of
the cell of spreadsheet referred to by the spreadsheet variable.
|
-
Excel methods (accessible via VBA run through Word)
|
| |
- <reference to an Excel
window>.Visible = <Boolean> : Makes an Excel
window visible or not visible
- <reference to an Excel
window>.workbooks.open("<Path to an Excel document">) :Opens
the Excel document named under the specified path and returns a
reference to that document.
- <reference
to an Excel window>.range("<Cell address>").Value :
returns the contents of the specified cell
|
| VBA methods (writing a program using Excel) |
| |
- Attributes of the
Range
object
- Range(cell address).Value :
Access the contents of a cell at the specified cell address
- Range(cell address).Font.Bold =
<Boolean> : Turns bolding at the specified cell
address on/off
- Range(cell address) = <Value>
: Assigns a value to the
cell at the specified address
|
| VBA methods (writing a program using
PowerPoint) |
| |
- The ActivePresentation object
(method)
- ActivePresentation.Slides.Add Index
:= <Slideshow index to add a new slide>, Layout := <Slide
layout:ppLayoutCustom, ppLayoutBlank> :
adds a new slide at the specified index with the specified
layout
- The ActivePresentation object
(attribute)
- ActivePresentation.Slides.Count :
the current number of slides in a slideshow (PPTX file)
|
As mentioned VBA documentation WILL NOT be provided for the following (you're
expected to know how to apply them without documentation)
(This is only a quick overview, you should look to the course notes for
details)
You could see exam questions that involve any of the above questions in the
form of VBA code writing or a program code trace.