Skip to content

Commit afc81fa

Browse files
committed
Better screenshots and test VBA Module1 execution
1 parent 9a61f59 commit afc81fa

File tree

7 files changed

+152
-9
lines changed

7 files changed

+152
-9
lines changed

scripts/Build-VBA.ps1

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
# and imports all .bas files from a specified folder into the document.
55
# It then saves and closes the document, and cleans up the COM objects.
66

7+
# Load utiliies
8+
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
9+
. "$scriptPath/utils/Screenshot.ps1"
10+
11+
# Args
712
$folderName = $args[0]
813
$officeAppName = $args[1]
914

@@ -37,6 +42,12 @@ if (-not (Test-Path $outputFilePath)) {
3742
exit 1
3843
}
3944

45+
$screenshotDir = (DirUp $outputDir) + "screenshots/"
46+
if (-not (Test-Path $screenshotDir)) {
47+
New-Item -ItemType Directory -Path $screenshotDir -Force | Out-Null
48+
Write-Host "Created screenshot directory: $screenshotDir"
49+
}
50+
4051
# Allows to double-ckeck if the VBOM is enabled
4152
# HKCU:\Software\Microsoft\Office\16.0\Common\TrustCenter
4253
# Just check the registry entry
@@ -105,6 +116,7 @@ if ($officeAppName -eq "Excel") {
105116
# Check if the document was opened successfully
106117
if ($null -eq $doc) {
107118
Write-Host "Error: Failed to open the document: $outputFilePath"
119+
Take-Screenshot -OutputPath "${screenshotDir}Screenshot_${fileNameNoExt}_{{timestamp}}.png"
108120
exit 1
109121
}
110122

@@ -156,15 +168,7 @@ $basFiles | ForEach-Object {
156168
}
157169
}
158170

159-
# Take a screenshot of the Office application
160-
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
161-
. "$scriptPath/utils/Screenshot.ps1"
162-
$screenshotDir = (DirUp $outputDir) + "screenshots/"
163-
if (-not (Test-Path $screenshotDir)) {
164-
New-Item -ItemType Directory -Path $screenshotDir -Force | Out-Null
165-
Write-Host "Created screenshot directory: $screenshotDir"
166-
}
167-
Take-Screenshot -OutputPath "${screenshotDir}Screenshot_${fileNameNoExt}.png"
171+
Take-Screenshot -OutputPath "${screenshotDir}Screenshot_${fileNameNoExt}_{{timestamp}}.png"
168172

169173
# Save the document
170174
Write-Host "Saving document..."
@@ -179,6 +183,7 @@ try {
179183
}
180184
} catch {
181185
Write-Host "Warning: Could not save document: $($_.Exception.Message)"
186+
Take-Screenshot -OutputPath "${screenshotDir}Screenshot_${fileNameNoExt}_{{timestamp}}.png"
182187

183188
# Alternative approach for PowerPoint if SaveAs fails
184189
if ($officeAppName -eq "PowerPoint") {
@@ -210,10 +215,39 @@ try {
210215
exit 0
211216
} catch {
212217
Write-Host "Error: Alternative save method also failed: $($_.Exception.Message)"
218+
Take-Screenshot -OutputPath "${screenshotDir}Screenshot_${fileNameNoExt}_{{timestamp}}.png"
213219
}
214220
}
215221
}
216222

