MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 29th, 2002, 06:45 AM   #1
JSherrod
New Member
 
Join Date: Mar 2002
Posts: 2
Default

Is there a macro to batch save many xls files to text (prn) files?
JSherrod is offline   Reply With Quote
Old Mar 29th, 2002, 07:43 AM   #2
Ivan F Moala
MrExcel MVP
 
Ivan F Moala's Avatar
 
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
Default

Quote:
On 2002-03-29 05:45, JSherrod wrote:
Is there a macro to batch save many xls files to text (prn) files?
Hi JSherrod
What I would suggest you do is to place all
the files you want to convert into a seperate
Directory and then run this code.
Note: You will have to change the Const

Const Drive As String = "C:AData"
to the directory you want to do the batch
conversion to.

Option Explicit

Dim i As Integer
Dim sChFiles() As String
Dim iWB As Integer
Dim dCount As Double

Const Drive As String = "C:AData"

Sub Version1()

With Application.FileSearch
.NewSearch
.LookIn = Drive
.SearchSubFolders = False
.Filename = "*.xls"
.MatchTextExactly = True
.MatchAllWordForms = True
.FileType = msoFileTypeAllFiles
If .Execute() > 0 Then
ReDim sChFiles(.FoundFiles.Count)
For i = 1 To .FoundFiles.Count
sChFiles(i) = .FoundFiles(i)
Next
End If
If .FoundFiles.Count = 0 Then MsgBox "No xls files in " & Drive: End

End With

On Error GoTo ErrH
Application.ScreenUpdating = False
Application.DisplayAlerts = False
dCount = 0
For iWB = 1 To UBound(sChFiles())
Workbooks.Open Filename:=sChFiles(iWB)
With ActiveWorkbook
.SaveAs Filename:=Left(sChFiles(iWB), Len(sChFiles(iWB)) - 3) & "prn", _
FileFormat:=xlTextPrinter
.Close False
dCount = dCount + 1
End With
Next

Application.ScreenUpdating = True
MsgBox "Completed - " & dCount & " files have been converted.", 4160, "Batch Info"

Exit Sub
ErrH:
If Err.Number <> 1004 Then
MsgBox Err.Number & " :=" & Err.Description
Else
Resume Next
End If

End Sub



HTH

Ivan
Ivan F Moala is offline   Reply With Quote
Old Mar 29th, 2002, 08:39 AM   #3
JSherrod
New Member
 
Join Date: Mar 2002
Posts: 2
Default

Thanks! Worked great. Exported 718 xls files.
JSherrod is offline   Reply With Quote
Old Jul 11th, 2006, 09:48 AM   #4
dgr7
Board Regular
 
Join Date: Apr 2006
Posts: 223
Default error in batch .xls to .prn conversion code?

hello,
I'm trying to deal with the same problem as the original post in this thread, where I have a large number of workbooks in a directory and I want to batch convert them to .prn files.
I cut & pasted the above VBA code into Excel and tried to run it, but I immediately get the error Combile error: Variable not defined and the word
msoFileTypeAllFiles
of the line
.FileType = msoFileTypeAllFiles
is highlighted. I looked through the code and don't see it mentioned anywhere else or Dim'd etc.
Why did the code work fine for JSherrod in this thread but not me?
The only change I made to the code is the line
Const Drive As String = "C:AData"
to
Const Drive As String = "D:\BDcd\The Ct Bnk"

I'm using Excel 2002, btw.
If I can't get this code working, is there some other VBA code out there that would do the same job?

thanks,
david
dgr7 is offline   Reply With Quote
Old Jul 11th, 2006, 02:12 PM   #5
dgr7
Board Regular
 
Join Date: Apr 2006
Posts: 223
Default

anyone? help?

thanks, david
dgr7 is offline   Reply With Quote
Old Jul 11th, 2006, 02:26 PM   #6
Datsmart
Board Regular
 
Datsmart's Avatar
 
Join Date: Jun 2003
Location: Olympia
Posts: 7,942
Default

I'll bet you need to set a reference in VBA.
Like Tools | References...
Microsoft Visual Basic for Applications Extensibility...

Might be a different reference though.
__________________
John
Datsmart is offline   Reply With Quote
Old Jul 11th, 2006, 02:36 PM   #7
Greg Truby
MrExcel MVP
Int'l Moderator
 
Greg Truby's Avatar
 
Join Date: Jun 2002
Location: 39° 17' 15" N, -94° 40' 26" W
Posts: 9,610
Default

John (Datsmart) is correct, it is a referencing error that you'll fix in the VBE menu option Tools | References... The reference you'll need is to Microsoft Office xx.x Object Library (the "mso" at the front of the variable names lets you know this). [You would also see this in the Object Browser, but only if you have a reference to Office -- sort of a Catch 22.]
__________________
Greg
………………………………………………
Work: XL 2003, 2007 and 2010 on Windows 7
Please use CODE tags - especially for longer excerpts of code.
Greg Truby is offline   Reply With Quote
Old Jul 11th, 2006, 04:53 PM   #8
dgr7
Board Regular
 
Join Date: Apr 2006
Posts: 223
Default

Datsmart & Gret Truby:
Thank you both! I turned on both of those References and now the code works!
One quick question:
In the line
MsgBox "Completed - " & dCount & " files have been converted.", 4160,
"Batch Info"
What is the 4160 referring to?

david
dgr7 is offline   Reply With Quote
Old Jul 11th, 2006, 04:56 PM   #9
Datsmart
Board Regular
 
Datsmart's Avatar
 
Join Date: Jun 2003
Location: Olympia
Posts: 7,942
Default

It assigns the type of Message Box to display.
In this case one with the Information icon (64) and the msgbox is systemModal (4096), all applications are suspened until a response.
Added together, they are 4160.
__________________
John
Datsmart is offline   Reply With Quote
Old Jul 11th, 2006, 05:12 PM   #10
Greg Truby
MrExcel MVP
Int'l Moderator
 
Greg Truby's Avatar
 
Join Date: Jun 2002
Location: 39° 17' 15" N, -94° 40' 26" W
Posts: 9,610
Default

4160 is the sum of
    [*]vbSystemModal (4096 / &H1000)[*]vbInformation (64 / &H40).[/list]You can see this in the Object Browser if you look up the VbMsgBoxStyle contants in the VBA library.
__________________
Greg
………………………………………………
Work: XL 2003, 2007 and 2010 on Windows 7
Please use CODE tags - especially for longer excerpts of code.
Greg Truby is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 10:32 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes