Visual Basic.NET - Importar datos de un archivo .txt a un nuevo libro mediante un windows form

 
Vista:

Importar datos de un archivo .txt a un nuevo libro mediante un windows form

Publicado por Diego Díaz Arriagada (1 intervención) el 18/09/2017 23:16:05
Hola, primero que todo mencionar que programo en Visual Studio 2017, Excel 2013, ahora el codigo que llevo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Imports Microsoft.Office.Core
Imports Microsoft.Office.Interop
Imports System.Data
Imports System.IO
Imports Microsoft.VisualBasic
 
Public Class Inicio
 
    Private ExcelApp As New Excel.Application
    Private ExcelLibro As Excel.Workbook = ExcelApp.Workbooks.Add
    Private ExcelHoja As Excel.Worksheet
    Private ExcelRange As Excel.Range
    Private ExcelQuerytable As Excel.QueryTables
 
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim saveFileDialog1 As New SaveFileDialog
        saveFileDialog1.Title = "Seleccione la Ubicación"
        saveFileDialog1.DefaultExt = "*.cvs"
        saveFileDialog1.FileName = "CENTRAL_UNIDAD_YYYYMMDD_HHMM"
        saveFileDialog1.Filter = "Archivos de Excel (*.csv)|*.csv"
        If saveFileDialog1.ShowDialog = DialogResult.OK Then
            ExcelLibro.SaveAs(saveFileDialog1.FileName, 6)
            MsgBox("El archivo se creo en la ubicación" & vbCrLf & saveFileDialog1.FileName, MsgBoxStyle.Information, "Información")
            ExcelApp.Quit()
            ExcelLibro = Nothing
            ExcelApp = Nothing
            Button2.Enabled = False
        Else
            MsgBox("Debe seleccionar una ubicación", MsgBoxStyle.Critical, "Información")
        End If
    End Sub
 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim openFileDialog1 As New OpenFileDialog
        openFileDialog1.Filter = "Text files |*.txt"
        If openFileDialog1.ShowDialog = DialogResult.OK Then
            ExcelHoja = ExcelLibro.Sheets(1)
            ExcelRange = ExcelHoja.Range("a1")
            Dim ubicacion As String = openFileDialog1.FileName
            ExcelQuerytable = ExcelHoja.QueryTables.Add(ubicacion, ExcelRange)
            Button1.Enabled = False
            Button2.Enabled = True
        End If
    End Sub
    Private Sub Inicio_Closed(sender As Object, e As EventArgs) Handles Me.Closed
        If Button2.Enabled = True Then
            ExcelApp.Quit()
            ExcelLibro = Nothing
            ExcelApp = Nothing
        End If
    End Sub
    Private Sub Inicio_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ExcelApp.DisplayAlerts = False
    End Sub
End Class

El problema es {"Exception from HRESULT: 0x800A03EC"} y hace referencia a la linea que esta subrayada, la verdad estoy cansado he probado de todo, ya no doy más, necesito su ayuda gracias, desde ya
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder