Results 1 to 3 of 3

Thread: [FAQ's: OD] How do I automate an Office App using VB.NET?

Threaded View

  1. #2

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [FAQ's: OD] How do I automate an Office App using VB.NET?

    VB.NET 2003 Late Binding Automation Code Example:
    Code:
    Option Explicit On 
    'Option Strict Off is needed to allow the late binding
    Option Strict Off
    
    'Late Binding Automation FAQ Example
    Public Class Form1
    
        Inherits System.Windows.Forms.Form
    
        "Windows Form Designer generated code"
    
        'Declare our Application Object as an unknown generic Object
        Public moApp As Object
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Declare and create a Workbook object
            Dim oWB As Object = moApp.Workbooks.Add()
            'Make it Visible to the user now that we are going to use it
            moApp.Visible = True
            'Write some text into Cell A1 on Sheet1
            oWB.Sheets("Sheet1").Cells(1, 1).Value = "Meow!"
            'Make sure our object variable is destroyed and released since its going out of scope
            oWB = Nothing
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Create our generic unknown object variable and trap for Excel not installed
            Try
                moApp = CreateObject("Excel.Application")
                'Keep it hidden from the user until we need to use it
                moApp.Visible = False
            Catch ex As Exception
                MessageBox.Show(ex.Message, "VB/Office Guru™ Excel Late Binding VB.NET Automation FAQ", _
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End Try
        End Sub
    
        Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
        Handles MyBase.Closing
            'Check if Excel Application is still instanciated
            If IsNothing(moApp) = False Then
                'Check if our Excel instance has any opened workbooks in it
                If moApp.Workbooks.Count > 0 Then
                    'Close all open workbooks not saving them
                    'Loop backwards so the index information will be relevant as they close
                    For i As Integer = moApp.Workbooks.Count To 1 Step -1
                        moApp.Workbooks.Item(i).Close(SaveChanges:=False)
                    Next
                End If
                'Close our Excel Application object
                moApp.Quit()
            End If
            'Make sure its destroyed
            moApp = Nothing
            'Absolutely none of our Excel instances will be left running!
        End Sub
    
    End Class
    Last edited by RobDog888; Apr 15th, 2007 at 04:58 PM. Reason: Fix code tags from recent upgrade
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width