MikeGozna
New Member
- Joined
- Feb 19, 2019
- Messages
- 10
- Platform
- Windows
Hi Guys, First post for me. Complete new user of VBA.
I am making a small programme in VBA which will open a document from a template from a user form. The code, although all done in longhand as a non programmer all seems to work fine up to this point. What I want to do now is save the new document with the filename that is stored in TextBox18.
The problem is no matter what i do i cannot get the file to save. Please could the experts offer a simple solution. I have managed to get it so I do not have multiple instances of winword and documents running. I have put some code below, please feel free to pick it to pieces for any improvements.
This is the code I am using to open word and the copy of my template.
PS Please do not at my attempt at coding to much, it is my first attempt. Thankyou in advance for any help.
I am making a small programme in VBA which will open a document from a template from a user form. The code, although all done in longhand as a non programmer all seems to work fine up to this point. What I want to do now is save the new document with the filename that is stored in TextBox18.
The problem is no matter what i do i cannot get the file to save. Please could the experts offer a simple solution. I have managed to get it so I do not have multiple instances of winword and documents running. I have put some code below, please feel free to pick it to pieces for any improvements.
This is the code I am using to open word and the copy of my template.
Code:
Private Sub OpenGoznaQuote_Click()
Dim wdoc As Object
Dim wrd As Object
Set wrd = CreateObject("Word.Application")
wrd.Visible = True
Set wdoc = wrd.Documents.Add("C:\New Dashboard\Templates\Gozna Quotation Temp")
wrd.Activate
'Alocate the values of the text boxes in the new quotation user form
'to the bookmarks in the word quotation document.
With wdoc
.Bookmarks("Quote").Range.text = Me.TextBox1.Value
.Bookmarks("Title").Range.text = Me.CboTitle.Value
.Bookmarks("First").Range.text = Me.TextBox5.Value
.Bookmarks("Surname").Range.text = Me.TextBox4.Value
.Bookmarks("Company").Range.text = Me.TextBox6.Value
.Bookmarks("HouseNo").Range.text = Me.TextBox2.Value
.Bookmarks("Address1").Range.text = Me.TextBox3.Value
.Bookmarks("Address2").Range.text = Me.TextBox12.Value
.Bookmarks("Address3").Range.text = Me.TextBox11.Value
.Bookmarks("Town").Range.text = Me.TextBox10.Value
.Bookmarks("County").Range.text = Me.TextBox9.Value
.Bookmarks("Postcode").Range.text = Me.TextBox8.Value
'This code looks to see if a range is blank and if it is
'the code deletes the corresponding bookmak and the line the bookmark is on in the word document.
If Me.TextBox6.Value = "" Then
.Bookmarks("Company").Range.paragraphs(1).Range.Delete
' .Bookmarks("Company").Range.Delete
End If
If Me.TextBox12.Value = "" Then
.Bookmarks("Address2").Range.paragraphs(1).Range.Delete
' .Bookmarks("Address2").Range.Delete
End If
If Me.TextBox11.Value = "" Then
.Bookmarks("Address3").Range.paragraphs(1).Range.Delete
' .Bookmarks("Address3").Range.Delete
End If
If Me.TextBox9.Value = "" Then
.Bookmarks("County").Range.paragraphs(1).Range.Delete
' .Bookmarks("Address3").Range.Delete
End If
End With
' This code moves my user form to the left of the screen
Me.Top = (Application.Height - Me.Height) / 2
Me.Left = (Application.Width - Me.Width - 700)
' This code brings the word document to the front and positions if adjacent
' to the user form
OpenDMWQuote.Enabled = False
OpenGoznaQuote.Enabled = False
Application.WindowState = xlMaximized
End Sub
Sub SaveQuotation_Click()
'Error checks that a surname has been entered.
If Me.TextBox4.Value = "" Then
msgbox "You must enter a customer Surname to save?", vbOKCancel
If vbYes Then
Me.TextBox4.SetFocus
Exit Sub
End If
End If
'Check to see if either a DMW Quote or a GOZNA quote has been typed out
If OpenDMWQuote.Enabled = True Then
msgbox "Please produce and PRINT either a DMW or GOZNA quotation before saving to file"
Exit Sub
Else
'
'
' THIS IS WHERE I WANT TO INSERT MY CODE TO SAVE THE NEW QUOTATION IN
'
' C:\New Dashboard\Quotations" & Textbox18.Text
' I have tried for days and its driving me nuts
''
'
'
'
'
'
' Transfers the details of the quote to the 'Quotation Data' sheet
Dim erow As Integer
Dim ws As Worksheet
Set ws = Worksheets("QuotationData")
With ws
erow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Me.TextBox1.SetFocus
ws.Cells(erow, 1).Value = Me.TextBox1.Value
ws.Cells(erow, 2).Value = Me.TextBox17.Value
ws.Cells(erow, 4).Value = Me.CboTitle.Value
ws.Cells(erow, 3).Value = Me.TextBox6.Value
ws.Cells(erow, 5).Value = Me.TextBox5.Value
ws.Cells(erow, 6).Value = Me.TextBox4.Value
ws.Cells(erow, 7).Value = Me.TextBox2.Value
ws.Cells(erow, 8).Value = Me.TextBox3.Value
ws.Cells(erow, 9).Value = Me.TextBox12.Value
ws.Cells(erow, 10).Value = Me.TextBox11.Value
ws.Cells(erow, 11).Value = Me.TextBox10.Value
ws.Cells(erow, 12).Value = Me.TextBox9.Value
ws.Cells(erow, 13).Value = Me.TextBox8.Value
ws.Cells(erow, 14).Value = Me.TextBox7.Value
ws.Cells(erow, 15).Value = Me.TextBox16.Value
ws.Cells(erow, 16).Value = Me.TextBox14.Value
ws.Cells(erow, 17).Value = Me.TextBox13.Value
Me.TextBox1.Value = ""
Me.TextBox17.Value = ""
Me.CboTitle.Value = ""
Me.TextBox6.Value = ""
Me.TextBox5.Value = ""
Me.TextBox4.Value = ""
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.TextBox12.Value = ""
Me.TextBox11.Value = ""
Me.TextBox10.Value = ""
Me.TextBox9.Value = ""
Me.TextBox8.Value = ""
Me.TextBox7.Value = ""
Me.TextBox16.Value = ""
Me.TextBox14.Value = ""
Me.TextBox13.Value = ""
End With
End If
End Sub
PS Please do not at my attempt at coding to much, it is my first attempt. Thankyou in advance for any help.
Last edited by a moderator: