Polynom3000
Mechanical
- Apr 30, 2014
- 21
Hello community,
My goal is to import a few cells from a MS Excel file to a vb.net application running inside NX.
Main steps:
1) Define the file path of the excel file
2) Click a read/import-button
3) Values of certain cells (only numbers) are imported and related to respective NumericUpDown elements.
4) Do manipulations inside NX with the imported values
The advantage with the NumericUpDown elements is that after importing the values the user can see the numbers and can still modify them if needed.
In this example I am only using one NumericUpDown element.
The code A works pretty fine inside Microsoft Visual Studio 2012. I just click on the button and the value is imported to the NumericUpDown1 element.
Further information about the code fragments:
Post #7 (Posted 21 July 2011 - 08:05 AM)
I added the standard NX libraries references in Studio and additionally the Excel library: Project -> Reference -> COM: Microsoft Excel 14.0 Object Library
In order to paste the code into the Journal Editor (or load a vb file), I compiled the whole project as shown below and saved it as project.vb. Line 16 is responsible for the Excel reference, but there might by something wrong with it? The program does not start, I get 3 journal compile errors:
Type Microsoft.Office.Interop.Excel.Application is not defined
Type Microsoft.Office.Interop.Excel.Workbook is not defined
Type Microsoft.Office.Interop.Excel.Worksheet is not defined
System: Windows 7 Prof, 64bit
NX: 8.0.3.4
Thanks for any help!
Best regards,
Polynom3000
My goal is to import a few cells from a MS Excel file to a vb.net application running inside NX.
Main steps:
1) Define the file path of the excel file
2) Click a read/import-button
3) Values of certain cells (only numbers) are imported and related to respective NumericUpDown elements.
4) Do manipulations inside NX with the imported values
The advantage with the NumericUpDown elements is that after importing the values the user can see the numbers and can still modify them if needed.
In this example I am only using one NumericUpDown element.
The code A works pretty fine inside Microsoft Visual Studio 2012. I just click on the button and the value is imported to the NumericUpDown1 element.
Code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim excel As New Microsoft.Office.Interop.Excel.Application()
Dim wb As Microsoft.Office.Interop.Excel.Workbook = excel.Workbooks.Open("C:\Test\ExcelTest.xlsx")
Dim ws As Microsoft.Office.Interop.Excel.Worksheet = TryCast(excel.ActiveSheet, Microsoft.Office.Interop.Excel.Worksheet)
NumericUpDown1.Value = ws.Cells(1, 4).Value2
wb.Close(True, Type.Missing, Type.Missing)
excel.Quit()
End Sub
End Class
Further information about the code fragments:
Post #7 (Posted 21 July 2011 - 08:05 AM)
I added the standard NX libraries references in Studio and additionally the Excel library: Project -> Reference -> COM: Microsoft Excel 14.0 Object Library
In order to paste the code into the Journal Editor (or load a vb file), I compiled the whole project as shown below and saved it as project.vb. Line 16 is responsible for the Excel reference, but there might by something wrong with it? The program does not start, I get 3 journal compile errors:
Type Microsoft.Office.Interop.Excel.Application is not defined
Type Microsoft.Office.Interop.Excel.Workbook is not defined
Type Microsoft.Office.Interop.Excel.Worksheet is not defined
System: Windows 7 Prof, 64bit
NX: 8.0.3.4
Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpenUI
Imports NXOpen.UF
Imports NXOpen.Selection
Imports System.IO
Imports System.Collections.Generic
Imports NXOpen.Drawings
Imports System.Windows.Forms
Imports System.Math
Imports NXOpen.Utilities
Imports NXOpen.Features
Imports Microsoft.Office.Interop.Excel
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Form1
Inherits System.Windows.Forms.Form
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
Public Shared Sub Main()
Application.Run(New Form1)
End Sub
Public Sub New()
Me.InitializeComponent()
End Sub
'=#=#=#=#=#=#=#=#=#=#CODE<>
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim excel As New Microsoft.Office.Interop.Excel.Application()
Dim wb As Microsoft.Office.Interop.Excel.Workbook = excel.Workbooks.Open("C:\Test\ExcelTest.xlsx")
Dim ws As Microsoft.Office.Interop.Excel.Worksheet = TryCast(excel.ActiveSheet, Microsoft.Office.Interop.Excel.Worksheet)
NumericUpDown1.Value = ws.Cells(1, 4).Value2
wb.Close(True, Type.Missing, Type.Missing)
excel.Quit()
End Sub
'=#=#=#=#=#=#=#=#=#=#CODE</>
Private components As System.ComponentModel.IContainer
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button()
Me.NumericUpDown1 = New System.Windows.Forms.NumericUpDown()
CType(Me.NumericUpDown1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(101, 85)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
Me.Button1.UseVisualStyleBackColor = True
'
'NumericUpDown1
'
Me.NumericUpDown1.DecimalPlaces = 1
Me.NumericUpDown1.Location = New System.Drawing.Point(85, 114)
Me.NumericUpDown1.Maximum = New Decimal(New Integer() {100000, 0, 0, 0})
Me.NumericUpDown1.Name = "NumericUpDown1"
Me.NumericUpDown1.Size = New System.Drawing.Size(120, 20)
Me.NumericUpDown1.TabIndex = 1
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 262)
Me.Controls.Add(Me.NumericUpDown1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.NumericUpDown1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents NumericUpDown1 As System.Windows.Forms.NumericUpDown
End Class
Thanks for any help!
Best regards,
Polynom3000