How to save a csv file through an lotusscript agent - lotus-notes

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.

Related

Path not found when creating text file

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.

Open an Excel file in exclusive mode using VBScript

I have a simple question, but I've searched for this and couldn't find any helpful topics..
I'm working on a VBScript that opens an Excel file and modify a few stuff in it.. so I'm using this code:
Set objXLApp = CreateObject("Excel.Application")
objXLApp.Visible = False
objXLApp.DisplayAlerts = False
Set objXLWb = objXLApp.Workbooks.Open(FilePath)
Now, what I want to do is to open the Excel file using a way that locks the file and prevents the user from opening it while it's open by the script (until it's closed).
Update:
I think the problem is somehow related to the Excel instances, I tried to do the following (while the file is open by the script):
When I manually open the file (while it's open by the script) they're both become a single instance.
When I open any other Excel file they're both also become a single instance!!! And the original file (opened by the script) becomes visible!
Now this is weird because I'm using CreateObject("Excel.Application") and not GetObject(, "Excel.Application")
There is registry key HKEY_CLASSES_ROOT\Excel.Sheet.8\shell\Open\command on Win 7 Excel 2010 for me with default value "C:\Program Files\Microsoft Office\Office14\EXCEL.EXE" /dde. The command line /dde switch enables DDE (Dynamic Data Exchange mechanism - an ancient Win 3.0 interprocess communication method) that forces Excel to start in a single instance. I've tried to remove that switch and opened workbooks, but to no avail. BTW, if you don't have a permission to edit the registry, or you intend to distribute your script to someone who doesn't, that is not a way. Also have tried this answer, but it doesn't work for Win 7 Office 2010.
I've tested test.xlsm file with DDE enabled. When user opens a file, actually it is just reopened in existing instance that make it visible. If any changes has been already made by the script, then Excel alerts:
Anyway write-access is given for the user. After that when the script saves the file, another alert appears:
Some time ago I created a script that worked with Excel application, and encountered the same issue with Win 7 Excel 2010 as you are describing. I noticed that if there were several Excel application instances created with CreateObject() within script, then Excel file opened by user always used exactly the first created instance. I've solved the issue by creating two invisible instances of Excel application, let's say dummy and target. In outline the algorithm for a script is as follows:
Create dummy instance first, no need to add a workbook. After that the dummy instance is exposured an Excel file to be opened by user within it.
Create target instance.
Quit dummy instance.
Open target workbook, modify and save it.
Quit target instance.
Consider the below code that illustrates a possible way to implement what you need:
' target file path
sPath = "C:\Users\DELL\Desktop\test.xlsm"
' create dummy instance
Set oExcelAppDummy = CreateObject("Excel.Application")
' create target instance
Set oExcelApp = CreateObject("Excel.Application")
' quit dummy instance
oExcelAppDummy.Quit
' open target workbook
With oExcelApp
.Visible = False
.DisplayAlerts = False
Set oWB = .Workbooks.Open(sPath)
End With
' make some changes and save
Set oWS = oWB.Sheets(1)
oWS.Cells(1, 1).Value = Now()
oWB.Save
' give additional time for test
MsgBox "Try to open test.xlsm, OK to end the script"
' close target workbook
oWB.Close
' quit target instance
oExcelApp.Quit
Trying open the file you will get desired output:
And the notification after the script ends:
That is strange that you aren't getting a message as below:
One possible method would be
to change the file attributes at the start and end of the code, the version below makes the file readonly and hidden
make your changes
save the file with a different name
change the attributes back
rename the changed file to the original name
code
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objXLApp = CreateObject("Excel.Application")
filePath = "C:\Temp\MyFile.xlsm"
filePath2 = "C:\Temp\MyFile1.xlsm"
set objFile = objFSO.GetFile(filePath)
objFile.Attributes = 3
objXLApp.Visible = False
objXLApp.DisplayAlerts = False
Set objxlWB = objXLApp.Workbooks.Open(filePath)
'do stuff
objxlWB.saveas filePath2
objxlWB.Close
objXLApp.Quit
set objXLApp = Nothing
objFile.Attributes = 32
objFile.Delete
objFSO.MoveFile filePath2, filePath

Extract gz and convert csv to xlsx

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 }

Creating VBScript file Excel

I don't have much knowledge in creating a VBScript file. I have a code i want to automate to send email out every month. With some research i found the code below:
dim EXL
set EXL = CreateObject("Excel.Application")
'not required
EXL.Visible = true
'your file and macro
EXL.Workbooks.Open "full path to your excel file including extension here"
EXL.Run "Fixing"
'close everything
EXL.Quit
Set EXL = Nothing
My Question is: do i implement this code into my excel module or is in the worksheet event?
Once i have this correctly applied i will be able to set the Windows Task Scheduler to run at the particular time.
Just save the VBscript file as a *.vbs file. Create a text file (txt), copy your code and save the file with the vbs extension. Next go to the Windows Task Scheduler and choose Run a Program and locate your VBS.
Sending emails from VBscript
http://www.analystcave.com/excel-send-email-excel-workbook/
Scheduling programs to run in Windows
http://windows.microsoft.com/en-au/windows/schedule-task#1TC=windows-7

Refresh Excel using SSIS script task

I have an Excel file that gets external data from database table. I need to refresh the file automatically and email it. I intend to use SSIS script task to run some VB script that would open the file, refresh data, save and close (obviously without bringing up the application). then I'll use email task to send the file by email. All I need is the script that refreshes the file and being total noob in VB or C# I have to ask if anyone has a script that does that lying around and which I could customize for my file and use in my script task.
I'll appreciate any hints!
thanks a lot,
Vlad
Hope this is what you looking for
' Create an Excel instance
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
' Disable Excel UI elements
oExcel.Visible = True
oExcel.DisplayAlerts = False
oExcel.AskToUpdateLinks = False
oExcel.AlertBeforeOverwriting = False
Set oWorkbook = oExcel.Workbooks.Open("absolute path to your file")
oWorkbook.RefreshAll
oWorkbook.Save
oExcel.Quit
Set oWorkbook = Nothing
Set oExcel = Nothing
In my case (Connection to MS SQL DB), I had to uncheck the "enable background refresh" option for working fine.
Excel: Data > Connections > Properties > (uncheck) enable background refresh
Old post but 4M01 answer has helped me out heaps.
In my case I had to put a sleep just after opening the workbook to ensure the file loads correctly.
i.e.
oWorkbook = oExcel.Workbooks.Open("absolute path to your file")
Threading.Thread.Sleep(3000)
oWorkbook.RefreshAll
Note also in VS 2015 set is no longer required. so just remove "set " for the code to work.
This may not exactly fit your needs, but if it helps you or someone I was able to just use a simple
Execute Process Task
with the
Executable as /..path../Excel.exe and the
Arguments as the desired file (full path) to be opened

Resources