Skip to content

Commit a8c0c90

Browse files
committed
Add Class Modules
1 parent 314c46f commit a8c0c90

File tree

5 files changed

+114
-36
lines changed

5 files changed

+114
-36
lines changed

scripts/Build-VBA.ps1

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,63 @@ if ($null -eq $doc) {
121121
}
122122

123123
# Define the module folder path
124-
125124
$moduleFolder = GetAbsPath -path "$folderName/Modules" -basePath $currentDir
126125
Write-Host "Module folder path: $moduleFolder"
127126

127+
# Define the class modules folder path
128+
$classModulesFolder = GetAbsPath -path "$folderName/Class Modules" -basePath $currentDir
129+
Write-Host "Class Modules folder path: $classModulesFolder"
130+
128131
#Check if the module folder does not exist create an empty one
129132
if (-not (Test-Path $moduleFolder)) {
130133
Write-Host "Module folder not found: $moduleFolder"
131134
New-Item -ItemType Directory -Path $moduleFolder -Force | Out-Null
132135
Write-Host "Created module folder: $moduleFolder"
133136
}
134137

138+
#Check if the class modules folder does not exist create an empty one
139+
if (-not (Test-Path $classModulesFolder)) {
140+
Write-Host "Class Modules folder not found: $classModulesFolder"
141+
New-Item -ItemType Directory -Path $classModulesFolder -Force | Out-Null
142+
Write-Host "Created class modules folder: $classModulesFolder"
143+
}
144+
145+
# Import class modules first (.cls files)
146+
$clsFiles = Get-ChildItem -Path $classModulesFolder -Filter *.cls -ErrorAction SilentlyContinue
147+
Write-Host "Found $($clsFiles.Count) .cls files to import"
148+
149+
# Loop through each class module file
150+
$clsFiles | ForEach-Object {
151+
Write-Host "Importing class module $($_.Name)..."
152+
try {
153+
$vbProject = $doc.VBProject
154+
# Check if the VBProject is accessible
155+
if ($null -eq $vbProject) {
156+
Write-Host "VBProject is not accessible. Attempting to re-open the application..."
157+
$officeApp.Quit()
158+
Start-Sleep -Seconds 2
159+
$officeApp = New-Object -ComObject $officeApp.Application
160+
$doc = $officeApp.Workbooks.Open($outputFilePath)
161+
162+
$vbProject = $doc.VBProject
163+
164+
if ($null -eq $vbProject) {
165+
Write-Host "VBProject is still not accessible after re-opening the application."
166+
# Throw an error to trigger the catch block
167+
exit 1
168+
} else {
169+
Write-Host "VBProject is now accessible after re-opening the application."
170+
}
171+
}
172+
173+
$vbProject.VBComponents.Import($_.FullName)
174+
175+
Write-Host "Successfully imported class module $($_.Name)"
176+
} catch {
177+
Write-Host "Failed to import class module $($_.Name): $($_.Exception.Message)"
178+
}
179+
}
180+
135181
# First check if there are any .bas files
136182
$basFiles = Get-ChildItem -Path $moduleFolder -Filter *.bas
137183
Write-Host "Found $($basFiles.Count) .bas files to import"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
VERSION 1.0 CLASS
2+
BEGIN
3+
MultiUse = -1 'True
4+
END
5+
Attribute VB_Name = "Class1"
6+
Attribute VB_GlobalNameSpace = False
7+
Attribute VB_Creatable = False
8+
Attribute VB_PredeclaredId = True
9+
Attribute VB_Exposed = False
10+
'@Lang VBA
11+
Option Explicit
12+
13+
14+
Public Sub ExecuteWrite()
15+
Dim filePath As String
16+
Dim fileNum As Integer
17+
18+
' Specify the path to the text file
19+
filePath = ThisWorkbook.Path & "\ExcelAddin.txt"
20+
21+
' Get a free file number
22+
fileNum = FreeFile
23+
24+
' Open the file for output
25+
Open filePath For Output As #fileNum
26+
27+
' Write some text to the file
28+
Print #fileNum, "Hello, World!"
29+
30+
' Close the file
31+
Close #fileNum
32+
End Sub

tests/ExcelAddin.xlam/Modules/Module1.bas

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,5 @@ End Sub
99

1010
'This code will be called via COM to test if the VBA import was successful
1111
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
12+
Class1.ExecuteWrite()
2913
End Sub
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
VERSION 1.0 CLASS
2+
BEGIN
3+
MultiUse = -1 'True
4+
END
5+
Attribute VB_Name = "Class1"
6+
Attribute VB_GlobalNameSpace = False
7+
Attribute VB_Creatable = False
8+
Attribute VB_PredeclaredId = True
9+
Attribute VB_Exposed = False
10+
'@Lang VBA
11+
Option Explicit
12+
13+
14+
Public Sub ExecuteWrite()
15+
Dim filePath As String
16+
Dim fileNum As Integer
17+
18+
' Specify the path to the text file
19+
filePath = ThisDocument.Path & "\WordDocument.txt"
20+
21+
' Get a free file number
22+
fileNum = FreeFile
23+
24+
' Open the file for output
25+
Open filePath For Output As #fileNum
26+
27+
' Write some text to the file
28+
Print #fileNum, "Hello, World!"
29+
30+
' Close the file
31+
Close #fileNum
32+
End Sub

tests/WordDocument.docm/Modules/Module1.bas

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,6 @@ Sub Demo()
88
End Sub
99

1010
'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
11+
Public Sub WriteToFile()
12+
Class1.ExecuteWrite()
2913
End Sub

0 commit comments

Comments
 (0)