Need Help | VBA | Doc.Name Via .TXT File

Okay - Hang in there with me while I try to explain...

What I have so far: 

  • CommandButton1 - Creates a new Document
  • CommandButton2 - Saves Document
  • A .txt file with a string number value. I started mine at 1000. (I am calling this my "Design Number"). 

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 Integer
Dim FilePath As String
Dim 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 Document
Set 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 Integer
Dim FilePath As String


FilePath = "C:\Users\chier\Desktop\New Text Document" & ".txt"

TextFile = FreeFile


Open FilePath For Output As TextFile ' Replaces Each Line in File
'Open FilePath For Append As TextFile ' Adds additional Lines

On Error Resume Next
Print #TextFile, ActiveDocument.Name

Close TextFile


ActiveDocument.SaveAs "C:\Users\chier\Desktop\" & ActiveDocument.Name & ".cdr"
End Sub

Any adjustments will be helpful :)