Using OLE Automation in VB

Page 2 of 3
  1. Introduction
  2. Creating the Application
  3. The rest of the code

Creating the Application

Follow the steps below to create the application:

Start a new EXE project. Select Microsoft Word 9.0 object library from the references dialog box (Project|References menu). Remember, you should have Word instance on your system. Add a new module to the project. In the module's general declarations section, add the following code:

Public objApp as New Word.application
Public objDoc as Object

Open the project's form and add four buttons to the form.
Buttons are:

  1. Launch
  2. Save
  3. Close
  4. Exit

Now double click the "Launch" button and add the following code:

Private sub cmdLaunch_click()

If Not(objdoc is nothing) then
    MsgBox "Application already running"
    Exit Sub
End if

'show the app
objapp.visible=true

'Add a document to the application

Set objdoc = objapp.Documents.Add()

'Write some text in the document

objdoc.content = "This is my first OLE application"

End Sub

Let's discuss the code. We check if the application is already running, if it's not then we display the application. We add a new document to the open word application and add some text to the application.

You might also like...

Comments

About the author

S.S. Ahmed United States

S.S. Ahmed is a senior IT Professional and works for a web and software development firm. Ahmed is a Microsoft Office SharePoint Server MVP. Ahmed specializes in creating database driven dynamic...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Anyone who considers arithmetic methods of producing random digits is, of course, in a state of sin.” - John von Neumann