File created by macro won't open

Deekappa

New Member
Joined
Nov 19, 2018
Messages
12
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!


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
</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>
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Only thing that looks 'odd' to me is the fact you add the extension using ".docm" and yet the extension is set when you use 'FileFormat:=wdFormatXMLDocument' I'm not very well up on word documents but I know when I save as Excel I don't need to provide the extension.

 
Upvote 0
Good news. Can be frustrating sometimes when it appears to work but in reality hasn't.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top