Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Trouble saving Excel file as tab delimited text file using .vbs

Posted on 2005-05-05
4
Medium Priority
?
1,432 Views
Last Modified: 2009-03-03
I have written a .vbs that will open an Excel file and reformat the raw data in the file (copy & paste cells etc.).  I need to then save the sheet as a tab delimited .txt file to be read into another application.  I keep getting an error, here is some sample code:

Set objExcel = createobject("Excel.application")
objExcel.Visible = false
objExcel.DisplayAlerts = False
objExcel.Workbooks.Open("C:\Sales.xls")

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'format the data
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

objExcel.ActiveWorkbook.SaveAs Filename:= "C:\MyWorkbook.txt", FileFormat:=xlText
objExcel.quit

I keep getting an error on the 'SaveAs' line.  It says "Error: Expected statement", "Code: 800A0400", "Source:  Microsoft VBScript compilation error".  It works fine if I use "objExcel.ActiveSheet.SaveAs ("C:\MyWorkbook.txt")", but it saves in a different type of 'Excel Text' because it is unreadable in any other applications (i.e. - notepad).  This has been driving me crazy for the past few days, and any help is greatly appreciated.  Let me know if you need anything else.
Thanks in advance.
0
Comment
Question by:RDeWolfe
  • 2
4 Comments
 
LVL 26

Expert Comment

by:EDDYKT
ID: 13939129
Try this

objExcel.Workbooks(("C:\Sales.xls").SaveAs Filename:= "C:\MyWorkbook.txt", FileFormat:=xlText
0
 
LVL 35

Accepted Solution

by:
mvidas earned 2000 total points
ID: 13939154
Hi,

VBScript doesn't understand the constant "xlText", so is probably treating that as a 0.  xlText has the value of -4158, so you could probably use
 objExcel.ActiveWorkbook.SaveAs Filename:="C:\MyWorkbook.txt", FileFormat:=-4158
I've had problems in the past specifying arguments as well, and since Filename and FileFormat are the first two arguments, you could also just use
 objExcel.ActiveWorkbook.SaveAs "C:\MyWorkbook.txt", -4158

For a list of constants' values, see http://techsupt.winbatch.com/TS/T000001033F49.html#Excel or just open excel VBA and enter "debug.print xltext" in the immediate window.
Matt
0
 

Author Comment

by:RDeWolfe
ID: 13944737
EDDYKT: I tried your solution, but there is a syntax error .... missing ")"

mvidas: Thanks a lot!  It is always some little detail that has been overlooked.
0
 
LVL 35

Expert Comment

by:mvidas
ID: 13944966
When I make my vbs files I generally assume that any constant won't be understood so I use the value and would put something like  '-4158=xlText   at the end, just for future changes if necessary.  Glad to help!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

571 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