0% found this document useful (0 votes)
14 views2 pages

Automating Other Application Activity

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Automating Other Application Activity

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1. Open Microsoft Word.

2. Press Alt + F11 to open the VBA editor.


3. Go to Insert > Module to create a new module.
4. Copy and paste the code below into the module:

vba

Sub AutomateExcel()
' Declare Excel application and workbook variables
Dim excelApp As Object
Dim workbook As Object
Dim sheet As Object

' Create a new instance of Excel


Set excelApp = CreateObject("Excel.Application")
excelApp.Visible = True ' Make Excel visible

' Add a new workbook


Set workbook = excelApp.Workbooks.Add

' Add data to the first sheet


Set sheet = workbook.Sheets(1)
sheet.Name = "Automated Data"

' Fill in some data


sheet.Range("A1").Value = "Name"
sheet.Range("B1").Value = "Score"
sheet.Range("A2").Value = "Alice"
sheet.Range("B2").Value = 85
sheet.Range("A3").Value = "Bob"
sheet.Range("B3").Value = 90
sheet.Range("A4").Value = "Charlie"
sheet.Range("B4").Value = 88

' Format the header row


With sheet.Range("A1:B1")
.Font.Bold = True
.Interior.Color = RGB(200, 200, 200)
.Borders.Weight = 2
End With

' Autofit columns


sheet.Columns("A:B").AutoFit

' Clean up objects


Set sheet = Nothing
Set workbook = Nothing
Set excelApp = Nothing
End Sub
1. Open Microsoft Word.
2. Press Alt + F11 to open the VBA editor.
3. Go to Insert > Module to create a new module.
4. Copy and paste the code below into the module:

vba

Sub AutomatePowerPoint()
' Declare PowerPoint application and presentation variables
Dim pptApp As Object
Dim presentation As Object
Dim slide As Object

' Create a new instance of PowerPoint


Set pptApp = CreateObject("PowerPoint.Application")
pptApp.Visible = True ' Make PowerPoint visible

' Add a new presentation


Set presentation = pptApp.Presentations.Add

' Add a title slide


Set slide = presentation.Slides.Add(1, 1) ' 1 = ppLayoutTitle
slide.Shapes.Title.TextFrame.TextRange.Text = "Automated Presentation"
slide.Shapes.Placeholders(2).TextFrame.TextRange.Text = "Created by VBA"

' Add a content slide with a title and some text


Set slide = presentation.Slides.Add(2, 2) ' 2 = ppLayoutText
slide.Shapes.Title.TextFrame.TextRange.Text = "Introduction"
slide.Shapes.Placeholders(2).TextFrame.TextRange.Text = "This slide was
generated using VBA in Word to control PowerPoint."

' Add another slide with an image


Set slide = presentation.Slides.Add(3, 1) ' 1 = ppLayoutTitle
slide.Shapes.Title.TextFrame.TextRange.Text = "Adding Images"

' Ensure the file path is valid before running


slide.Shapes.AddPicture "C:\path\to\image.jpg", _
MsoFalse, MsoCTrue, 100, 150, 400, 300

' Clean up objects


Set slide = Nothing
Set presentation = Nothing
Set pptApp = Nothing
End Sub

You might also like