I have a process in a vs 2012 project that exports data to a spreadsheet. First it drops a sheet (i.e. table) in the spreadsheet, creates the sheet, and then inserts data into it. The first time the process is run it works fine. But if I try it again I get an error that says 'cannot expand named range'. We recently converted to Office 2012 so I am assuming it is some type of issue related to that but have no clue what it could be. I think something is not working correctly when it drops the table because if I go in and manually clear the data out of htat sheet and then re-run it is fine. Here is my code:
Dim SQL_DeleteSheet = "DROP TABLE [FallsDataRaw]" Dim ExcelCommand_DeleteSheet As New System.Data.OleDb.OleDbCommand(SQL_DeleteSheet, ExcelConnection) ExcelCommand_DeleteSheet.ExecuteNonQuery() ' Create FallsDataRaw sheet Dim SQL_CreateSheet = "CREATE TABLE [FallsDataRaw]([Unit] varchar(3), [RoomBed] varchar(11), [Name] varchar(34), [CareType] char(3), [AdmitDate] char(10), [AssessFall] char(1), " & _"[PostFallAsmntComp] char(1), [DateofFall] char(10), [FallWithin30Days] char(1), [Dayofweek] char(1), [ShiftCode] char(1), " & _"[FallTime] char(8), [TimeCode] char(2), [LocationCode] char(2), [ActPriortoFall] char(1), [Injury] char(1), [InjuryType] char(1)," & _"[TXLocation] char(2), [InjuryDetail] varchar(100), [HistoryofFalls] char(1), [AcuteMedProb] char(1), [ChronicMedProb] char(1), " & _"[Medications] char(1), [FunctionalStat] char(1), [SensoryStat] char(1), [PsycStat] char(1), [EnvironmentIssues] char(1), [Restraint] char(1), [Comments] varchar(200))" Dim ExcelCommand_CreateSheet As New System.Data.OleDb.OleDbCommand(SQL_CreateSheet, ExcelConnection) ExcelCommand_CreateSheet.ExecuteNonQuery() ' Insert data into FallsDataRaw sheet Dim SQL_Insert = "INSERT INTO [FallsDataRaw] (Unit, RoomBed, Name, CareType, AdmitDate, AssessFall, PostFallAsmntComp, DateofFall, " & _"FallWithin30Days, Dayofweek, ShiftCode, FallTime, TimeCode, LocationCode, ActPriortoFall, Injury, InjuryType, " & _"TXLocation, InjuryDetail, HistoryofFalls, AcuteMedProb, ChronicMedProb, Medications, FunctionalStat, SensoryStat," & _"PsycStat, EnvironmentIssues, Restraint, Comments) SELECT Unit, RoomBed, Name, CareType," & _"Format(AdmitDate, 'mm/dd/yyyy') as AdmitDate, AssessFall, PostFallAsmntComp, Format(DateofFall, 'mm/dd/yyyy') as DateofFall," & _"FallWithin30Days, Dayofweek, ShiftCode, FallTime, TimeCode, LocationCode, ActPriortoFall, Injury, InjuryType," & _"InjuryOutcome as TXLocation, InjuryDetail, HistoryofFalls, AcuteMedProb, ChronicMedProb, Medications, FunctionalStat," & _"SensoryStat, PsycStat, EnvironmentIssues, Restraint, Comments FROM [QryDataforSpreadsheet]" & _"IN '' [ODBC;Driver={SQL Server};Server=server1;Database=FallsData;Trusted_Connection=yes]" & _"WHERE (FacN ='" & FacNum & "') and (DateofFallMonth = " & ParMonth & ") and (DateofFallYear = " & ParYear & ") and (FallTime > '""');" Dim ExcelCommand As New System.Data.OleDb.OleDbCommand(SQL_Insert, ExcelConnection) ExcelCommand.Parameters.AddWithValue("Param1", ParFacNum) ExcelCommand.Parameters.AddWithValue("Param2", ParMonth) ExcelCommand.Parameters.AddWithValue("Param3", ParYear) ExcelCommand.ExecuteNonQuery()