Visual Basic

advertisement
Visual Basic
Using Microsoft Word Objects
This section gives us an introduction into Visual Basic and its use with Microsoft Word.
You will be entering commands to the Visual Basic Editor. These commands may be
used to: select text from the document, print words or sentences, change words, count
paragraphs and so on.
Getting Started:
 First you must open a blank Word Document.
 Now enter Blackboard. The text needed for homework can be found on
Blackboard under the Assignments Tab, under Assignment 2 Source Code for
Project. Please use the Assignments listing, the top link on this page.
 Open the Source Code document, copy the text, and paste it into the empty word
document from the first step.
 Save your document as immediate.doc
Entering the Visual Basic Editor
 Your Word document, named immediate.doc, must be opened.
 Select Tools from the top menu of Word, then select Macro, then Visual Basic
Editor
Opening the Immediate Window
 From inside Visual Basic Editor, select View, then Immediate Window
 The Immediate Window will appear at the right.
Sample Commands to get you started



Enter: ?application.documents(1).name
This will print out the name of your
Word document. If you followed the above steps you will see: Immediate.doc
Enter: ?application.documents(1).words.count This will print the number of
words
Enter: application.documents(1).sentences(4).select
Notice there is not a ?
at the beginning. This will select the fourth sentence. You can see this if you go
back to your Word document. You can go back by clicking on the Word
document on the bottom Task Bar. To return click on the Microsoft Visual Basic
tab on the Task Bar.
Introduction to Objects in Microsoft Word
Objects are used to reference portions of a document, anything from single
characters, words, sentences, paragraphs for example. Visual Basic provides the user
with a means to view the different objects from within the Visual Basic Editor.
Here are some of the basic objects:
 application The entire word application which may contain more than
one document
 documents Refers to one or more documents currently open
 paragraphs Refers to one or more paragraphs
 sentences
Refers to one or more sentences
 words
Used to reference one or more words
 selection
Used to reference text selected (drag mouse over)
When we wish to reference a document or portion of a document we will use
these objects.
Immediate Window
In the following, enter each command. The command is in bold.
To print the name of the current Microsoft Application:
?application
Microsoft Word
To print the current number of open documents:
?application.Documents.Count
1
To print the name of the first document:
?application.Documents(1).Name
Word Objects.doc
To print the number of paragraphs:
?application.Documents(1).Paragraphs.Count
8
To print the text of the fourth sentence (heading counts as one sentence)
?application.Documents("Word Objects.doc").Sentences(4).Text
Here are some of the basic objects:
To print selected text, first select text in the document, then go back to the Editor.
?application.selection.words(2)
print
To print all selected text first select text in the document, then return to Editor.
?application.Selection.Text
To print selected text, first select text in the document
To select text under control of the macro, enter this text from within the Editor.
(Note, this time do not precede the statement with a ?. We are not printing any
response, just selecting text)
application.Documents(1).Sentences(3).Select
Print text just selected.
?application.Selection.Text
Visual Basic provides the user with a means to view the different objects from
within the Visual Basic Editor.
Download