Solved

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

Posted on 2013-01-28
3
1,085 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
3 Comments
 
LVL 43

Expert Comment

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

Accepted Solution

by:
peter57r earned 500 total points
Comment Utility
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
Comment Utility
Thanks, this is exacly what I needed.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now