Hi Team,
I have a macro in my workbook, which creates a new word document based on a template, and populates sections of the template with data from my workbook. We use it for producing quote letters based on figures in excel.
The macro seems to work fine, it creates the document, populates it correctly and saves it in the correct location. The only issue is that once I close the word document, it won't open again, whenever I try to open it, Word opens with no file. I've tried opening it through different methods, but having no luck.
Is there anything wrong with the code, something missing?
Thanks in advance!
</quote_number></staff_member></quote_number></job_address_line2></job_address_line1></contact_email></contact_person></billing_address_line2></billing_address_line1></client_name></quote_number>
I have a macro in my workbook, which creates a new word document based on a template, and populates sections of the template with data from my workbook. We use it for producing quote letters based on figures in excel.
The macro seems to work fine, it creates the document, populates it correctly and saves it in the correct location. The only issue is that once I close the word document, it won't open again, whenever I try to open it, Word opens with no file. I've tried opening it through different methods, but having no luck.
Is there anything wrong with the code, something missing?
Thanks in advance!
Code:
Sub CreateQuoteLetter()
Dim wApp As Word.Application
Dim wDoc As Word.Document
Dim thisWb As Workbook
Set thisWb = ActiveWorkbook
Set wApp = CreateObject("Word.Application")
wApp.Visible = True
Set wDoc = wApp.Documents.add(Template:="I:\Mark B\New Job Template Resources\Custom Office Templates\Certifed Quote Template.dotm", NewTemplate:=False, DocumentType:=0)
With wDoc
.Application.Selection.Find.Text = "<quote_number>"
.Application.Selection.Find.Execute
.Application.Selection = Worksheets("Job Information").Range("K4")
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<client_name>"
.Application.Selection.Find.Execute
.Application.Selection = Worksheets("Job Information").Range("C3")
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<billing_address_line1>"
.Application.Selection.Find.Execute
.Application.Selection = Worksheets("Job Information").Range("C5")
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<billing_address_line2>"
.Application.Selection.Find.Execute
.Application.Selection = Worksheets("Job Information").Range("C6")
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<contact_person>"
.Application.Selection.Find.Execute
.Application.Selection = Worksheets("Job Information").Range("C4")
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<contact_email>"
.Application.Selection.Find.Execute
.Application.Selection = Worksheets("Job Information").Range("C11")
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<job_address_line1>"
.Application.Selection.Find.Execute
.Application.Selection = Worksheets("Job Information").Range("C7")
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<job_address_line2>"
.Application.Selection.Find.Execute
.Application.Selection = Worksheets("Job Information").Range("C8")
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<quote_number>"
.Application.Selection.Find.Execute
.Application.Selection = Worksheets("Job Information").Range("K4")
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<staff_member>"
.Application.Selection.Find.Execute
.Application.Selection = Worksheets("Job Information").Range("K3")
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<quote_number>"
.Application.Selection.Find.Execute
.Application.Selection = Worksheets("Job Information").Range("K4")
.Application.Selection.EndOf
.SaveAs2 Filename:=thisWb.Path & ("\1. Quotation\") & Worksheets("Job Information").Range("K4") & (".docm"), _
FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False
End With
End Sub