Skip to content

Commit 14472f0

Browse files
committed
Merging pull request 20. Update Build.bas
1 parent 23ee723 commit 14472f0

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

src/vbaDeveloper.xlam/Build.bas

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ Attribute VB_Name = "Build"
77
' * Microsoft Visual Basic for Applications Extensibility 5.3
88
' * Microsoft Scripting Runtime
99
' 3. Rename the project to 'vbaDeveloper'
10-
' 5. Enable programatic access to VBA:
10+
' 4. Enable programatic access to VBA:
1111
' File -> Options -> Trust Center, Trust Center Settings, -> Macros,
1212
' tick the box: 'Enable programatic access to VBA' (In excel 2010: 'Trust access to the vba project object model')
1313
' If you get 'path not found' exception in Excel 2013, include the following step:
14-
' In 'Trust Center' settings, go to 'File Block Settings' and check 'open' and/or 'save' for 'Excel 2007 and later Macro-Enabled Workbooks and Templates'.
14+
' In 'Trust Center' settings, go to 'File Block Settings' and check 'open' and/or 'save'
15+
' for 'Excel 2007 and later Macro-Enabled Workbooks and Templates'.
16+
' 5. If using a non-English version of Excel, rename your current workbook into ThisWorkbook (in VB Editor, press F4,
17+
' then under the local name for Microsoft Excel Objects, select the workbook. Set the property '(Name)' to ThisWorkbook)
1518
' 6. In VB Editor, press F4, then under Microsoft Excel Objects, select ThisWorkbook.Set the property 'IsAddin' to TRUE
1619
' 7. In VB Editor, menu File-->Save Book1; Save as vbaDeveloper.xlam in the same directory as 'src'
1720
' 8. Close excel. Open excel with a new workbook, then open the just saved vbaDeveloper.xlam

src/vbaDeveloper.xlam/Formatter.bas

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Private Const BEG_END_ELSEIF = "ElseIf"
6262
Private Const BEG_END_CASE = "Case "
6363

6464
Private Const THEN_KEYWORD = "Then"
65-
Private Const LINE_CONTINUATION = "_"
65+
Private Const LINE_CONTINUATION = " _"
6666

6767
Private Const INDENT = " "
6868

@@ -199,8 +199,9 @@ Public Sub formatCode(codePane As codeModule)
199199
Dim lineCount As Integer
200200
lineCount = codePane.CountOfLines
201201

202-
Dim indentLevel As Integer, nextLevel As Integer, levelChange As Integer
202+
Dim indentLevel As Integer, nextLevel As Integer, levelChange As Integer, isPrevLineContinuated as Boolean
203203
indentLevel = 0
204+
isPrevLineContinuated = False
204205
Dim lineNr As Integer
205206
For lineNr = 1 To lineCount
206207
Dim line As String
@@ -231,7 +232,10 @@ Public Sub formatCode(codePane As codeModule)
231232
line = indentation(indentLevel) + line
232233
indentLevel = nextLevel
233234
End If
234-
Call codePane.ReplaceLine(lineNr, line)
235+
If Not isPrevLineContinuated Then
236+
Call codePane.ReplaceLine(lineNr, line)
237+
EndIf
238+
isPrevLineContinuated = isLineContinuated(line)
235239
Next
236240
Exit Sub
237241
formatCodeError:
@@ -261,7 +265,7 @@ Private Function indentChange(ByVal line As String) As Integer
261265
Set w = vbaWords
262266

263267
If isEqual(line, ONEWORD_END_FOR) Or _
264-
isEqual(line, ONEWORD_END_LOOP) Then
268+
lineStartsWith(ONEWORD_END_LOOP, line) Then
265269
indentChange = -1
266270
GoTo hell
267271
End If
@@ -321,6 +325,13 @@ Private Function isOneLineIfStatemt(line As String) As Boolean
321325
End Function
322326

323327

328+
Private Function isLineContinuated(line As String) As Boolean
329+
Dim trimmedLine As String
330+
trimmedLine = TrimComments(line)
331+
isLineContinuated = lineEndsWith(LINE_CONTINUATION, trimmedLine)
332+
End Function
333+
334+
324335
' Trims trailing comments (and whitespace before a comment) from a line of code
325336
Private Function TrimComments(ByVal line As String) As String
326337
Dim c As Long

