I want to be able to save a copy of an Excel workbook (after adding some data in some cells) as the name of the variable ClientName within a ForEachLoop container using the Script Task code below.
I have some code lifted from Stack OVerflow which already populates specific cells within an Excel Workbook template but I'm trying to save each Workbook on each iteration with the ClientName variable e.g. ClientName.xlsx
Public Sub Main()
Dim ClientName As String
Dim ClientAddress As String
Dim CurrentDate As Date
Dim m_XlApp = New Excel.Application
Dim m_xlWrkbs As Excel.Workbooks = CType(m_XlApp.Workbooks, Excel.Workbooks)
Dim m_xlWrkb As Excel.Workbook
m_xlWrkb = m_xlWrkbs.Open("C:\Users\UserAZ\Documents\TemplateStatement.xlsx")
Dim m_XlWrkSheet As Excel.Worksheet = CType(m_xlWrkb.Worksheets(1), Excel.Worksheet)
'ClientName = (Dts.Variables("User::strClientName").Value).ToString()
'MsgBox(ClientName)
'ClientAddress = (Dts.Variables("User::strClientAddress").Value).ToString()
'MsgBox(ClientAddress)
'CurrentDate = Today
'MsgBox(CurrentDate)
m_XlWrkSheet.Range("A1").Value = ClientName
m_XlWrkSheet.Range("A2").Value = ClientAddress
m_XlWrkSheet.Range("B4").Value = CurrentDate
'm_xlWrkb.Save()
m_xlWrkb.SaveCopyAs("C:\Users\UserAZ\Documents\" & ClientName & ".xlsx")
m_xlWrkb.Close(SaveChanges:=True)
Marshal.ReleaseComObject(m_xlWrkb)
Marshal.ReleaseComObject(m_xlWrkbs)
m_XlApp.Quit()
Marshal.ReleaseComObject(m_XlApp)
Dts.TaskResult = ScriptResults.Success
End Sub
I was expecting that after each iteration in the For Loop Container the script task would save the copy of the template using the variable ClientName and move onto the next iteration and repeat.
However, it throws up a dialogue box asking if I wan to save a copy of TemplateStatement.xlsx. If I press don't save it actually moves onto the next iteration. But the whole point is to have it run without any manual intervention.
I'm not familar with VB.Net.