tags:

views:

40

answers:

2

I have this script that I'd like to in addition save as a xls file, also save as a .txt in the same directory, or even a different one. Can I get some guidance here?

Imports System.IO

Module Module1

 Private Property fs As Object
 Private Property BaseName As Object
 Private Property FullTargetPath As Object

Sub Main()

    Dim targetfolder As String = "C:\TEST"
    Dim sourcefolder As String = "\\10.97.8.16\c$\Checks\XMLFiles"
    Dim Searchpattern As String = String.Format("{0:MM-dd-yyyy}*.xml", Date.Today)
    Dim todaysfiles() As String = Directory.GetFiles(sourcefolder, Searchpattern)
    Dim xlApp, xlWkb
    xlApp = CreateObject("excel.application")
    fs = CreateObject("Scripting.FileSystemObject")
    Const xlnormal = 1

    'Extra Dims


    'Hide Excel
    xlApp.Visible = False

    For Each file As String In todaysfiles
        ' Excel stuff... ' 

        Dim fileName As String = IO.Path.GetFileNameWithoutExtension(file)

        ' Concatenate full path. Extension will be automatically added by Excel. ' 
        Dim fullTargetPath = IO.Path.Combine(targetfolder, fileName)

        'Process each file in SourceFolder
        ' For Each file In fs.GetFolder(SourceFolder).files
        'Open file in SourceFolder
        xlWkb = xlApp.Workbooks.Open(file)
        'Get Filename
        BaseName = fs.getbasename(file)
        'Concatenate full path. Extension will be automatically added by Excel
        fullTargetPath = targetfolder & "\" & BaseName
        'Save as XLS file into TargetFolder
        xlWkb.SaveAs(fullTargetPath, xlnormal)

        'Close the file after its done
        xlWkb.close()
    Next

    xlWkb = Nothing
    xlApp = Nothing
    fs = Nothing

    ' MsgBox("Thank you. Currently the Date is: " & Date.Today & " people like to eat chicken Every " & Date.Today.Ticks & " minutes.")


    'This is for extra code below

End Sub

End Module

A: 

Pretty much exactly as you are doing now, but change the format in from xlnormal to xltext.

xlWkb.SaveAs(fullTargetPath, xltext)

When researching something like this I like to look around the object browser under vba editor. Look up Workbook object in this case and then the SaveAs sub to get the definition (parameters and types) then you can click on the parameter you want more information on and look it up from the help menu. I say that from memory I have not played with excel macros in years. Good luck!

Hugo
Hi Hugo, changing that along with the Const xltext =1, still changed it to an xls file, not a txt file.
gabrielVa
xlText doens't equal 1... here let me see. xlText is a global vba constant like xlnormal is. xlText is equal to -4158 and xlNormal is equal to -4143, use the magic numbers if you insist.
Hugo
Hmmm, intresting enough i had it set to Const xltext =1 and it did the convert to xls. I changed it to -4158 and it does convert it to a text file, however its now asking me to save it everytime as where before it didnt. I have it set to xlweb.close().
gabrielVa
If you want to avoid pesky dialog boxes set Application.DisplayAlerts = False. Remember google is your friend.
Hugo
Another thing what is it that you mean when you say Const xltext = 1 ? As a guideline for the future if an environment gives you constants like this don't redefine them elsewhere or use magic numbers and most importantly use them, they provide insights in function calls as well as improve readability. Ah I looked at your code again and you do Const xlNormal = 1, that is a very very very bad habit, do not do this, ever. Good luck. xlNormal is suppose to be equal to -4143. Please think about what you are trying to do before you jump in and monkey around like that.
Hugo
OK thanks for the insight Hugo. I was able to prevent the popup by changing my close statment to reflect the following: > xlWkb.close(savechanges = True) The code use to say> xlWkb.close()Do you happen to know why it still worked using 1 and not -4143? Im just curious and thought maybe you had some additional insight to that. Seems to be working great now. Its now opens each xml file, runs the conversion in Excel, then saves as a txt file.
gabrielVa
I don't have excel in front of me but from what I think I remember excel vba is pretty forgiving about that sort of thing. If you pass in invalid arguments it will probably use the default which is xlNormal (or xlNormalWorkbook) just as it would if you didn't pass any argument for the file format. Glad to hear it's working nicely now. Cheers! Last bit, the object browser window is very nice for things like this, check it out whenever you are stuck on which params to use or if there is already a function for so and so.
Hugo
Hey Hugo, What if I wanted to save them as a .prn of flat file space delimited? what would the const # for that be? Do you know or do you have a website that would list all the saves?
gabrielVa
Const xlprn = 36 seems to work.
gabrielVa
there you go http://msdn.microsoft.com/en-us/library/bb241279(office.12).aspx
Hugo
for a more general reference page http://msdn.microsoft.com/en-us/library/bb149081(v=office.12).aspx
Hugo
A: 

Why can't you call

xlWb.SaveAs(fullTargetPath, XlFileFormat.xlTextWindows)

or with any other xlFileFormat type?

jalexiou