@@ -21,6 +21,7 @@ Option Explicit
21
21
22
22
Private Const IMPORT_DELAY As String = "00:00:03"
23
23
24
+ 'We need to make these variables public such that they can be read by application.ontime
24
25
Private componentsToImport As Dictionary 'Key = componentName, Value = componentFilePath
25
26
Private vbaProjectToImport As VBProject
26
27
@@ -133,14 +134,16 @@ End Sub
133
134
Public Sub importVbaCode (vbaProject As VBProject )
134
135
'find project files
135
136
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
137
141
If vbProjectFileName = "" Then
138
142
'In this case it is a new workbook, we skip it
139
143
Debug.Print "No file name for project " & vbaProject.name & ", skipping"
140
144
Exit Sub
141
145
End If
142
146
143
-
144
147
Dim fso As New Scripting.FileSystemObject
145
148
Dim projDir As String
146
149
projDir = fso.GetParentFolderName(vbProjectFileName)
@@ -181,7 +184,7 @@ Public Sub importVbaCode(vbaProject As VBProject)
181
184
'Then import them
182
185
Debug.Print "Invoking Application.Ontime with delay " & IMPORT_DELAY ' To prevent duplicate modules, like MyClass1 etc.
183
186
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
185
188
End Sub
186
189
187
190
Private Sub checkHowToImport (file As Object )
@@ -200,7 +203,7 @@ Private Sub checkHowToImport(file As Object)
200
203
Case ".cls" ' 10 == Len(".sheet.cls")
201
204
If Len(fileName) > 10 And Right(fileName, 10 ) = ".sheet.cls" Then
202
205
'import lines into sheet
203
- 'TODO importLines vbaProject , file
206
+ importLines vbaProjectToImport , file
204
207
Else
205
208
'importComponent vbaProject, file
206
209
componentsToImport.Add componentName, file.Path
@@ -238,7 +241,6 @@ Public Sub importComponents()
238
241
Set vbaProjectToImport = Nothing
239
242
End Sub
240
243
241
-
242
244
' Assumes any component with same name has already been removed
243
245
Private Sub importComponent (vbaProject As VBProject , filePath As String )
244
246
Debug.Print "Importing component from " & filePath
@@ -247,29 +249,65 @@ End Sub
247
249
248
250
249
251
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 )
258
254
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
261
265
c.CodeModule.DeleteLines 1 , c.CodeModule.CountOfLines
262
266
c.CodeModule.AddFromFile file.Path
263
267
End Sub
264
268
265
269
266
270
Public Function componentExists (ByRef proj As VBProject , name As String ) As Boolean
267
271
On Error GoTo doesnt
268
-
269
272
Dim c As VBComponent
270
273
Set c = proj.VBComponents(name)
271
274
componentExists = True
272
275
Exit Function
273
276
doesnt:
274
277
componentExists = False
275
278
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