Excel and Access, LLC

Expert Access Developers Help your Business

Expert Excel Developers Help your Business

Code Samples: Excel and Access, LLC

This area of our website is for examples of Expert Excel and Access VBA (Visual Basic for Applications).  It is written by Expert Excel Consultants and Expert Access Programmers. 

Excel and Access, LLC
Info@ExcelandAccessLLC.Com
714.262.6893


------------------------------
This examples shows you how to ...

CallOption Compare Database
Option Explicit
Private Sub Command1_Click()
    Dim DB As Database
    Dim rst As Recordset
    Dim sql As String
   
    Set DB = CurrentDb
   
    sql = "SELECT Email FROM Email;"
    Set rst = DB.OpenRecordset(sql)
    rst.MoveFirst
   
    Do Until rst.EOF
      
        Dim AppOutLook As Outlook.Application
        Dim MailOutLook As Outlook.MailItem
       
        Set AppOutLook = CreateObject("Outlook.Application")
        Set MailOutLook = AppOutLook.CreateItemFromTemplate(CurrentProject.Path & "\OutlookTemplateFile.oft")
        Dim xnamemail As String
        xnamemail = "Default"
       
        Call AppOutLook.GetNamespace("MAPI").Logon(Profile:=xnamemail)
        With MailOutLook
           .BodyFormat = olFormatHTML
           .TO = rst.Fields(0)
           .Subject = "Place Subject Here"
           .Importance = olImportanceHigh
           .Send
        End With
              
        rst.MoveNext
       
    Loop
End Sub
or E-mail to
------------------------------




This examples shows you how to .

------------------------------
Here are some useful ones I often post to answers on EE -- the first gets a file name (generally saved to a variable for use in code), and the second gets a folder name.  They require setting a reference to the appropriate version of the Microsoft Office object library.
 
One:

Public Function SelectFile() As String
'Requires Office XP (2002) or higher
'Requires a reference to the Microsoft Office Object Library
'Created by Helen Feddema 3-Aug-2009
'Last modified 3-Aug-2009

On Error GoTo ErrorHandler

   Dim fd As Office.FileDialog
   Dim varSelectedItem As Variant
   Dim strFileNameAndPath As String
  
   'Create a FileDialog object as a File Picker dialog box.
   Set fd = Application.FileDialog(msoFileDialogFilePicker)
  
   With fd
      'Set AllowMultiSelect to True to allow selection of multiple files
      .AllowMultiSelect = False
      .Title = "Browse for File"
      .ButtonName = "Select"
      .Filters.Clear
      .Filters.Add "Documents", "*.doc; *.txt", 1
      .InitialView = msoFileDialogViewDetails
      If .Show = -1 Then
         'Get selected item in the FileDialogSelectedItems collection
         For Each varSelectedItem In .SelectedItems
            strFileNameAndPath = CStr(varSelectedItem)
         Next varSelectedItem
      Else
         Debug.Print "User pressed Cancel"
         strFileNameAndPath = ""
      End If
   End With
  
   SelectFile = strFileNameAndPath
  
ErrorHandlerExit:
   Set fd = Nothing
   Exit Function

ErrorHandler:
   MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
   Resume ErrorHandlerExit

End Function

------------------------------


Two:

Public Function SelectFolder() As String
'Requires Office XP (2002) or higher
'Requires a reference to the Microsoft Office Object Library
'Created by Helen Feddema 3-Aug-2009
'Last modified 3-Aug-2009

On Error GoTo ErrorHandler

   Dim strFolderPath As String
   Dim fd As Office.FileDialog
   Dim strPath As String

   'Create a FileDialog object as a Folder Picker dialog box.
   Set fd = Application.FileDialog(msoFileDialogFolderPicker)

   'Set strPath to the folder you want to open initially
   strPath = "G:\Data"

   With fd
      .Title = "Browse for folder where _________ are located"
      .ButtonName = "Select"
      .InitialView = msoFileDialogViewDetails
      '.InitialFileName = strPath
      If .Show = -1 Then
         strFolderPath = CStr(fd.SelectedItems.Item(1)) & "\"
      Else
         Debug.Print "User pressed Cancel"
         strFolderPath = ""
      End If
   End With

   SelectFolder = strFolderPath
  
ErrorHandlerExit:
   Exit Function

ErrorHandler:
   MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
   Resume ErrorHandlerExit

End Function

Helen Feddema

------------------------------




This examples shows you how to ...

 

[ Home ]











Site Links: Excel and Access LLC Home | Home HTML | Home HTM | Expert Excel Consulting Services | Expert Access Consulting Services | Expert Excel Sample Code | Excel and Access LLC Contact | Excel and Access Links | Missing | SiteMap HTML | SiteMap HTM |

Related Site Links: Excel And Access Site | Excel and Access, LLC Blog | Get Excel Help | Get Access Help | Excel Consultants | Excel and Access Training | Excel and Access Consultants | Excel and Access for Business |

Pages on popular sites: Excel And Access LLC on LinkedIn Site | Excel And Access LLC Company on LinkedIn Site | Excel And Access LLC Company on LinkedIn Site | Excel And Access LLC on FaceBook Site | Excel And Access LLC Company on Twitter Site | Excel And Access LLC on Trustys | Excel And Access LLC on MySpace | Excel And Access LLC on LivePerson | Excel And Access LLC on p2w2 | Excel And Access LLC on Get a Freelancer | Excel And Access LLC on IT ToolBox | Excel And Access LLC on Guru Site |

Copyright © 2010 Excel and Access, LLC. All rights reserved. The best place to find the best Expert Excel Consultants.