Advertisement

05.27.2008 at 09:16AM PDT, ID: 23435347
[x]
Attachment Details

Saving Text as XLS

Asked by RalphOrtiz in VB Objects, Visual Basic Programming, VB Script

Tags: Visual Basic 6.0

I'm trying to import a text file (Working Well) but when I go to save it I get "Object variable or With block variable not set"

Private Function XLSConvertTEXT(strSource As String, strTarget As String)

    'Create a new instance of Excel
    Dim oExcel As Object
    Dim oBook As Object
    Dim oSheet As Object
    Dim saveFile As Variant
   
    Set oExcel = CreateObject("Excel.Application")
    Set oBook = oExcel.ActiveWorkbook
    'Open the text file
    'Set oBook = oExcel.workbooks.Open(strSource)
    'Batch Method
    'The Array(X,Y) in field info represents the column and data type
    'Where X represents the column, Y represents the data type
    '1 = General; 2 = Text; 3 = Date (MMDDYY)
    oExcel.Visible = True
    oExcel.Workbooks.OpenText strSource, 437, 1, 1, 1, False, True, False, False, False, True, ","
    'FieldInfo:=Array(Array(1, 1), Array(2, 1), _
    'Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), _
    'Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 1), Array(15, 1), Array( _
    '16, 1)), TrailingMinusNumbers:=True
    oExcel.Columns.AutoFit
'I tried both with the Excel object and with the Book object
    oBook.SaveAs strTarget, 1
    oExcel.SaveAs strTarget, 1
    'oBook.SaveAs fileName:=Left(strTarget, Len(strTarget) - 3) & "xls", FileFormat:=43, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
    oExcel.ActiveWorkbook.Close
    oExcel.Application.SaveAs fileName:=strTarget, FileFormat:=43
    oBook = Nothing
    oExcel.Quit
    oExcel = Nothing

End FunctionStart Free Trial
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:
Private Function XLSConvertTEXT(strSource As String, strTarget As String)
 
    'Create a new instance of Excel
    Dim oExcel As Object
    Dim oBook As Object
    Dim oSheet As Object
    Dim saveFile As Variant
    
    Set oExcel = CreateObject("Excel.Application")
    Set oBook = oExcel.ActiveWorkbook
    'Open the text file
    'Set oBook = oExcel.workbooks.Open(strSource)
    'Batch Method
    'The Array(X,Y) in field info represents the column and data type
    'Where X represents the column, Y represents the data type
    '1 = General; 2 = Text; 3 = Date (MMDDYY)
    oExcel.Visible = True
    oExcel.Workbooks.OpenText strSource, 437, 1, 1, 1, False, True, False, False, False, True, ","
    'FieldInfo:=Array(Array(1, 1), Array(2, 1), _
    'Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), _
    'Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 1), Array(15, 1), Array( _
    '16, 1)), TrailingMinusNumbers:=True
    oExcel.Columns.AutoFit
    oBook.SaveAs strTarget, 1
    'oBook.SaveAs fileName:=Left(strTarget, Len(strTarget) - 3) & "xls", FileFormat:=43, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
    oExcel.ActiveWorkbook.Close
    oExcel.Application.SaveAs fileName:=strTarget, FileFormat:=43
    oBook = Nothing
    oExcel.Quit
    oExcel = Nothing
 
End Function
[+][-]05.27.2008 at 09:40AM PDT, ID: 21653391

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: VB Objects, Visual Basic Programming, VB Script
Tags: Visual Basic 6.0
Sign Up Now!
Solution Provided By: GrahamSkan
Participating Experts: 1
Solution Grade: A
 
 
[+][-]05.27.2008 at 09:53AM PDT, ID: 21653481

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.27.2008 at 10:16AM PDT, ID: 21653682

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]05.27.2008 at 11:37AM PDT, ID: 21654404

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.27.2008 at 11:43AM PDT, ID: 21654461

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628