Skip to content

Commit 7886059

Browse files
committed
Allow complete recreation of file (VBA + XML)
1 parent fd9afca commit 7886059

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
Attribute VB_Name = "VBAandXML"
2+
Option Explicit
3+
4+
Sub exportVbaAndXMLCode()
5+
6+
Dim wb As Workbook
7+
Set wb = ActiveWorkbook
8+
9+
wb.Save
10+
11+
Call Build.exportVbaCode(wb.VBProject)
12+
Call XMLexporter.unpackXML(wb.name)
13+
14+
'Delete the VBProject bin file
15+
'On Error Resume Next
16+
'Delete files
17+
Dim FSO As New Scripting.FileSystemObject
18+
FSO.DeleteFile wb.Path & "\src\" & wb.name & "\XMLsource\xl\vbaProject.bin", True
19+
'On Error GoTo 0
20+
21+
MsgBox "Successfully exported VB code and XML content."
22+
End Sub
23+
24+
Sub ImportVbaAndXMLCode_ActiveWorkbook()
25+
26+
Call ImportVbaAndXMLCode
27+
28+
End Sub
29+
30+
Sub ImportVbaAndXMLCode(Optional ByVal FileFolderPath As String)
31+
32+
Dim wb As Workbook
33+
Dim oFileName As String, nFileName As String, nShortFileName As String, oFileFolderPath As String
34+
35+
If FileFolderPath = vbNullString Then
36+
Set wb = ActiveWorkbook
37+
oFileFolderPath = wb.Path
38+
oFileName = wb.FullName
39+
nShortFileName = wb.name
40+
wb.Close
41+
Else
42+
oFileFolderPath = Left(FileFolderPath, InStr(FileFolderPath, "\src") - 1)
43+
oFileName = Replace(FileFolderPath, "\src", "")
44+
nShortFileName = Split(FileFolderPath, "\")(UBound(Split(FileFolderPath, "\")))
45+
End If
46+
47+
Dim nwb As Workbook
48+
Dim ErrFlag As Boolean, ErrMsg As String
49+
ErrFlag = False
50+
ErrMsg = ""
51+
52+
'Ask the user to confirm
53+
Dim ireply As Variant
54+
ireply = MsgBox(prompt:="Are you sure that you want to overwrite " & nShortFileName, Buttons:=vbYesNo, title:="Decision")
55+
56+
If ireply = vbYes Then
57+
'Do nothing (Continue)
58+
ElseIf ireply = vbNo Then
59+
Exit Sub
60+
Else 'They cancelled (VbCancel)
61+
Exit Sub
62+
End If
63+
64+
Call XMLexporter.rebuildXML(oFileFolderPath, oFileFolderPath & "\src\" & nShortFileName, ErrFlag, ErrMsg, nwb)
65+
66+
If ErrFlag = True Then
67+
MsgBox (ErrMsg)
68+
Exit Sub
69+
End If
70+
71+
nFileName = nwb.FullName
72+
nwb.Close
73+
74+
Dim FSO As New Scripting.FileSystemObject
75+
Dim nFile As file, oFile As file
76+
Set oFile = FSO.GetFile(oFileName)
77+
Set nFile = FSO.GetFile(nFileName)
78+
79+
oFile.Delete
80+
nFile.name = nShortFileName
81+
82+
Set nwb = Workbooks.Open(oFileName)
83+
84+
Call Build.importVbaCode(nwb.VBProject)
85+
MsgBox "Successfully imported VB code and XML content."
86+
87+
End Sub

src/vbaDeveloper.xlam/XMLexporter.bas

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Sub test_rebuildXML()
104104

105105
End Sub
106106

107-
Public Sub rebuildXML(destinationFolder As String, containingFolderName As String, errorFlag As Boolean, errorMessage As String)
107+
Public Sub rebuildXML(destinationFolder As String, containingFolderName As String, errorFlag As Boolean, errorMessage As String, Optional ByRef NewWorkbook As Workbook)
108108

109109
'input format cleanup - containing folder name should not have trailing "\"
110110
containingFolderName = removeSlash(containingFolderName)
@@ -143,7 +143,12 @@ Public Sub rebuildXML(destinationFolder As String, containingFolderName As Strin
143143
'Rename the zipFileName to be the fileName (this effectively removes the zip file)
144144
Name zipFileName As fileName
145145
errorFlag = False
146-
146+
147+
'Open and pass the value of the workbook
148+
If NewWorkbook Is Nothing Then
149+
Set NewWorkbook = Workbooks.Open(fileName)
150+
End If
151+
147152
End Sub
148153

149154

0 commit comments

Comments
 (0)