Dear all,
I'm trying to make a macro which fills in a cell in a specific excel workbook. I've got this working, but the only thing i would still like to do is to close the workbook again after pressing the ok button (or any other event, doesn't really matter for the question).
I've tried to accomplish this through the XLbook.close command (XLbook is dim'd as Excel.Workbook in this case), but it's giving me the following error: Object variable or With block variable not set (Error 91)
I can't really find out what's the problem in here, it's probably something stupidly easily, but i can't seem to find it?
I'll hope you guys can help me find the error in this code. The code is as following:
<p> Dim xlApp As Excel.Application</p><p> Dim XLbook As Excel.Workbook</p><p> </p><p> </p><p>Private Sub CommandButton1_Click()</p><p> MsgBox TextBox1.Value</p><p> xlApp.ActiveSheet.Range("C3").Value = TextBox1.Value</p><p> </p><p>End Sub</p><p></p><p></p><p>Private Sub CommandButton2_Click()</p><p> Set xlApp = CreateObject("Excel.Application")</p><p> Dim XLbook As Excel.Workbook</p><p> 'Set XLbook = xlApp.Workbook("X:\\Arno\\Parametrisch model\\Test.probeersels\\Excel file vullen vanuit Solidworks\\Excelfile.xlsx")</p><p> 'xlApp.Workbooks.Close FileName:="X:\\Arno\\Parametrisch model\\Test.probeersels\\Excel file vullen vanuit Solidworks\\Excelfile.xlsx"</p><p> XLbook.Close (SaveChanges = False)</p><p> Unload Me</p><p> </p><p>End Sub</p><p></p><p></p><p></p><p></p><p></p><p></p><p>Private Sub UserForm_Initialize()</p><p> Set xlApp = CreateObject("Excel.Application")</p><p> 'Dim xlApp As Excel.Application</p><p> Set xlApp = CreateObject("Excel.Application")</p><p> 'Dim Sourcebook As Excel.Workbook</p><p> </p><p> xlApp.Visible = True</p><p> Set XLbook = xlApp.Workbooks.Open("X:\\Arno\\Parametrisch model\\Test.probeersels\\Excel file vullen vanuit Solidworks\\Excelfile.xlsx", , False) 'Opens Workbook with your instance of xlBook Object</p><p> 'xlApp.Workbooks.Open FileName:="X:\\Arno\\Parametrisch model\\Test.probeersels\\Excel file vullen vanuit Solidworks\\Excelfile.xlsx"</p><p> 'xlApp.ActiveSheet.Range("C3") = TextBox1.Value</p><p>End Sub</p>The code is written in VBA.
Thank you for your help,
Best regards,
Arno
SolidworksApi macros