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.
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.
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 built a macro that writes a custom csv file to disk. I need to add the option to also save this file in a zipped format. I followed Ron de Bruins excellent article on the matter but face the problem that my zip file is empty.
NewZip (FilePathZip) 'creates an empty zip file
Set objShell = CreateObject("Shell.Application")
objShell.Namespace(FilePathZip).CopyHere FilePathCSV
FilePathZip is the full path to the new zip file I'm creating here. FilePathCSV is the full path to the CSV that was just saved to disk. Interestingly, when I switch out FilePathCSV for any other file that already exists before I run the macro, it works. Apparently, I only face this problem when trying to zip a file that was created during the runtime of the macro.
I already checked if the CSV path is recognized by excel through Dir(FilePathCSV) and made sure that the file is closed after the writing process. I also tried adding timeouts (Wait()). I have no idea what the problem is.
Wrapping FilePathCSV in a Dir() function solved my problem. I'm not sure why though.
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 }
Here's what I'm doing:
I'm using a Foreach Loop container to grab any .xlsx files in a specified folder and assigning the fully qualified name to a variable called FileName.
Then I have a data flow with an Excel source importing to an OLE DB Destination.
How do I make the excel source the FileName variable?
--When I create the same process for flat files I have no problems creating an expression and changing the delay validation to true but when I try excel files it doesn't work the same. I've been able to work around the problem by using a file system task to move the xlsx files to a new folder giving it a static name and importing from that file, but I'm tired of doing that. Any help will be greatly appreciated!