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 :)
The problem you describe in your script is, that you want to save a document name (including extension!!!) into a text file. Then you try to convert this filename into a number which will fail and result as 0.To avoid this, you could to the following:Store the ActiveDocument.Name in a string variable, search for the first dot and cut off the part starting at that position. Then you have again only your number and it will work.Since I'm very familiar with this kind of problem, I would suggest an absolutely different solution. This way even allows you to give your document a name in addition to the version number.In my example you even can start with a version 0 with 3 or more leading zeros:
Option ExplicitSub aaTestMe()SaveAsNext ActiveDocument, "C:\Temp\2018\Test\ForCDT_01_1000.Cdr"End SubSub SaveAsNext(ByRef iDoc As Document, ByVal pDefault As String)Dim CurPath As String, CurBody As String, CurNum As String, CurExt As String, CurName As StringDim CurFile As String, MaxFile As StringDim n1 As Integer, n2 As Integer, Ch1 As String, Ch2 As StringCurName = pDefaultn1 = InStrRev(CurName, ".")CurExt = Mid(CurName, n1)CurName = Left(CurName, n1 - 1)n1 = InStrRev(CurName, "\")CurPath = Left(CurName, n1)CurBody = Mid(CurName, n1 + 1)n1 = InStrRev(CurBody, "_")CurNum = Mid(CurBody, n1 + 1)CurBody = Left(CurBody, n1)'After splitting the default name into different needed parts we explore, what is already existentMaxFile = ""CurFile = Dir(CurPath + CurBody + "*" + CurExt)While CurFile <> "" If MaxFile = "" Then MaxFile = UCase(CurFile) Else If UCase(CurFile) > MaxFile Then MaxFile = UCase(CurFile) End If CurFile = DirWendIf MaxFile <> "" Then 'at least one file of that default name exists n1 = InStrRev(MaxFile, ".") MaxFile = Left(MaxFile, n1 - 1) ' cuts off the extention Ch1 = Mid(MaxFile, Len(CurBody) + 1) 'Cuts out the highest numeric part of the series n1 = Val(Ch1) + 1 'convert this to the next number Ch1 = Trim(Str(n1)) 'makes a string While Len(Ch1) < Len(CurNum) Ch1 = "0" + Ch1 Wend 'fills the leading zeros, so that you can start also with 0 CurNum = Ch1End IfCurName = CurPath + CurBody + CurNum + CurExtiDoc.SaveAs CurNameEnd Sub
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!