Automation
is a process that allows applications
that are written in languages such as
Visual Basic .NET or C# to
programmatically control other
applications. Automation to Word allows
you to perform actions such as creating
new documents, adding text to documents,
mail merge, and formatting documents.
With Word and other Microsoft Office
applications, virtually all of the
actions that you can perform manually
through the user interface can also be
performed programmatically by using
automation. Word exposes this
programmatic functionality through an
object model. The object model is a
collection of classes and methods that
serve as counterparts to the logical
components of Word. For example, there
is an Application object, a Document
object, and a Paragraph object, each of
which contain the functionality of those
components in Word.
You can call the interesting methods and
properties that Microsoft Word provides
to you to manipulate documents in Word.
The best way to learn how to navigate
the object models of Word, Excel, and
PowerPoint is to use the Macro Recorder
in these Office applications:
1. Choose Record New Macro from the
Macro option on the Tools menu and
execute the task you’re interested in.
2. Choose Stop Recording from the Macro
option on the Tools menu.
3. Once you are done recording, choose
Macros from the Macro option on the
Tools menu, select the macro you
recorded, then click Edit.
This takes you to the generated VBA code
that accomplishes the task you recorded.
Keep in mind that the recorded macro
will not be the best possible code in
most cases, but it provides a quick and
usable example.
For example to open an existing file and
append some text:
object fileName = “c:\\test.doc”;
object readOnly = false;
object isVisible = true;
object missing =
System.Reflection.Missing.Value;
Word.ApplicationClass oWordApp = new
Word.ApplicationClass();
EDrawOfficeViewer1.Open(ref fileName,
ref missing, ref missing);
Word.ApplicationClass oWordApp =
EDrawOfficeViewer1.GetApplication();
oWordApp.Selection.TypeText(”This is the
text”);
oWordApp.Selection.TypeParagraph();
|
Macro Makes Office Automation Easier |
| ||||||||||||||||||
