Lesson Summary Vba ( Email Code)

download Lesson Summary Vba ( Email Code)

of 16

Transcript of Lesson Summary Vba ( Email Code)

  • 8/3/2019 Lesson Summary Vba ( Email Code)

    1/16

    LESSON SUMMARY: How to use the 'VB Editor' and understanding VBA structure.

    The VB Editor

    The 'VB Editor' is accessed from the 'Visual Basic' Toolbar, and opens in a separate window. You would normally switch

    between the application window (WORD, EXCEL or ACCESS) and the VBE window using the Windows 'Task bar' or the 'Alt ' +'Tab' key combination.

    If the 'Visual Basic' toolbar is not visible in your applictaion, you might need to display it by selecting 'View | Toolbars | Visual

    Basic'.

  • 8/3/2019 Lesson Summary Vba ( Email Code)

    2/16

    The three controls you can see on the 'Visual Basic' Toolbar shown above are ...

  • 8/3/2019 Lesson Summary Vba ( Email Code)

    3/16

    Visual Basic Editor [ VB Editor ]

    Control Toolbox

    Design Mode

    You can leave this toolbar to 'float', but it is best to move it into the main toolbar section below the main menu bar.

    VISUAL BASIC EDITOR [Description]

    The VB Editor is where you enter your VBA Code. You can also enter the VB Editor by double clicking on a control if you are in

    design mode. [See below]

  • 8/3/2019 Lesson Summary Vba ( Email Code)

    4/16

    In this view of the VB Editor, you can see the 'Project Explorer' window on the left with the 'Code' window on the right. There are

    other windows you can open, such as the 'Property' window and the 'object Browser' window. Use the 'View' menu to select

    which windows you wish to view.

    CONTROL TOOLBOX

    This is where you select the type of control to use. It could be ...

  • 8/3/2019 Lesson Summary Vba ( Email Code)

    5/16

    a simple 3D Button, called a "Command Button"

    a Radio Button

    a Picture Button

    ... or a more complicated control such as a Combo Box.

    DESIGN MODE

    Use this to switch between being able to edit a button, and being able to use a button.

    For example, in edit mode, if you click on an object such as a 'CommandButton', you will be automatically taken to the objects

    'click' procedure in the VBE.

    If you right click on a object in edit mode, a menu will appear where you access its various properties.

  • 8/3/2019 Lesson Summary Vba ( Email Code)

    6/16

    If you turn edit mode off, when you click on the object, you will run its 'click' procedure if one has been created.

    ERROR HANDLING !

    Before you start using the VBE you must know how to handle errors in your VBA code. At this stage, you will most likely have a

    'Compiler Error', often refered to as a 'Development Error', where there is a spelling or expression error.

  • 8/3/2019 Lesson Summary Vba ( Email Code)

    7/16

    The line of code containing the error will be highlighted in red and you will be given a hint as to the cause of the error in the

    Error Message dialog box.

    If your code has a 'Run-Time Error', the VBA code will be halted, and will not restart until you reset the project using the 'reset'button on the VBA toolbar. You will be given a hint as to what the problem is as well by an error code.

  • 8/3/2019 Lesson Summary Vba ( Email Code)

    8/16

    Handling 'run-time' errors will be looked at in 'Lesson 8' and 'debugging' your code will be looked at in detail in 'Lesson 18'.

    MY FIRST VBA PROGRAM !

    STEP ONE: Start a new word document and save it as 'VBA Basics.doc'.

    STEP TWO: Open the control toolbox and select a 'Command Button'. [ It will be pasted into your document ]

    STEP THREE: Copy the following lines ...

    Dim Msg as String

    Msg = "Hello!"

    MsgBox Msg

    STEP FOUR: Double click on the button to open the 'VB Editor'.

    STEP FIVE: Paste the copied text in between the following two lines which will have been created when you double clicked on

    the control.

    ( This code will NOT be created if you enter the 'VB Editor' via the Toolbar! ).

    Private Sub CommandButton1_Click()

    End Sub

  • 8/3/2019 Lesson Summary Vba ( Email Code)

    9/16

    STEP SIX: Exit the 'VB Editor', turn off Design Mode , then click on the CommandButton. A message should appear saying

    "Hello!"

    EXPLANATION:

  • 8/3/2019 Lesson Summary Vba ( Email Code)

    10/16

    A VBA project will react to a defined 'Event', such as a keypress, a mouse action, or a document 'Event' such as being

  • 8/3/2019 Lesson Summary Vba ( Email Code)

    11/16

    opened, closed or saved.

    The defined 'Event' will start a VBA code procedure, called a sub , which is a 'sub-routine' within a larger program body.

    The VBA code within that 'sub' will initiate action(s) on the properties of OFFICE application Objects.

    In our example we displayed a dialog box 'Object' using the 'MsgBox' function where we set its 'prompt' property value to

    "Hello!".

    For example ....

    A WORD Document has many 'Objects': characters, words, paragraphs, tables, footers, headers etc.

    OFFICE applications have many 'Objects': Menus, ToolBars, Controls etc.

    All of these 'Objects' have 'properties' that can have actions performed upon them by VBA code.

    So, the ' BASIC VBA CONCEPT ' is that ....

    anything in a OFFICE application is an 'object' that has 'properties' that can have actions performed on it by VBA code!

    EXERCISE

    Before you continue, have a good look at the Help contents from within the 'VB Editor'. Look especially at the sections on 'Visual

    basic User Interface Help' and 'Visual Basic How-To Topics'. Using the 'Help' provided in the 'Visual Basic Editor' is the best

    way to expand your knowledge of VBA after gaining knowledge of some computer programming concepts. Look at the screen

  • 8/3/2019 Lesson Summary Vba ( Email Code)

    12/16

    shot below to see how comprehensive the available help is!

  • 8/3/2019 Lesson Summary Vba ( Email Code)

    13/16

  • 8/3/2019 Lesson Summary Vba ( Email Code)

    14/16

    Introduction Index Page Lesson 2

    2007 Gary Radley

    be looked at in 'Lesson 8' and 'debugging' your code will be looked at in detail in 'Lesson 18'.

    How to Send Email Using Excel 2007 VBAVisual Basic for Applications (VBA) is commonly used to automate routine tasks in Microsoft Office applications. Sending an email from

    Microsoft Excel 2007 using VBA is useful in cases when you need to continuously email the latest data in an Excel workbook. In a few steps

    you can write code to easily automate the task of sending the email through Microsoft Outlook.

    Read more: How to Send Email Using Excel 2007 VBA | eHow.comhttp://www.ehow.com/how_6931953_send-

    using-excel-2007-vba.html#ixzz1kpWCVmw1

    Instructions

    1.

    o 1

  • 8/3/2019 Lesson Summary Vba ( Email Code)

    15/16

    Start Microsoft Office 2007 and select the "Developer" tab. Cl ick on "Visual Basic" and select "Tools." Click on "References" check "Microsoft

    Outlook 12.0 Object Library" and select "OK."

    o 2

    Type the following to create a new sub:

    Sub sendEmail()

    Press "Enter."

    o 3

    Type the following to create your variables:

    Dim oLookApp As Outlook.Application

    Dim oLookMail As Outlook.MailItem

    o 4

    Type the following to assign the outlook object and create a new email:

    Set oLookApp = New Outlook.Application

    Set oLookMail = oLookApp.CreateItem(0)

    o 5

    Type the following to define the variables for your email:

    With oLookMail

  • 8/3/2019 Lesson Summary Vba ( Email Code)

    16/16

    .To = "[email protected]"

    .Subject = "My Workbook"

    .Body = "I'm e-mailing this workbook.."

    .Attachments.Add ActiveWorkbook.FullName

    .send

    End With

    o 6

    Execute your sub to email the current Excel workbook.

    Read more: How to Send Email Using Excel 2007 VBA | eHow.comhttp://www.ehow.com/how_6931953_send-

    using-excel-2007-vba.html#ixzz1kpWMgDfa