Writing to excel with VBA
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I've been having an error in a part that I want to write to excel. The gist is that this part is a generator for a number of parts of the same geometry of it and I want them to be reusable so I have them write to an excel file so that I can do a lookup and report the file number if the specified part already exists.
I tried the built in ILogic commands but every time I used that excel would throw out a message asking you to save the file and no amount of commands telling it to just go ahead and save or silence the program was enough to make it stop throwing those save messages on closing the excel file. So I used vba instead. The issue with the vba program is that it seems to be opening the program twice every single time. So it opens the file, then opens it again, and then reports that the file is open by someone else (yourself) and cannot be written to.
Can anyone tell me why its throwing this error?
Thank you,
Thomas Long
Sub Main() On Error GoTo ErrorHandler If Not Dir("C:\Users\" & Environ$("Username") & "\Documents\RESUME.XLW") = "" Then Kill ("C:\Users\" & Environ$("Username") & "\Documents\RESUME.XLW") ' Create the Excel application. Dim oRow As Integer Dim oParameters As Parameters Dim oLength As Parameter Dim oWidth As Parameter Dim oMaterial As Parameter Dim oType As Parameter Dim odoc As PartDocument Dim oExcel As Object Dim oBook As Object Dim objWorkbook As Object Dim oSheet As Object Dim UOM As UnitsOfMeasure Set oExcel = CreateObject("Excel.Application") Set oBook = oExcel.Workbooks Set objWorkbook = oBook.Open("N:\Mechpart\TLONG\Parts Lists\INVENTOR COVER PLATES.xlsx") Set oSheet = objWorkbook.Worksheets(1) Set odoc = ThisApplication.ActiveDocument Set oParameters = odoc.ComponentDefinition.Parameters Set UOM = odoc.UnitsOfMeasure 'Makes Excel invisibile oExcel.Visible = False Set oLength = oParameters.Item("Length") Set oWidth = oParameters.Item("Width") Set oMaterial = oParameters.Item("Material") Set oType = oParameters.Item("Type") 'Write in Data '\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ With oSheet oRow = 1 Do While .range("A" & oRow) <> "" oRow = oRow + 1 Loop .range("A" & oRow) = UOM.ConvertUnits(oLength.Value, UOM.GetTypeFromString(UOM.GetDatabaseUnitsFromExpression(oLength.Expression, oLength.Units)), oLength.Units) .range("B" & oRow) = UOM.ConvertUnits(oWidth.Value, UOM.GetTypeFromString(UOM.GetDatabaseUnitsFromExpression(oWidth.Expression, oWidth.Units)), oWidth.Units) .range("C" & oRow) = oMaterial.Value .range("D" & oRow) = oType.Value .range("E" & oRow) = odoc.File.FullFileName .range("F" & oRow) = Split(Split(odoc.File.FullFileName, "\")(UBound(Split(odoc.File.FullFileName, "\"))), ".")(0) End With '\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 'Saving and exiting Excel '\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ oExcel.Save oExcel.Quit Set oSheet = Nothing Set oBook = Nothing Set objWorkbook = Nothing Set oExcel = Nothing '\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ Exit Sub ErrorHandler: oExcel.Save oExcel.Quit Set oSheet = Nothing Set oBook = Nothing Set objWorkbook = Nothing Set oExcel = Nothing Exit Sub End Sub