Skip to content

Commit

Permalink
Add Calling Macros from Win32COM
Browse files Browse the repository at this point in the history
  • Loading branch information
areed1192 committed Feb 13, 2020
1 parent 934b4cd commit ffa5b1d
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions python/python-vba/Lesson 11 - Calling Macros From Win32COM.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import win32com.client

# Grab the Active Instance of Excel.
ExcelApp = win32com.client.GetActiveObject("Excel.Application")

# Grab a workbook called "HasMyMacros". Technically you don't have to do this, but for demonstration purposes it helps out.
xlWorkbook = ExcelApp.Workbooks("HasMyMacros.xlsm")

# Execute the Macro "PopulateTwoCellsWithArguments", pass through the arguments ["Bob","Smith"]
ExcelApp.Run("PopulateTwoCellsWithArguments", "Bob", "Smith")

# Execute the Macro "PopulateTwoCells", which doesn't require any arguments.
ExcelApp.Run("PopulateTwoCells")

# ---------------------------------------------------------------------------------
# VBA CODE - PopulateTwoCellsWithArguments
# ---------------------------------------------------------------------------------

# Sub PopulateTwoCellsWithArguments(FirstName As String, LastName As String)

# Dim Cell1 As Range
# Dim Cell2 As Range

# Set Cell1 = Sheet1.Range("C5")
# Set Cell2 = Sheet1.Range("C6")

# Cell1.Value = FirstName
# Cell2.Value = LastName

# End Sub

# ---------------------------------------------------------------------------------
# VBA CODE - PopulateTwoCells
# ---------------------------------------------------------------------------------

# Sub PopulateTwoCells()

# Dim Cell1 As Range
# Dim Cell2 As Range

# Set Cell1 = Sheet1.Range("C2")
# Set Cell2 = Sheet1.Range("C3")

# Cell1.Value = 3000
# Cell2.Value = 4000

# End Sub

0 comments on commit ffa5b1d

Please sign in to comment.