src/vbaDeveloper.xlam/Menu.bas

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ Public Sub createMenu()
3939
caption = projectName & " (" & Dir(project.fileName) & ")" '<- this can throw error
4040

4141
Dim exCommand As String, imCommand As String, formatCommand As String
42-
exCommand = "'Menu.exportVbProject """ & projectName & """'"
43-
imCommand = "'Menu.importVbProject """ & projectName & """'"
44-
formatCommand = "'Menu.formatVbProject """ & projectName & """'"
42+
exCommand = "'Menu.exportVbProject """ & project.fileName & """'"
43+
imCommand = "'Menu.importVbProject """ & project.fileName & """'"
44+
formatCommand = "'Menu.formatVbProject """ & project.fileName & """'"
4545

4646
addMenuItem exSubMenu, exCommand, caption
4747
addMenuItem imSubMenu, imCommand, caption
@@ -121,29 +121,28 @@ Public Sub refreshMenu()
121121
menu.createMenu
122122
End Sub
123123

124-
Public Sub exportVbProject(ByVal projectName As String)
124+
Public Sub exportVbProject(ByVal projectPath As String)
125125
On Error GoTo exportVbProject_Error
126126

127127
Dim project As VBProject
128-
Set project = Application.VBE.VBProjects(projectName)
128+
Set project = GetProjectByPath(projectPath)
129129
Build.exportVbaCode project
130130
Dim wb As Workbook
131131
Set wb = Build.openWorkbook(project.fileName)
132132
NamedRanges.exportNamedRanges wb
133133
MsgBox "Finished exporting code for: " & project.name
134134

135-
On Error GoTo 0
136135
Exit Sub
137136
exportVbProject_Error:
138137
ErrorHandling.handleError "Menu.exportVbProject"
139138
End Sub
140139

141140

142-
Public Sub importVbProject(ByVal projectName As String)
141+
Public Sub importVbProject(ByVal projectPath As String)
143142
On Error GoTo importVbProject_Error
144143

145144
Dim project As VBProject
146-
Set project = Application.VBE.VBProjects(projectName)
145+
Set project = GetProjectByPath(projectPath)
147146
Build.importVbaCode project
148147
Dim wb As Workbook
149148
Set wb = Build.openWorkbook(project.fileName)
@@ -157,11 +156,11 @@ importVbProject_Error:
157156
End Sub
158157

159158

160-
Public Sub formatVbProject(ByVal projectName As String)
159+
Public Sub formatVbProject(ByVal projectPath As String)
161160
On Error GoTo formatVbProject_Error
162161

163162
Dim project As VBProject
164-
Set project = Application.VBE.VBProjects(projectName)
163+
Set project = GetProjectByPath(projectPath)
165164
Formatter.formatProject project
166165
MsgBox "Finished formatting code for: " & project.name & vbNewLine _
167166
& vbNewLine _
@@ -250,3 +249,22 @@ Function GetFolder(InitDir As String) As String
250249
GetFolder = sItem
251250
Set fldr = Nothing
252251
End Function
252+
253+
254+
Function GetProjectByPath(ByVal projectPath As String) As VBProject
255+
'Simple search to find project by file path
256+
Dim project As VBProject
257+
For Each project In Application.VBE.VBProjects
258+
On Error GoTo skipone
259+
If UCase(project.fileName) = UCase(projectPath) Then
260+
Set GetProjectByPath = project
261+
Exit Function
262+
End If
263+
nextprj:
264+
Next project
265+
'If not found return nothing
266+
Exit Function
267+
skipone:
268+
Resume nextprj
269+
End Function
270+

src/vbaDeveloper.xlam/Test.bas

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ Public Sub testExport()
4646
Dim proj_name As String
4747
proj_name = "vbaDeveloper"
4848

49-
menu.exportVbProject proj_name
49+
Dim vbaProject As Object
50+
Set vbaProject = Application.VBE.VBProjects(proj_name)
51+
menu.exportVbProject vbaProject.fileName
5052
End Sub
5153

5254

0 commit comments

Comments
 (0)