Connecting Tech Pros Worldwide Forums | Help | Site Map

Application Automation

NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 16,173
#1   Feb 10 '07
In VBA (I expect VB would be similar) controlling another Office Application (Automation) can be done using the following steps :
  1. Ensure the Reference (VBA Window / Tools / References) has been ticked for the application you want to Automate.
  2. Set up an application object variable to use (You can use With...End With if you prefer).
    Either :
    1. Use CreateObject(Class) to open a new object for multi-instance programs (For single Instance programs it will return the current instance if already open).
    2. If supported for your desired application use GetObject(Document, Class) to open a specific document.
  3. Set the application's .Visible property. This determines if your changes are visible to the operator.
  4. While the application's object is active, you can run code as if within the other application simply by referencing the application object (EG. accApp.Visible = True...Call accApp.CurrentDB.Close).
  5. If you're finished with one document but you still want to keep the application ready for your use then set the .Visible property to False and re-activate later.
  6. When you're finished completely either :
    1. If you want to close down the application simply close it (Call accApp.Quit).
    2. If you want to leave it open for the operator to use just make sure you leave .Visible set to True.
  7. Release any associated objects (Set to Nothing or End With).
Here are some useful Methods / Properties that can be used from within your code :
  • Quit - Each Application object has a Quit method.
  • Activate - This sets the focus to the Application.
  • Run - Enables you to run code from the Application (or Document).
  • Copy / Paste - This should work happily between different Applications
  • WindowState - Can be set to Maximized, Minimized or Normal.

Posted by fauxanadu

Basic Outlook Automation (including Send New e-Mail)
Expand|Select|Wrap|Line Numbers
  1. ' Requires A References to the Microsoft Outlook Object Library
  2. ' in VBE, select Tools->References, and ensure it is checked
  3.  
  4. Private Sub ExportToOutlook()
  5.     ' Variable Declaration
  6.     Dim objOutlook as Object
  7.     Dim objEMail as Object
  8.  
  9.     ' If Outlook is open, use GetObject, otherwise open Outlook
  10.     Set objOutlook = GetObject(, "Outlook.Application")
  11.     If objOutlook Is Nothing Then Set objOutlook = CreateObject("Outlook.Application")
  12.  
  13.     ' Creates a new e-mail
  14.     Set objEMail = objOutlook.CreateItem(0)
  15.     With objEMail
  16.  
  17.         ' Adds To Recipient
  18.         Set ToContact = .Recipients.Add("Me@Gmail.Com")
  19.  
  20.         ' Adds CC recipient
  21.         ToContact.Type = olCC
  22.         Set ToContact = .Recipients.Add("You@Gmail.com")
  23.  
  24.         ' Sets the Subject
  25.         .Subject = "Service Report 1234"
  26.  
  27.         ' Sets the Body
  28.         .Body = "Attached herein are the Reports"
  29.  
  30.         ' Adds attachment
  31.         .Attachments.Add "c:\Service Report 1234", olByValue, , "Service Report"
  32.  
  33.         ' Embeds attachment
  34.         .Attachments.Add "c:\JoeBob.gif", olEmbeddedItem, , "Joe Bob's Picture"
  35.  
  36.         ' Receipt upon delivery
  37.         .OriginatorDeliveryReportRequested = True
  38.  
  39.         ' Recipt upon read
  40.         .ReadReceiptRequested = True
  41.  
  42.         ' Displays the E-Mail
  43.         .Display
  44.  
  45.         ' Sends the E-Mail
  46.         .Send
  47.  
  48.         ' Saves a Draft of the E-Mail
  49.         .Save                                        
  50.     End With
  51.  
  52. End Sub
Basic Excel Automation
Expand|Select|Wrap|Line Numbers
  1. ' Requires a Reference to Microsoft Excel 8.0 Object Library or Higher
  2. ' In VBE, goto Tools->References... and select it from the list
  3. Private Const conAppNotRunning As Long = 429
  4.  
  5. Private Sub ExportToExcel()
  6.     ' Variable Declarations
  7.     Dim objExcel As Excel.Application
  8.  
  9.     ' If Excel is open, use GetObject, otherwise create a new Excel object
  10.     On Error Resume Next
  11.     Set objExcel = GetObject(, "Excel.Application")
  12.     If Err = conAppNotRunning Then Set objExcel = New Excel.Application
  13.  
  14.     With objExcel
  15.         ' Adds a new workbook to the Excel environment
  16.         .Workbooks.Add
  17.  
  18.         ' Excel VBA Code goes here
  19.  
  20.         ' Causes the Excel window to become visible
  21.         .Visible = True
  22.     End With
  23. End Sub



Reply