Naming a XLSX file - xlsxwriter

I want to generate XLSX file and save it with custom name, for instance, "current time" or a series of names like hello[i] 0
def excel_print(self):
workbook = xlsxwriter.Workbook('walk1.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write(0,4, time.asctime( time.localtime(time.time()) ))
worksheet.set_column(0, 0, 30)
worksheet.write(0,0,"No of steps")
worksheet.write(0,1,self.steps)
workbook.close()
Now in the above code, each time I run the program,I want file to be saved with different name. Is that possible??

workbook=xlsxwriter.Workbook(str(bookTitle)+".xlsx") is the solution I have used in my code and it works.
You will just need to pass in the variable that I have named bookTitle in this case. If bookTitle is already a string you won't need the str at the front of str(bookTitle) however with your example hello[i], (str(hello[i])+".xlsx") should work.

Related

Change value of the column .csv file using Access VBA

I have a .dat file that I saved as a .csv and it imports in a table, OK. But this file has in the first column = HOUR "hnnss". The file name contains the date that I already managed to separate it and save it in a variable=date.
My problem is: when saving the file as .csv I need to open the file, change the values of the first column from hnnss to data hh:nn:ss and then save and close, only then do I import it into the table. It needs to be in that sequence. Thanks for help.
PS: I'm using Access 365 + VBA 7.1
Andreia: You can read the file using method like in here Import csv to array and in this loop :
For i = 2 To UBound(aryFile) - 1 ' in your case start with second line
tsOut.WriteLine aryFile(i)
Next
implement the formatting like this:
' in your case start with second line if the 1st is header
For i = 1 To UBound(aryFile) - 1
aryRow = Split(aryFile(i), ",")
aryRow(0) = formatTime(aryRow(0), aryRow(?)) ' replace ? with index of date field
tsOut.WriteLine Join(aryRow,",")
Next
The above code is using function formatTime(fieldWithTime, fieldWithDate) which you need to write and which returns your formatted string for the whole date. I leave it for you. If you won't be able to code it let me know but in that case you rather read some books about VBA programming.
Note: I did not debug the code. This is just an idea.

Create a text file with a variable name and write to it

For an application i'm working on, i am trying to create a 'save data' feature.
First off, it needs to create a .txt file with a custom name, this needs to be the date (today) and the text of a textbox, it needs to be in the format of yyyymmdd_textbox1(.txt)
How would i go about doing this? it can create it where ever, but if it already exists it needs to append to it on a new line
thanks for any responses
If you want a file-name from user-input you first need this method:
Public Function SanitizeFileName(fileName as String) As String
For Each c In IO.Path.GetInvalidFileNameChars()
filename = filename.Replace(c, "_"c)
Next
Return fileName
End Function
Then it's easy
Dim filename = $"{DateTime.Today.ToString("yyyyMMdd")}_{SanitizeFileName(textbox1.Text)}.txt"

Solidworks VBA save as new name

I am currently generating a solidworks part file name from a text document for a batch "rename" of the documents. This assigns sequential numbers to them which i then save each part in a folder as. I have been trying to figure out a method for the new file names to be referenced in the assembly they were a part of.
For saving the document I am using :
swModel.SaveAs3 "" & FileName & "", 0, 0
I have no issue creating all the files in a batch but I could not seem to find a method of apply this save in such a way that any open documents that it is referenced in change to referencing the new part name.
Edit: I figured out a serviceable solution using some of the information held in this link:
help.solidworks
Using the pack and go functionality in solidworks avoids having to deal with the references as they are already handled by the process itself Hope this helps.
Try the below approach:
Dim FileName As String
FileName = "blablabla" & Variable & "blablabla"
swModel.SaveAs3 ("C:\User\Username\Desktop" + FileName + ".SLDPRT", 0, 1)
Variable = Variable + 1
'continue with your loop

Initial Excel Workbook Path

I have an excel spread sheet which, after 30 seconds, saves itself as a new file on the desktop so the original file is not edited. I want the path of the original file, so i add -
"ActiveWorkbook.Path"
But, this doesnt work correctly now, as when the file is saved as a new name after 30 seconds, it takes the
"ActiveWorkbook.Path"
of the newley named file, which in this case is the desktop.
Is there anyway in getting the Path to the initial file, before it got saved ?
I would try to store it in a variable at the start of the code:
Dim originalPath As String
originalPath = ActiveWorkbook.Path
Then, you can refer back to it as needed.

How to import part of a file name to a field

I have a file that i import into access 2007 and i was wondering if i can take part of that file name and put it into a field in access? For example here is one example of a file name:
"20140211_agent_statistics.csv"
I have done some research on this but cant seem to find the answer when numbers change all of the time. I just need to grab the numbers on this file name. However, these numbers change all of the time. Does anyone have a solution for this? Thank you in advance. Any help and code is much appreciated i am very new to vba.
Working on a few assumptions:
You are importing this by code so it picks up the file name?
The numbers are the date so probably always 8 characters long?
If you import by code you will assign the file name to a variable, in case you don't here is how to:
Dim strFileO as String, strFileLoc as String
strFileLoc = "C:\YourFolder\" ' Folder where file is saved
strFileO = Dir(strFileLoc & "*.csv")
Above will pick up any .csv file in the folder, you should move them once imported
Once you have the strFileo then to get the date:
Dim lDate as Long
lDate = Left(strFileO,8)
'Or if the numbers aren't always 8 characters:
lDate = Left(strFileO. InStr(strFileO,"_") - 1) ' Assumes numbers followed by "_"