what's the correct syntax to loop through Excel codename uisng NX Journal?
what's the correct syntax to loop through Excel codename uisng NX Journal?
(OP)
the following code is from nxjournaling.com
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.Annotations
Imports NXOpen.UI
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports System.Windows.Forms
Imports System.Collections.Generic
Imports System.Environment
Module replaceTabNotesusingExcelData
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession
Dim workPart As Part = theSession.Parts.Work
Dim theUI As UI = UI.GetUI()
Sub Main()
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Dim objExcel = CreateObject("Excel.Application")
If objExcel Is Nothing Then
MsgBox("Could not start Excel, this journal will now exit.", MsgBoxStyle.Critical, "Error")
Exit Sub
End If
'excel file location
Dim excelFile As String = "e:\excelsheet.xlsx"
If Not IO.File.Exists(excelFile) Then
MsgBox("Specified file not found, journal will now exit.", MsgBoxStyle.Critical, "File not found.")
Exit Sub
End If
'open excel file
Dim objWorkbook = objExcel.Workbooks.Open(excelFile)
If objWorkbook Is Nothing Then
MsgBox("Could not open Excel file, journal will now exit.", MsgBoxStyle.Critical, "Error")
Exit Sub
End If
Dim i As Integer
i = objExcel.activeworkbook.worksheets.count
lw.WriteLine(i)
'my goal is to loop through each excel sheet to get its codename instead of the worksheet name, but something wrong with my syntax
Dim y As String
Dim ws = objWorkbook.objExcel.activeworkbook.worksheets
For Each ws In objExcel.activeworkbook.worksheets
y = objExcel.Worksheet.codeName
MsgBox(y)
Next
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.Annotations
Imports NXOpen.UI
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports System.Windows.Forms
Imports System.Collections.Generic
Imports System.Environment
Module replaceTabNotesusingExcelData
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession
Dim workPart As Part = theSession.Parts.Work
Dim theUI As UI = UI.GetUI()
Sub Main()
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Dim objExcel = CreateObject("Excel.Application")
If objExcel Is Nothing Then
MsgBox("Could not start Excel, this journal will now exit.", MsgBoxStyle.Critical, "Error")
Exit Sub
End If
'excel file location
Dim excelFile As String = "e:\excelsheet.xlsx"
If Not IO.File.Exists(excelFile) Then
MsgBox("Specified file not found, journal will now exit.", MsgBoxStyle.Critical, "File not found.")
Exit Sub
End If
'open excel file
Dim objWorkbook = objExcel.Workbooks.Open(excelFile)
If objWorkbook Is Nothing Then
MsgBox("Could not open Excel file, journal will now exit.", MsgBoxStyle.Critical, "Error")
Exit Sub
End If
Dim i As Integer
i = objExcel.activeworkbook.worksheets.count
lw.WriteLine(i)
'my goal is to loop through each excel sheet to get its codename instead of the worksheet name, but something wrong with my syntax
Dim y As String
Dim ws = objWorkbook.objExcel.activeworkbook.worksheets
For Each ws In objExcel.activeworkbook.worksheets
y = objExcel.Worksheet.codeName
MsgBox(y)
Next





RE: what's the correct syntax to loop through Excel codename uisng NX Journal?
i dont want to use the sheet name as reference because user can change sheet name. then, later on, when the progrma runs, the program will throw error saying the sheet doenst exist...
i try to mimic this code written by someone
Function GetWsFromCodeName(wb As Workbook, CodeName As String) As Excel.Worksheet
Dim ws As Excel.Worksheet
For Each ws In wb.Worksheets
If ws.CodeName = CodeName Then
Set GetWsFromCodeName = ws
Exit For
End If
Next ws
End Function
but, i failed after i tried to think about possible correct syntax for NX journal
RE: what's the correct syntax to loop through Excel codename uisng NX Journal?
CODE
For i = 1 To objWorkbook.worksheets.count MsgBox(objWorkbook.worksheets(i).Codename) Nextwww.nxjournaling.com
RE: what's the correct syntax to loop through Excel codename uisng NX Journal?
Mr, cowski,
you are really my life saver...
changed from
xlsSheetName = objExcel.Worksheets(nXlsSheet).Name
to
xlsCodeName = objExcel.Worksheets(nXlsSheet).codeName
If you dont mind, i have the other question for today:)
I experimentally loop through notes for all table on drawing, my way is to match the title of the table with the codename.
however, i found this is not good, and it will be better to use the Name of the table which can be defined by rightclick > property> general tab> Name...
I look through theUfSession.Tabnot.XXXXXX
none of them give me somethign like getname() which is a property for general notes on drawing...
i know the name of the table notes can be changed, any hint?
you recommened attribute, i am trying something also :)