Results 1 to 2 of 2
  1. #1
    2 Star Lounger
    Join Date
    Sep 2003
    Toronto, Ontario, Canada
    Thanked 0 Times in 0 Posts

    Exporting to Office (VB6)

    I have an application I developed and I would like to add a feature where the user can export some information to Microsoft Word or Excel.
    (more specifically a template file?)
    Is there some kind of plugin I can get to perform this operation? Any help would be great,

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Thanked 28 Times in 28 Posts

    Re: Exporting to Office (VB6)

    By creating a Word.Application or Excel.Application object, you can run an invisible instance of Word or Excel from your VB6 application, and control it through the VBA version for that Office app. The easiest way to develop the code is to set a reference in Project | References... to the Microsoft Word n.0 Object Library and/or Microsoft Excel n.0 Object Library, where n=8 for Office 97, n=9 for Office 2000, n=10 for Office 2002/XP and n=11 for Office 2003. The advantage of setting a reference is that IntelliSense will display available properties, methods etc. This is called early binding.

    You can then write code like the following:

    Sub TestWord()
    Dim objWordApp As Word.Application
    Dim objWordDoc As Word.Document

    On Error Resume Next

    Set objWordApp = CreateObject("Word.Application")
    If objWordapp Is Nothing Then
    MsgBox "Cannot start Word.", vbExclamation
    Exit Sub
    End If

    On Error GoTo ErrHandler

    Set objWordDoc = objWordApp.Documents.Add
    objWordApp.Selection.TypeText "This document was created from VB6."
    objWordDoc.SaveAs "C:WordTest.doc"

    On Error Resume Next
    objWordDoc.Close False
    Set objWordDoc = Nothing
    Set objWordApp = Nothing
    Exit Sub

    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

    For the final version, you may want to remove the reference, to be independent of the specific version of Office on your development PC. When you do this, you must change the declaration of Word and Excel objects to As Object:

    Dim objWordApp As Object
    Dim objWordDoc As Object

    This is called late binding.

    See WendellB's Automation 101 tutorial. It contains many useful links.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts