Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Problème d'écriture vers Excel 2003

1 réponse
Avatar
donald pelletier
Bonjour,

J'essaie d'écrire dans une feuille Excel à partir de VB.net. J'utilise
Visual Studio Architect Entreprise et Excel 2003 et j'obtiens le message
suivant:



Object reference not set to to an instance of an object.

Voici le code VB:

Quelqu'un peut-il me donner une indice de la cause du problème.


Merci de votre aide

Imports Microsoft.Office.Core

Public Class Form1

Inherits System.Windows.Forms.Form

.

.

.

' Open Application

Dim objApp As Excel.Application

Dim objBook As Excel._Workbook

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim objBooks As Excel.Workbooks

Dim objSheets As Excel.Sheets

Dim objSheet As Excel._Worksheet

Dim range As Excel.Range

If chkNieuw.Checked = True Then

'New File

Try

ObjApp = New Excel.Application

objBooks = ObjApp.Workbooks

objBook = objBooks.Add

objSheets = objBook.Worksheets

objSheet = objSheets(1)

'Start Cell

range = objSheet.Range("A1", Reflection.Missing.Value)

range = range.Resize(20, 20)

'Fill in

ObjApp.Application.Cells(1, 1).Value = "Name"

ObjApp.Application.Cells(2, 1).Value = "First Name"

ObjApp.Application.Cells(3, 1).Value = "City"

ObjApp.Application.Cells(1, 2).Value = txtName.Text

ObjApp.Application.Cells(2, 2).Value = txtFirst.Text

ObjApp.Application.Cells(3, 2).Value = txtCity.Text

'Visible

ObjApp.Visible = True

ObjApp.UserControl = True

Catch ex As Exception

MsgBox("Programerror:" & ex.Message)

End Try

Else

Try

'A excisted file -> Fill in

Dim ObjApp As New Excel.Application

Dim ObjWorkSheet As Excel.Worksheet

'Open File

'YOU NEED TO CREATE THIS FILE BEFORE YOU CAN RUN THIS

ObjApp.Workbooks.Add()

ObjApp.Workbooks.Open("C:\Test.xls")

Dim ObjW = ObjApp.ActiveWorkbook

ObjWorkSheet = ObjApp.Worksheets(1)

ObjWorkSheet.Application.Cells(1, 1).Value = "Name"

ObjWorkSheet.Application.Cells(2, 1).Value = "First Name"

ObjWorkSheet.Application.Cells(3, 1).Value = "City"

ObjWorkSheet.Application.Cells(1, 2).Value = txtName.Text

ObjWorkSheet.Application.Cells(2, 2).Value = txtFirst.Text

ObjWorkSheet.Application.Cells(3, 2).Value = txtCity.Text

'Visible

ObjApp.Visible = True

ObjApp.UserControl = True

Catch ex As Exception

MsgBox("Programerror:" & ex.Message)

End Try

End If

'Close

'YOU NEED TO DO THIS

'If you dont do this, i can swear you, if you shutdown youre computer

'you will have a lot of errors about Excel files that are open :)

range = Nothing

objSheet = Nothing

objSheets = Nothing

objBooks = Nothing

End Sub

1 réponse

Avatar
Jacques93
Bonjour donald pelletier,
donald pelletier a écrit :
Bonjour,

J'essaie d'écrire dans une feuille Excel à partir de VB.net. J'utilise
Visual Studio Architect Entreprise et Excel 2003 et j'obtiens le message
suivant:



Object reference not set to to an instance of an object.




Le forum dédié à .Net est :

microsoft.public.fr.dotnet.vb

tu auras peut être plus de chance d'y avoir des réponses. Ici, on en est
resté à VB6 :-)

--
Cordialement,

Jacques.