-
-
Notifications
You must be signed in to change notification settings - Fork 802
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
python/python-vba/Lesson 11 - Calling Macros From Win32COM.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |