Professional Documents
Culture Documents
Another powerful feature of Excel VBA is to interact with Other Applications. We can deal with Other MS
Office Applications (Word, PowerPoint,Access). We can also deal with Other Applications like Internet
Explorer, SAS, VBScript from Excel. I will show some examples on interacting with different Application from
Excel.
Sub sbPowePoint_SendDataFromExcelToPPT()
'Declarations
Dim oPPT As PowerPoint.Application
Dim oPPres As PowerPoint.Presentation
Dim oPSlide As PowerPoint.Slide
Dim sText As String
'Open PowerPoint
Set oPPT = New PowerPoint.Application
Set oPPres = oPPT.Presentations.Add
oPPT.Visible = True
'Add a Slide
Set oPSlide = oPPres.Slides.Add(1, ppLayoutTitleOnly)
oPSlide.Select
oPPT.Activate
'Release Objects
End Sub
Sub sbWord_FormatingWordDoc()
'Declarations
Dim oWApp As Word.Application
Dim oWDoc As Word.Document
Dim sText As String
Dim iCntr As Long
For i = 0 To 2
Set para = oWDoc.Paragraphs.Add
para.Space2
Next
oWApp.Visible = True
End Sub
Sub sbAccess_OpenAForm()
'Opening a From
With oAApp
.DoCmd.OpenForm "MyForm", acNormal
.Visible = True
End With
End Sub
Sub sbOutlook_SendAMail()
'Declaration
Dim oOApp As Object
Dim oMail As Object
End Sub
Sub sbWord_ExcelToWord()
'Declarations
Dim oWApp As Word.Application
Dim oWDoc As Word.Document
Dim sText As String
Dim iCntr As Long
oWApp.Visible = True
End Sub
Sub sbIE_OpenASite()
Dim IE As Object
IE.Visible = True
'Release
Set IE = Nothing
End Sub
Sub sbAnyApplication_OpenCalculator()
End Sub
Sub sbVBScript_RunVBS()
End Sub
Sub emailingProgram()
Dim olapp As Outlook.Application
Dim objmail As Outlook.mailitem
Dim pos As Integer
Set olapp = Outlook.Application
For Each xcell In Sheets("Sheet1").Range(Range("RangetoCopy"), _
Range("RangetoCopy").End(xlDown))
msgText = Range("Msg")
xcell.Activate
ActiveCell.Offset(0, 1).Select
'If you think that the email ID is in the pattern firstname.lastname@mail.com use this if block
'The code will go into the else statement if the First Name is not mentioned
If Selection.Value = "" Then
pos = InStr(1, xcell.Value, ".")
Fname = Mid$(xcell.Value, 1, InStr(1, xcell.Value, ".") - 1)
Else
'If you have mentioned the first names in the First Name column this part will read it directly
Fname = Selection.Value
End If
'For each of the cells present in the To List we create a MailItem and send it
Set objmail = olapp.CreateItem(olMailItem)
objmail.BodyFormat = olFormatRichText
'Setting the subject
'Uncomment the following line of code in case you want to send a plain message
'objmail.Body = "Hi " + UCase(Mid$(Fname, 1, 1)) + Mid$(Fname, 2) + "," + Chr(13) + Chr(10) + msgText
Next xcell
End Sub