Okay - Hang in there with me while I try to explain...
What I have so far:
When I Create a New Document via Commandbutton1 the ActiveDocument.Name is the string value on the .txt file + 1. (I'm generating design numbers that count up). Basically for every new document via the macro is a new design number naming that document.
When I Save Document (commandbutton2) - The New Document.Name should replace the string in the txt file with 1001. But this part isnt working.
And it should keep going up from there every time i create a new document and save it.
Private Sub commandButton1_Click()
Dim TextFile As IntegerDim FilePath As StringDim FileContent As String
On Error Resume Next FilePath = "C:\Users\chier\Desktop\Design Number" & ".txt"
TextFile = FreeFile
Open FilePath For Input As TextFile
FileContent = Input(LOF(TextFile), TextFile)
ActiveDocument.Name = FileContent
Close TextFile
Dim doc1 As DocumentSet doc1 = CreateDocument() doc1.Name = FileContent + 1 End Sub
Private Sub commandButton2_Click()
'(This is where stuff starts action crazy i think). IT doesnt save properly i think.
Dim TextFile As IntegerDim FilePath As String
FilePath = "C:\Users\chier\Desktop\New Text Document" & ".txt"
Open FilePath For Output As TextFile ' Replaces Each Line in File'Open FilePath For Append As TextFile ' Adds additional Lines
On Error Resume NextPrint #TextFile, ActiveDocument.Name
ActiveDocument.SaveAs "C:\Users\chier\Desktop\" & ActiveDocument.Name & ".cdr"End Sub
Any adjustments will be helpful :)
NudeFan! You went way deep into this! This is helpful, though and Im starting to parse it. Thank you for taking the time to help me. Also, I got my code to work. I'm currently utilizing yours for other routes.
If you wanted test the code out! It's below.
1. Create a text file called Design Number. Type 1000 inside and save it.
2. Change the path on the code below to your new path.
3. Then run Sub newDesignNumber() // Make sure corel is open - obviously.
Option Explicit
Sub newDesignNumber()
On Error Resume NextFilePath = "C:\Users\chier\Desktop\" & "Design Number" & ".txt"
Dim doc1 As DocumentSet doc1 = CreateDocument()doc1.Name = FileContent + 1
Call nextDesignNumberEnd Sub
Private Sub nextDesignNumber()
FilePath = "C:\Users\chier\Desktop\" & "Design Number" & ".txt" TextFile = FreeFile
Open FilePath For Output As TextFile ' Replaces Each Line in File On Error Resume Next Print #TextFile, ActiveDocument.Name Close TextFile
End Sub
It's simple and not perfect ... I know it. But it's easy to manage and does what I need it to. Hopefully this helps others who need something similar!
If you have other recommendations - Please share!!!
As very often, I did not test your initial script before I replied.
Please consider this not as critics on your way of programming, just as a comment of a guy who started most likely with programming before you started to breath. During approx. 40 years of working partly mainly in computer science I saw so many situations a newbie can't imagine, which performed my way of writing programs. Even I made a lot of mistakes and learned (at least partly) out of it.
My wrong assumption was, that Corel indeed is that stupid, to execute a SaveAs with saving a file without extension if it's omitted in the filename. Most programs (I tried this in the past) did this with the default extension, here '.Cdr'. To force that programs to save a file without an extension you needed to add a dot at the end of the file name.
Ok, it works now that way, but I would never trust, that this will work also in Corel 2019 or later. So using a file name which defines all parts is the more save way.
What your tool is doing, is generating a file without extension, means you have big problems later to open it again. Either you create a macro to do this under VBA or you have to rename it manually after saving it automatically. A third option would be, to do this with a right click in the explorer, where you always have to select Corel.
Next strange thing you did: You assigned 'FileContent' as a string, read it from a file added to a string an integer of 1. VBA is very error tolerant and changed your string with the value "1000" to a string with "1001", but a more strict evaluation of variable types (maybe in the future of VBA or you have to move to an other programming language) may make not work anymore.
I don't know, if your line 'FilePath = "C:\Users\chier\Desktop\" & "Design Number" & ".txt"' had been added that way to remove internal names, I assume, you now, that you may use 'FilePath = "C:\Users\chier\Desktop\Design Number.txt"' as well.
But: Do you know as well:You can define a variable directly under the header of the module. Then you can give a value to it in the first sub routine and it's defined after that in all other sub routines. In your example:
Option ExplicitDim FilePath As StringSub MyClick()'...FilePath = "C:\Temp\2018\Test\MyNum.txt"'...newDesignNumber'...End SubSub newDesignNumber()'...Call nextDesignNumber'...End SubPrivate Sub nextDesignNumber()' Dim FilePath As String 'you must not have this line in this subroutine!!!'...MsgBox FilePath'...End SubSo you have to define this string only once! But be careful: You must not declare this variable name in any sub routine!
That's enough for the today's lesion Oldies teach Newbies, hope it will bring something beside frustration to you
I understand. No frustration. You're always helping me.
I'm not intentionally trying to save my document just yet. I'm just setting up my document "name" for when I do save it in the location I want to save it in which will then add .cdr then.
Also, I need the the text file to count up. Its supposed to add 1 every time a new document is created and store the new number. So document 1 is named 1000, doc 2 is named 1001, doc 3 is 1002 and so on...
I'm attempting to generate "design numbers" per each new document. Its like a "Sales Order Number" for an Order; that number is specific to that order. Im trying to generate a number that is specific to each document with a design. So customers will say "can you revise proof number 1001 to blue? See what I mean? So i pull up proof 1001 and revise it.
The code I have above is showing you exactly what i want it to do but i certainly don't want it not working later down the road because I know it's not written well. I want to improve it.
Perhaps a proper way of writing it but it doesn't exactly what its doing?
Oki, hopefully now I understood what you need.You ever spend an idea to split the information and saving only a number in the file and generate out of it a file name?With the way you do it with the file handling you are on the right way, some small remarks:FileContent = Input(LOF(TextFile), TextFile): The LoF will cause, that the content is read at the end of the file, when you overwrite later the file, you don't need to search the end of the file. I'm using for such purposes normally Line Input #TextFile, CurLine. This converts the (first) line you see in your text file in a string, no matter, if it contains separator like comma or tabulator, you get what you see. If you need to separate some parts of the line you can do this manually by searching for a comma or tab, but you're on the save side.You're using On Error Resume Next. This causes, that - when in the entire routine an error occurs - the next line is executed. So you do not get an error, but an erroneous result without notification. You can switch off this feature after the line you expect an error with On Error Goto 0. Then you don't get a notification only when the error occurs in a specific line. You should place this command only before a line which may cause an error, e.g. before you open a file. If this happened without problems, switch the error handling on again to get warnings when any other thing will fail. A more convenient way is, to handle the error:
Sub SaveAsNext2()Dim TextFile As Integer, FilePath As String, FileContent As StringFilePath = "C:\Temp\2018\Test\MyNum.Txt"TextFile = FreeFile'what happens until now, is all caused by the programmer, (s)he can fix it manuallyOn Error GoTo ErrHdl'now we have a line which may cause an error form outside, e.g. a colleague opened the same file on a different computerOpen FilePath For Input As TextFile'This is the only situation where an error may occur, the programmer can't fix in the module, so switch this offOn Error GoTo 0If TextFile > 0 Then 'the following steps can be done only, when everything wend fine Line Input #TextFile, FileContent Close #TextFileEnd IfExit Sub ' if everything went fine, you have to leave here (Note: If you use this in a function, you need Exit Function!)ErrHdl: 'here starts the error trappingSelect Case Err.Number Case 76 ' the location can't be found, maybe network is not available MsgBox "Check the network" TextFile = -1 'set's a variable to show, that an error occurred Case Else MsgBox Err.Number 'shows you the number of the not yet handled error On Error GoTo 0 'switches off the error trapping Resume ' will cause that you get the error message as usualEnd SelectEnd Sub
I would solve your needs (as I understood) the following way in one sub routine you can call with your click:Sub SaveAsNext3(ByVal pId As Integer)Dim TextFile As Integer, FilePath As String, FileContent As String, OutBase As StringDim DesignNumber As Integer, Ch1 As String, Ch2 As StringFilePath = "" ' to ensure, that this variable is emptySelect Case pId 'so you can deal different projects with the same routine Case 1 FilePath = "C:\Temp\2018\Test\MyNum.Txt" OutBase = "C:\Temp\2018\Test\ForCDT_" Case 2 FilePath = "C:\Temp\2018\Test\MyNum2.Txt" OutBase = "C:\Temp\2018\Test\ForCDT2_" Case Else MsgBox "SaveAsNext3 for" + Str(pId) + "not defined!"End SelectIf FilePath <> "" Then ' only if you use it with a valid id TextFile = FreeFile On Error GoTo ErrHdl Open FilePath For Input As TextFile On Error GoTo 0 If TextFile > 0 Then 'the following steps can be done only, when everything wend fine Line Input #TextFile, FileContent DesignNumber = Val(Trim(FileContent)) + 1 ' converts the string into an integer Close #TextFile TextFile = 0 End If If TextFile = 0 Then 'either we could read the textfile or the path is ok and we can create a new one Ch1 = Trim(Str(DesignNumber)) While Len(Ch1) < 4: Ch1 = "0" + Ch1: Wend ' a more compact way TextFile = FreeFile On Error GoTo ErrHdl Open FilePath For Output As TextFile On Error GoTo 0 If TextFile > 0 Then Print #TextFile, Ch1 'to save only a numeric value stored in a string Close #TextFile End If Ch1 = OutBase + Ch1 + ".Cdr" ' to make a nice file name with location and so on ActiveDocument.SaveAs Ch1 End If End IfExit SubErrHdl: 'here starts the error trappingSelect Case Err.Number Case 53 ' file does not (yet) exists TextFile = 0 'set's a variable to show, that an expected error had been handeled DesignNumber = 1000 ' is the default initial value Case 76 ' the loaction can't be found, maybe network not available MsgBox "Check the network" TextFile = -1 'set's a variable to show, that an error occured Case Else MsgBox Err.Number 'shows you the number of the not yet handeled error On Error GoTo 0 'switches off the error trapping Resume ' will cause that you get the error message as usualEnd SelectEnd SubFeel free to reply, if you don't understand all the lines, when I have some time I will reply to you.