I hope someone can help me with this issue. I tried to Google something that do what I need but there isn't any of specific or easy to adapt and I'm a dud with programming and coding...
An application I use everyday produces several gz files containing one csv file each. With the aim to simplify a bit my life I wish run a script that do this work:
Extract the csv files from any gz in the current folder
Save any extracted csv file as xlsx files in the same folder
Rename the xlsx file properly
Delete any gz and csv files from the current folder
Here some more details about the environment and requirements:
Running OS is Windows 7 Enterprise (Powershell is installed)
Office 2010 is installed
I can't install any additional software or library (it's a corporate laptop)
Any csv file has the following naming convention:
CheeseFile_YellowCheese_yyyy_mm_dd-randomnumber_othersnumber.csv
CheeseFile_BlueCheese_yyyy_mm_dd-randomnumber_othersnumber.csv
HamFile_RawHam_yyyy_mm_dd-randomnumber_othersnumber.csv
HamFile_CookedHam_yyyy_mm_dd-randomnumber_othersnumber.csv
And should be respectively saved as:
OutputFile - CheeseFile_mmddyyyy_Yellow Cheese.xlsx
OutputFile - CheeseFile_mmddyyyy_Blue Cheese.xlsx
OutputFile - HamFile_mmddyyyy_Raw Ham.xlsx
OutputFile - HamFile_mmddyyyy_Coocked Ham.xlsx
The date in the original file name has to be kept in the output xlsx file
Cheese files are in their folder and Ham files are in a separate folder so I can adapt the script as I need.
Please let me know if you need further details and thank you in advance for any appreciated help :)
While I agree the OP does not appear to have done their fair share of figuring this out (how hard is Google?), I know someone else will be looking in the future. Posting information will help them.
#OP, I'm not going to do all your file handling work for you but here is the basic code to convert from CSV to XLSX which is probably the least common part of the question posed.
Option Explicit
Dim strCSVfile, strExcelFile, FSO
Dim objWorkbook, objWorksheet1
set FSO = CreateObject("Scripting.FileSystemObject")
strCSVfile = "C:\temp\Excel Test\myFile.csv"
strExcelFile = "C:\temp\Excel Test\myFile.xlsb" 'this was changed
if FSO.FileExists(strCSVfile) then
Dim objExcel
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.displayalerts=false
'Import CSV into Spreadsheet
Set objWorkbook = objExcel.Workbooks.open(strCSVfile)
Set objWorksheet1 = objWorkbook.Worksheets(1)
'Save workbook (51 = 2010 format)
'Formats ref https://msdn.microsoft.com/en-us/library/office/ff198017.aspx
'Parameters ref https://msdn.microsoft.com/en-us/library/office/ff841185.aspx
objWorksheet1.SaveAs strExcelFile, 50 'this was changed
objExcel.Quit()
else
msgbox "File Note found"
end if
Edit: BTW, OP#... post your code and more people will help you. If you update your question with the code you have written for unzipping and file handling, I will help with that part.
Edit: Updated for .xlsb format output. Lines changed marked as such.
7z e *.gz;ls | foreach {$old=$_.name;$newname='OutputFile - '+$oldname.split('_')[0]+'_'+$oldname.split('_')[3]+$oldname.split('_')[4]+$oldname.split('_')[2]+'_'+$oldname.split('_')[1]+'.xlsx';mv $oldname $newname }
Related
I've been through all stackoverflow posts about this and mostly its wrong path, space, wrong date format etc. etc... Nothing to help me.
I have this chunk of code:
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile(SaveLocation & "PONUDA.TXT", True)
a.Write (txtstring)
a.Close
where second line produces that error.
SaveLocation is filled as string with various things to create folder tree and final form of it is next:
C:\Users\User\Desktop\Otpremnice\2022\January\O1-9_BuyerName\
I can confirm that folders are created and do exist, so there is no reason for code to not work. Moreover it worked a month ago when I tested and wrote it...
Were there any windows updates that broke excel or I didn't enable some references?
Excel file is stored on desktop, so is folder I create.
For lots of years now I've been using a macro which imports data from a .xls file to MS Project. The thing is that for unknown reasons it only works on MS Project 2003. When I try running it on MS Project 2010 the moment before Import process finishes MS Project just freezes and nothing happens after that.
What is even more strange is that even without the macro (if I try to import the data manually using File -> Open -> ...) it still doesn't work, whereas in MS Project 2003 both ways work fine.
It seems to me as if the problem isn't connected to VBA at all but I am gonna post the macro here so you guys could see it anyway.
Here is some further information which I think might be important:
I'm using a custom mpp template.
I'm using a custom import map.
I tried using method FileOpenEx and it still doesn't work.
The .xls file I'm trying to import has three sheets for Task, Ressource and Assigment.
I tried saving the file as .xlsx since I'm using a newer version of Project but this didn't change anything.
I think it fails just at the end cause if i try to do it manually I can see how work is shown on the timeline graph next to the table in MS Project.
And here is the macro:
Dim filename As String
FileNew Template:="", FileNewWorkpane:=True
FileOpen Name:="PATH TO THE TEMPLATE FILE", _
ReadOnly:=False
FileOpen Name:="PATH TO FILE TO IMPORT", _
ReadOnly:=False, map:="MY CUSTOM MAPNAME"
filename = "C:\Imported.mpp"
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FileExists(namedoc) Then
Dim OldName, NewName
nomfile = 1
OldName = filename: NewName = "C:\Imported old Copy 1.mpp"
While fs.FileExists(NewName)
nomfile = nomfile + 1
NewName = "C:\Imported old Copy " & nomfile & ".mpp"
Wend
Name OldName As NewName
End If
FileSaveAs Name:="C:\Imported.mpp", FormatID:="MSProject.MPP"
Any suggestions would be highly appreciated. Thank you in advance!
The problem was with the custom fields I was using. I had four Outline_Code columns used for storing some text information.
I replaced them with four Text fields (Text1, Text2, Text3, Text4) and now the data is imported!
I have a lotusscript agent which needs to move the contents of a cell in excel sheet(a csv file) to another cell. Following is the piece of code:
Dim xlApp As Variant, xlwb As Variant, xlsheet As Variant
Set xlApp = CreateObject ( "Excel.Application" )
Set xlwb = xlApp.Workbooks.Open(StrFilePath) 'strFilePath is the path to csv file
Set xlsheet = xlwb.Worksheets(1)
'Logic to check necessary cells in the excel sheet goes here...
'Following two lines move the contents from (p,1) to (1,n)
xlsheet.cells(1,n).value = xlsheet.cells(p,1).value
xlsheet.cells(p,1).value = ""
Now the problem arises when I'm trying to save this csv file after moving the contents.
I have used the below line to save the file:
xlwb.SaveAs(StrFilePath)
This method does not return any error. Yet the file doesn't get saved.
Then I have also tried using the below line to save the file:
xlApp.activeworkbook.SaveAs(StrFilePath)
This method returns "Automation object error".
The file is not getting saved by either of the methods. At this point, the agent is not able to execute further.
The agent then needs to move this file from the path StrFilePath to another directory using the FileCopy statement. At this point, the agent throws the "Permission denied" error.
The file is present in a directory in the D disk drive on the server.
The agent also has been given "Allow restricted operations with full administration rights".
Could someone please let me know what is the correct way to save this csv file and how to provide the necessary permissions for the file to be saved?
Thanks!
Okay, so i was digging a little more deeper and found the solution for this.
The following line of code worked for me and the file got saved successfully.
xlApp.ActiveWorkBook.save
And then I just added the following lines for the cleanup...(this was not part of my question, but writing here just for the sake of completion)
xlApp.ActiveWorkBook.close
xlApp.quit
Set xlApp = Nothing
But I'm not sure why SaveAs did not work. Will post if I find the answer for this.
I wrote the following code to import TXT files in a ZIP file to excel. To do this I have to be able to unzip the file first.
I use:
ShellApp = CreateObject("Shell.Application")
ShellApp.Namespace(unzipToPath).CopyHere
ShellApp.Namespace(Filename).items
to do so but it keeps returning
error 91
The namespace contains Filename as a variable because I am looping through a list of file names in my workbook.
I hope this discription is clear and you guys are able and willing to help me out. If you guys needs any additional information please let me know.
The question is how to open a .xls file and save it as a .txt in powerbuilder 6.5.
For example the following doesn't work and I am stuck in the opening:
oleobject testexcel
testexcel= Create oleobject
testexcel.ConnectToNewObject("excel.application")
testexcel.workbooks.Open("something.xls")
testexcel.ActiveWorkbook.saveas("something.txt")
testexcel.disconnectobject()
Destroy testexcel
The OLE solution is the way to go if you need the process to be automated.
If you are doing data manipulation (one time) than simply open excel and saveas (txt, csv, etc) and then do dw_1.importfile(xxxx ) to import the temporary file you created from Excel.
I suspect you are looking for the first solution because the second is more obvious-- and I think that is where Matt was going with his answer.
Found another StackOverflow question like this that was answered here: How to import Excel file into DataWindow
Here is some code, that isn't exactly what you wanted, but it could be revised to work...
string ls_pathname, ls_filename
long ll_rc
oleobject loo_excel
IF GetFileOpenName ( "Open File", ls_pathname, ls_filename, "XLS", "Excel (*.xls),*.xls" ) < 1 THEN Return
loo_excel = CREATE OLEObject
loo_excel.ConnectToNewObject( "excel.application" )
loo_excel.visible = false
loo_excel.workbooks.open( ls_pathname )
loo_excel.ActiveCell.CurrentRegion.Select()
loo_excel.Selection.Copy()
ll_rc = dw_1.ImportClipBoard ( 2 )
ClipBoard('')
loo_excel.workbooks.close()
loo_excel.disconnectobject()
DESTROY loo_excel
The clipboard seems kind of round-about but it was all I could find on short-time. Good luck
You can save it as a csv file which is basically the same as text.