D
deko
I have a batch process that exports from Access. The problem I'm having is
creating (and naming) a new workbook, and inserting (and naming) the
multiple worksheets.
First, creating the Workbook (see line marked with ****). How do I create a
Workbook (and give the Workbook a specified name)? For example, in the
below function, I pass in "fld", which is a path to a directory. If the
user selected "new workbook" then I need to create a new Workbook in the
given directory and name the new Workbook somehow. Do I do this with Excel
automation? fso object?
Public Function GetSubFolders(fld As Scripting.Folder) As Boolean
Dim xlapp As Excel.Application
Dim xlwkbs As Excel.Workbooks
Dim xlwkb As Excel.Workbook
Dim fldSub As Scripting.Folder
Dim fso As Scripting.FileSystemObject
Dim strMdb As String
Dim strTarget As String
Dim bytOutput As Byte
Set xlapp = New Excel.Application
Set xlwkbs = xlapp.Workbooks
bytOutput = Forms("frmMain")!fraOutput
strTarget = Forms("frmMain")!txtOutput
Select Case bytOutput
Case 1 'existing workbook
Set xlwkb = xlwkbs(strTarget)
Case 2 'new workbook
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(strTarget) Then
fso.DeleteFile (strTarget) 'delete if already exists
End if
xlwkbs.Add '************************
End Select
For Each fldSub In fld.SubFolders
strMdb = fld & "\" & fldSub.Name & "\MEAS.MDB"
If LinkTables(strMdb) Then Call CreateWorksheets(bytOutput, _
strTarget, xlapp, xlwkbs, xlwkb)
Next fldSub
GetSubFolders = True
End Function
Next, I need to insert a bunch of Worksheets. Am I going about this the
right way?
Private Function CreateWorksheets(bytOutput As Byte, strTarget As String, _
xlapp As Excel.Application, xlwkbs As Excel.Workbooks, wkb As
Excel.Workbook)
Dim xlwks As Excel.Worksheets
Dim xlwkss As Excel.Worksheets
Dim i As Byte
Select Case bytOutput
Case 1 'existing workbook
i = xlwkbs.Count - 1
xlwkss.Add After:=Worksheets(i)
Case 2 'new workbook
xlwkss.Add After:=Worksheets(i)
End Select
Set xlwks = xlwkss(i + 1)
xlwks.Name = strWksName '************
Call PopulateWorksheet 'dumps tables into wks
End Function
Thanks in advance.
creating (and naming) a new workbook, and inserting (and naming) the
multiple worksheets.
First, creating the Workbook (see line marked with ****). How do I create a
Workbook (and give the Workbook a specified name)? For example, in the
below function, I pass in "fld", which is a path to a directory. If the
user selected "new workbook" then I need to create a new Workbook in the
given directory and name the new Workbook somehow. Do I do this with Excel
automation? fso object?
Public Function GetSubFolders(fld As Scripting.Folder) As Boolean
Dim xlapp As Excel.Application
Dim xlwkbs As Excel.Workbooks
Dim xlwkb As Excel.Workbook
Dim fldSub As Scripting.Folder
Dim fso As Scripting.FileSystemObject
Dim strMdb As String
Dim strTarget As String
Dim bytOutput As Byte
Set xlapp = New Excel.Application
Set xlwkbs = xlapp.Workbooks
bytOutput = Forms("frmMain")!fraOutput
strTarget = Forms("frmMain")!txtOutput
Select Case bytOutput
Case 1 'existing workbook
Set xlwkb = xlwkbs(strTarget)
Case 2 'new workbook
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(strTarget) Then
fso.DeleteFile (strTarget) 'delete if already exists
End if
xlwkbs.Add '************************
End Select
For Each fldSub In fld.SubFolders
strMdb = fld & "\" & fldSub.Name & "\MEAS.MDB"
If LinkTables(strMdb) Then Call CreateWorksheets(bytOutput, _
strTarget, xlapp, xlwkbs, xlwkb)
Next fldSub
GetSubFolders = True
End Function
Next, I need to insert a bunch of Worksheets. Am I going about this the
right way?
Private Function CreateWorksheets(bytOutput As Byte, strTarget As String, _
xlapp As Excel.Application, xlwkbs As Excel.Workbooks, wkb As
Excel.Workbook)
Dim xlwks As Excel.Worksheets
Dim xlwkss As Excel.Worksheets
Dim i As Byte
Select Case bytOutput
Case 1 'existing workbook
i = xlwkbs.Count - 1
xlwkss.Add After:=Worksheets(i)
Case 2 'new workbook
xlwkss.Add After:=Worksheets(i)
End Select
Set xlwks = xlwkss(i + 1)
xlwks.Name = strWksName '************
Call PopulateWorksheet 'dumps tables into wks
End Function
Thanks in advance.