Skip to content

Commit 1cea60d

Browse files
committed
Add new sheets if needed while importing
1 parent 0db6d7a commit 1cea60d

File tree

1 file changed

+54
-16
lines changed

1 file changed

+54
-16
lines changed

src/vbaDeveloper.xlam/Build.bas

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Option Explicit
2121

2222
Private Const IMPORT_DELAY As String = "00:00:03"
2323

24+
'We need to make these variables public such that they can be read by application.ontime
2425
Private componentsToImport As Dictionary 'Key = componentName, Value = componentFilePath
2526
Private vbaProjectToImport As VBProject
2627

@@ -133,14 +134,16 @@ End Sub
133134
Public Sub importVbaCode(vbaProject As VBProject)
134135
'find project files
135136
Dim vbProjectFileName As String
136-
vbProjectFileName = vbaProject.fileName
137+
On Error Resume Next
138+
'this can throw if the workbook has never been saved.
139+
vbProjectFileName = vbaProject.fileName
140+
On Error GoTo 0
137141
If vbProjectFileName = "" Then
138142
'In this case it is a new workbook, we skip it
139143
Debug.Print "No file name for project " & vbaProject.name & ", skipping"
140144
Exit Sub
141145
End If
142146

143-
144147
Dim fso As New Scripting.FileSystemObject
145148
Dim projDir As String
146149
projDir = fso.GetParentFolderName(vbProjectFileName)
@@ -181,7 +184,7 @@ Public Sub importVbaCode(vbaProject As VBProject)
181184
'Then import them
182185
Debug.Print "Invoking Application.Ontime with delay " & IMPORT_DELAY ' To prevent duplicate modules, like MyClass1 etc.
183186
Application.OnTime Now() + TimeValue(IMPORT_DELAY), "'Build.importComponents'"
184-
Debug.Print "Waiting to import code for " & vbaProject.name
187+
Debug.Print "imported code for " & vbaProject.name
185188
End Sub
186189

187190
Private Sub checkHowToImport(file As Object)
@@ -200,7 +203,7 @@ Private Sub checkHowToImport(file As Object)
200203
Case ".cls" ' 10 == Len(".sheet.cls")
201204
If Len(fileName) > 10 And Right(fileName, 10) = ".sheet.cls" Then
202205
'import lines into sheet
203-
'TODO importLines vbaProject, file
206+
importLines vbaProjectToImport, file
204207
Else
205208
'importComponent vbaProject, file
206209
componentsToImport.Add componentName, file.Path
@@ -238,7 +241,6 @@ Public Sub importComponents()
238241
Set vbaProjectToImport = Nothing
239242
End Sub
240243

241-
242244
' Assumes any component with same name has already been removed
243245
Private Sub importComponent(vbaProject As VBProject, filePath As String)
244246
Debug.Print "Importing component from " & filePath
@@ -247,29 +249,65 @@ End Sub
247249

248250

249251
Private Sub importLines(vbaProject As VBProject, file As Object)
250-
Dim component_name As String
251-
component_name = Left(file.name, InStr(file.name, ".") - 1)
252-
253-
If Not componentExists(vbaProject, component_name) Then
254-
'Create a sheet and component to import this into
255-
'...skipping that for now
256-
Exit Sub
257-
End If
252+
Dim componentName As String
253+
componentName = Left(file.name, InStr(file.name, ".") - 1)
258254
Dim c As VBComponent
259-
Set c = vbaProject.VBComponents(component_name)
260-
Debug.Print "Importing lines from " & component_name & " into component " & c.name
255+
If Not componentExists(vbaProject, componentName) Then
256+
'Create a sheet to import this code into. We cannot set the ws.codeName property which is read-only,
257+
' instead we set its vbComponent.name which leads to the same result.
258+
Dim addedSheetCodeName As String
259+
addedSheetCodeName = addSheetToWorkbook(componentName, vbaProject.fileName)
260+
Set c = vbaProject.VBComponents(addedSheetCodeName)
261+
c.name = componentName
262+
End If
263+
Set c = vbaProject.VBComponents(componentName)
264+
Debug.Print "Importing lines from " & componentName & " into component " & c.name
261265
c.CodeModule.DeleteLines 1, c.CodeModule.CountOfLines
262266
c.CodeModule.AddFromFile file.Path
263267
End Sub
264268

265269

266270
Public Function componentExists(ByRef proj As VBProject, name As String) As Boolean
267271
On Error GoTo doesnt
268-
269272
Dim c As VBComponent
270273
Set c = proj.VBComponents(name)
271274
componentExists = True
272275
Exit Function
273276
doesnt:
274277
componentExists = False
275278
End Function
279+
280+
281+
' Returns a reference to the workbook. Opens it if it is not already opened.
282+
' Raises error if the file cannot be found.
283+
Public Function openWorkbook(ByVal filePath As String) As Workbook
284+
Dim wb As Workbook
285+
Dim fileName As String
286+
fileName = Dir(filePath)
287+
On Error Resume Next
288+
Set wb = Workbooks(fileName)
289+
On Error GoTo 0
290+
If wb Is Nothing Then
291+
Set wb = Workbooks.Open(filePath) 'can raise error
292+
End If
293+
Set openWorkbook = wb
294+
End Function
295+
296+
' Returns the CodeName of the added sheet or an empty String if the workbook could not be opened.
297+
Public Function addSheetToWorkbook(sheetName As String, workbookFilePath As String) As String
298+
Dim wb As Workbook
299+
On Error Resume Next 'can throw if given path does not exist
300+
Set wb = openWorkbook(workbookFilePath)
301+
On Error GoTo 0
302+
If Not wb Is Nothing Then
303+
Dim ws As Worksheet
304+
Set ws = wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count))
305+
ws.name = sheetName
306+
'ws.CodeName = sheetName: cannot assign to read only property
307+
Debug.Print "Sheet added " & sheetName
308+
addSheetToWorkbook = ws.CodeName
309+
Else
310+
Debug.Print "Skipping file " & sheetName & ". Could not open workbook " & workbookFilePath
311+
addSheetToWorkbook = ""
312+
End If
313+
End Function

0 commit comments

Comments
 (0)