Solved

Use Excel Automation from MSAccess VBA to Import a Text File and Save as Workbook

Posted on 2013-01-28
3
1,130 Views
Last Modified: 2013-01-28
I have a text file that has been importing via Excel with VBA and gets saved as a workbook. I want to do the same with automation in MSAccess. I am getting a compile error on the Workbooks.OpenText method: Expected function or variable. Can you help me with the syntax? I have attached the txt file to import and the Access accdb that needs to run the automation.

Here is the code that works in Excel VBA:
Private Sub ImportTextFile1()

'Import order data
Workbooks.OpenText Filename:="C:\Dev\OrderTest.txt", Origin:=936, _
        StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(4, _
        2), Array(12, 2), Array(32, 2), Array(34, 1), Array(48, 1), Array(60, 1), Array(76, 1), _
        Array(84, 1), Array(102, 2), Array(112, 2), Array(121, 2), Array(129, 2)), _
        TrailingMinusNumbers:=True

'Save the file
ActiveWorkbook.SaveAs Filename:= _
        "C:\Dev\Order.xls", _
        FileFormat:=xlNormal, Password:="", WriteResPassword:=""

End Sub
TestDB.accdb
OrderTest.txt
0
Comment
Question by:venture-it
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38825933
You have assigned excel zone. Do you need any help with excel?
0
 
LVL 77

Accepted Solution

by:
peter57r earned 500 total points
ID: 38826024
Modify as follows to use in Access...(assumes Excel is not running)

Private Sub ImportTextFile1()

Dim xlApp As Object
Const xlfixedwidth = 2    
Const xlnormal = -4143
Set xlApp = CreateObject("Excel.Application")


'Import order data
xlApp.Workbooks.OpenText FileName:="C:\Dev\OrderTest.txt", Origin:=936, _
        StartRow:=1, DataType:=xlfixedwidth, FieldInfo:=Array(Array(0, 1), Array(4, _
        2), Array(12, 2), Array(32, 2), Array(34, 1), Array(48, 1), Array(60, 1), Array(76, 1), _
        Array(84, 1), Array(102, 2), Array(112, 2), Array(121, 2), Array(129, 2)), _
        TrailingMinusNumbers:=True

'Save the file
xlApp.ActiveWorkbook.SaveAs FileName:= _
        "C:\Dev\Order.xls", _
        FileFormat:=xlnormal, Password:="", WriteResPassword:=""


xlApp.Quit
Set xlApp = Nothing

End Sub
0
 

Author Closing Comment

by:venture-it
ID: 38829371
Thanks, this is exacly what I needed.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question