Hi all,
Basically I'm trying to use a texbox.value to be inserted in a codemodule line. Line 6 & 7 to be exact. The textbox name is "frmNewAccount.txtNewAccountName". The codemodule name is "cmdSaveQuote".
So here is my problem. The workbook name and file path that I'm trying to change it to doesn't exist yet. I have a userform with a textbox. When the user types in text and clicks "OK" then the new workbook with file path will be created so I don;t know the exact file name. The workbook file name will be whatever the user types into the textbox "txtNewAccountName". In my code I Dim file_name As string and file_name = frmNewAccount.txtNewAccountName. So when I insertlines in the code module I use file_name. It litterally inserts "file_name" instead of the value in the textbox obviously because I'm not doing it correctly. My code is incomplete because I ran into this road block but...
Here is my code,
Private Sub cmdOK_Click()
'-------------------------------------------------------
'******Create new workbook save as textbox.value********
'-------------------------------------------------------
Application.ScreenUpdating = False
On Error GoTo HandleErr
Application.ScreenUpdating = False
Dim file_path As String
Dim file_name As String
Dim complete_name As String
Dim next_file_path As String
Dim next_complete_name As String
Dim VBProj As Object
Dim VBcomp As Object
Dim CodeMod As Object
Dim file_extension As String
file_extension = ".xlsm"
file_path = "C:\TSD Safety\Clients"
file_name = frmNewAccount.txtNewAccountName.Value
complete_name = file_path & "\"
next_file_path = "C:\TSD Safety\Collective Data"
next_complete_name = next_file_path & "\"
On Error Resume Next
Workbooks("Quote Template.xlsm").Activate
If Err <> 0 Then Err.Clear: Workbooks.Open ("C:\TSD Safety\Templates\Quote Template.xlsm")
Set VBProj = Application.Workbooks("Quote Template.xlsm").VBProject
Set VBcomp = VBProj.VBComponents("cmdSaveQuote")
Set CodeMod = VBcomp.CodeModule
With CodeMod
.DeleteLines 6, 1
.InsertLines 6, "Workbooks(""file_name & file_extension"").Activate"
.DeleteLines 7, 1
.InsertLines 7, "If Err <> 0 Then Err.Clear: Workbooks.Open (""file_path & file_name & file_extension"")"
End With
ActiveWorkbook.SaveAs Filename:=complete_name & file_name, FileFormat:=52
ActiveWorkbook.Close SaveChanges:=False
On Error Resume Next
Workbooks("Quote Collective Date Template.xlsx").Activate
If Err <> 0 Then Err.Clear: Workbooks.Open ("C:\TSD Safety\Templates\Quote Collective Data Template.xlsx")
ActiveWorkbook.Sheets("Sheet1").Name = file_name
ActiveWorkbook.SaveAs Filename:=next_complete_name & file_name, FileFormat:=52
ActiveWorkbook.Close SaveChanges:=False
Unload frmNewAccount
Application.ScreenUpdating = True
Exit_Here:
Exit Sub
HandleErr:
Debug.Print "Error Number " & Err.Number & ": "; Err.Description
End Sub
Any help on this would be greatly appreciated. I've been banging my head against the wall for several days trying to get this code to do what I want. Code that writes code is hard. I'm barely learning it now. I didn't even know it existed until about a week ago.
Moderator note - code tags added for you
Bookmarks