Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations JAE on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

what's the correct syntax to loop through Excel codename uisng NX Journal?

Status
Not open for further replies.

godpaul

Automotive
Joined
May 4, 2014
Messages
119
Location
US
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
 
my goal is to loop through each excel sheet to get its codeName instead of the worksheet name, but something wrong with my syntax

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
 
How about this:

Code:
For i = 1 To objWorkbook.worksheets.count
    MsgBox(objWorkbook.worksheets(i).Codename)
Next

www.nxjournaling.com
 
Oh my Gee,
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 :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top