223+
# Call the WriteFile macro to check if the module was imported correctly
224+
try {
225+
$macroName = "WriteFile"
226+
$officeApp.Run($macroName)
227+
228+
# Check if the file was created successfully with the correct content
229+
$outputFile = "$outputDir/$fileNameNoExt.txt"
230+
if (Test-Path $outputFile) {
231+
$fileContent = Get-Content -Path $outputFile
232+
if ($fileContent -eq "Hello, World") {
233+
Write-Host "🟢 Macro executed successfully and file content is correct."
234+
} else {
235+
Write-Host "🟡 Warning: Macro executed, but file content is incorrect."
236+
}
237+
238+
# Delete the output file after checking
239+
Remove-Item -Path $outputFile -Force
240+
Write-Host "Output test file deleted successfully."
241+
242+
} else {
243+
Write-Host "🟡 Warning: Macro executed, but output file was not created."
244+
}
245+
246+
} catch {
247+
Write-Host "🟡 Warning: Could not execute macro ${macroName}: $($_.Exception.Message)"
248+
Take-Screenshot -OutputPath "${screenshotDir}Screenshot_${fileNameNoExt}_{{timestamp}}.png"
249+
}
250+
217251
# Close the document
218252
try {
219253
$doc.Close()

scripts/utils/Screenshot.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ function Take-Screenshot {
2020
# Copy the screen to the bitmap
2121
$graphic.CopyFromScreen($bounds.X, $bounds.Y, 0, 0, $bounds.Size)
2222

23+
# Replace {{timestamp}} with the current date and time
24+
$timestamp = (Get-Date).ToString("yyyyMMdd_HHmmss")
25+
$OutputPath = $OutputPath -replace "{{timestamp}}", $timestamp
26+
2327
# Save the bitmap as a file
2428
$bitmap.Save($OutputPath)
2529

tests/AccessDatabase.accdb/Modules/Module1.bas

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,25 @@ Option Explicit
55

66
Sub Demo()
77
MsgBox "Hello, World!"
8+
End Sub
9+
10+
'This code will be called via COM to test if the VBA import was successful
11+
Sub WriteToFile()
12+
Dim filePath As String
13+
Dim fileNum As Integer
14+
15+
' Specify the path to the text file
16+
filePath = CurrentProject.Path & "\AccessDatabase.txt"
17+
18+
' Get a free file number
19+
fileNum = FreeFile
20+
21+
' Open the file for output
22+
Open filePath For Output As #fileNum
23+
24+
' Write some text to the file
25+
Print #fileNum, "Hello, World!"
26+
27+
' Close the file
28+
Close #fileNum
829
End Sub

tests/ExcelAddin.xlam/Modules/Module1.bas

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,25 @@ Option Explicit
55

66
Sub Demo()
77
MsgBox "Hello, World!"
8+
End Sub
9+
10+
'This code will be called via COM to test if the VBA import was successful
11+
Sub WriteToFile()
12+
Dim filePath As String
13+
Dim fileNum As Integer
14+
15+
' Specify the path to the text file
16+
filePath = ThisWorkbook.Path & "\ExcelAddin.txt"
17+
18+
' Get a free file number
19+
fileNum = FreeFile
20+
21+
' Open the file for output
22+
Open filePath For Output As #fileNum
23+
24+
' Write some text to the file
25+
Print #fileNum, "Hello, World!"
26+
27+
' Close the file
28+
Close #fileNum
829
End Sub

tests/ExcelWorkbook.xlsm/Modules/Module1.bas

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,25 @@ Option Explicit
55

66
Sub Demo()
77
MsgBox "Hello, World!"
8+
End Sub
9+
10+
'This code will be called via COM to test if the VBA import was successful
11+
Sub WriteToFile()
12+
Dim filePath As String
13+
Dim fileNum As Integer
14+
15+
' Specify the path to the text file
16+
filePath = ThisWorkbook.Path & "\ExcelWorkbook.txt"
17+
18+
' Get a free file number
19+
fileNum = FreeFile
20+
21+
' Open the file for output
22+
Open filePath For Output As #fileNum
23+
24+
' Write some text to the file
25+
Print #fileNum, "Hello, World!"
26+
27+
' Close the file
28+
Close #fileNum
829
End Sub

tests/PowerPointPresentation.pptm/Modules/Module1.bas

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,25 @@ Option Explicit
55

66
Sub Demo()
77
MsgBox "Hello, World!"
8+
End Sub
9+
10+
'This code will be called via COM to test if the VBA import was successful
11+
Sub WriteToFile()
12+
Dim filePath As String
13+
Dim fileNum As Integer
14+
15+
' Specify the path to the text file
16+
filePath = ThisPresentation.Path & "\PowerPointPresentation.txt"
17+
18+
' Get a free file number
19+
fileNum = FreeFile
20+
21+
' Open the file for output
22+
Open filePath For Output As #fileNum
23+
24+
' Write some text to the file
25+
Print #fileNum, "Hello, World!"
26+
27+
' Close the file
28+
Close #fileNum
829
End Sub

tests/WordDocument.docm/Modules/Module1.bas

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,25 @@ Option Explicit
55

66
Sub Demo()
77
MsgBox "Hello, World!"
8+
End Sub
9+
10+
'This code will be called via COM to test if the VBA import was successful
11+
Sub WriteToFile()
12+
Dim filePath As String
13+
Dim fileNum As Integer
14+
15+
' Specify the path to the text file
16+
filePath = ThisDocument.Path & "\WordDocument.txt"
17+
18+
' Get a free file number
19+
fileNum = FreeFile
20+
21+
' Open the file for output
22+
Open filePath For Output As #fileNum
23+
24+
' Write some text to the file
25+
Print #fileNum, "Hello, World!"
26+
27+
' Close the file
28+
Close #fileNum
829
End Sub

0 commit comments

Comments
 (0)