Hi, here you go. There is no need to use "excelApp" when you're using "GoExcel". Read the descriptions in code and let me know if you'll want me to code the numbering for you.
Section = Left(Parameter("SECTION"),1) 'Type (mechanical, Fluid)
Struct = Left(Parameter("STRUCTURE"),1) 'Assembly or Component
'define the file to create/open
'No need to have it maped as "Z:", you can use the absolute server adress to server, e.g. "\\dc\...\PN.xlsx"
'Ask your IT if you don't know the server adress
Worksheet = "Z:\CAD\DESIGN ENVIRONMENT\iLogic\PN.xlsx"
Sheetno = ("Sheet1")
'check for existing file
If Dir(Worksheet) <> "" Then
'workbook exists, open it
GoExcel.Open(Worksheet, Sheetno)
' Define Range. It doesn't matter now, it will fill the rows up to infinity now
RowStart = 2
RowEnd = 100
'Find the first empty cell
For countA = RowStart To (RowEnd * 0.01) + RowStart
'If it's not blank jump to cell + 100. E.g. search cell C2 and then C102 etc.
If Not String.IsNullOrEmpty(GoExcel.CellValue("C" & RowEnd)) Then
'Add another 100 to searched cells
RowEnd = RowEnd + 100
Else
'Else if it's empty exit loop
Exit For
End If
Next
'We want to search for empty rows just in last 100 rows, but we don't want to search in rows under "RowStart"
If RowStart > (RowEnd - 100) Then RowS2 = RowStart Else RowS2 = (RowEnd - 100)
'Find the first empty cell
For RowPN = RowS2 To RowEnd
'If it's blank count it
If String.IsNullOrEmpty(GoExcel.CellValue("C" & RowPN)) Then
'Row Number is equal to the current row
rowN = RowPN
'Exit loop
Exit For
End If
Next
GoExcel.CellValue("A1") = Section & Struct
iProperties.Value("Project", "Revision Number") = "A"
OldPartNumber = iProperties.Value("Project", "Part Number")
'Show user the next row number which is blank
MessageBox.Show("Inserting Data on Row " & rowN, "Updating Database...")
'Store Old part Number
NewPartNumber = GoExcel.CellValue("B1")
'Update iProperties
iProperties.Value("Project", "Part Number") = NewPartNumber
Filename = GoExcel.CellValue("B1") & iProperties.Value("Project", "Revision Number")& "_" & iProperties.Value("Project", "Description")
iProperties.Value("Summary", "Title") = Filename
GoExcel.CellValue("C" & RowN) = GoExcel.CellValue("B1")
GoExcel.CellValue("D" & RowN) = iProperties.Value("Project", "Revision Number")
GoExcel.CellValue("E" & RowN) = iProperties.Value("Project", "Description")
GoExcel.CellValue("F" & RowN) = Filename
iProperties.Value("Project", "Designer") = iProperties.Value("Summary", "Author")
MessageBox.Show("Your Unique Part Number is " & Filename, "Updating Database...")
'define the active document
oDoc = ThisDoc.Document
'create a file dialog box
Dim oFileDlg2 As inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg2)
'check file type and set dialog filter
If oDoc.DocumentType = kPartDocumentObject Then
oFileDlg2.Filter = "Autodesk Inventor Part Files (*.ipt)|*.ipt"
Else If oDoc.DocumentType = kAssemblyDocumentObject Then
oFileDlg2.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
Else If oDoc.DocumentType = kDrawingDocumentObject Then
oFileDlg2.Filter = "Autodesk Inventor Drawing Files (*.idw)|*.idw"
End If
'set the directory to open the dialog at
oFileDlg2.InitialDirectory = ThisDoc.WorkspacePath()
'set the file name string to use in the input box
oFileDlg2.FileName = Filename
'work with an error created by the user backing out of the save
oFileDlg2.CancelError = True
On Error Resume Next
'specify the file dialog as a save dialog (rather than a open dialog)
oFileDlg2.ShowSave()
'Catch an empty String in the imput
If Err.Number <> 0 Then
MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled")
'Enter Code which removes last entry from Spreadsheet
'We already have the "RowN" so there is no need to look for it again
iProperties.Value("Project", "Part Number") = OldPartNumber
iProperties.Value("Summary", "Title") = iProperties.Value("Project", "Description")
MessageBox.Show("No File Saved." &vbLf & "Removing Last Entry From Database...")
GoExcel.CellValue("C" & RowN) = ""
GoExcel.CellValue("D" & RowN) = ""
GoExcel.CellValue("E" & RowN) = ""
GoExcel.CellValue("F" & RowN) = ""
ElseIf oFileDlg2.FileName <> "" Then
MyFile = oFileDlg2.FileName
'save the file
oDoc.SaveAs(MyFile, False) 'True = Save As Copy & False = Save As
'I dont know what does your part numbering look like, so you have to modify it here...
'I can give you some other numbering examples if you'll want me to, or make it exactly for your own
Dim NewB1 As String = GoExcel.CellValue("B1")
Dim LenNr As Integer = 4 'sets how many symbols are defining the numbering, e.g. PAX0001 = last 4 symbols
Dim NewNr As String = Val(Right(NewB1, LenNr)) + 1 'New number will be highter by 1 so e.g. PAX0001 > PAX0002
'as we extracted the number from the numbering symbols, there are probably missing all the zeros we had there, so we have to place them back
If Len(NewNr) < 2 Then
NewNr = "000" & NewNr
Else If Len(NewNr) < 3 Then
NewNr = "00" & NewNr
Else If Len(NewNr) < 4 Then
NewNr = "0" & NewNr
End If
GoExcel.CellValue("B1") = Left(NewB1, Len(NewB1) - LenNr) & NewNr
End If
GoExcel.Save
GoExcel.Close
Else
MessageBox.Show("Excel Failed to Open." &vbLf & "Ensure Engineering Root is mapped to Z:/", "Error Handler")
End If
iLogicVb.UpdateWhenDone = True
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike
"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods