Exporting to a Word Template

ph151

New Member
Joined
Jun 5, 2018
Messages
8
Hi All.


I am relatively new to VBA coding and am trying to export a table (Column) to a word document, this part I can do successfully, and save it to a specified location with a name.


I did a search and found a topic covering this, however, when I tried to implement the suggestions it did not work, hence why I am here asking for help or some direction. (See https://www.mrexcel.com/forum/gener...l-data-word-document-based-word-template.html)

What I would like to be able to do is when exporting the file is to have a specific template with headers and footers to open so the report is branded.

The area below is the part giving me issues and in my VBE is showing up red and giving me a syntax error.

Hope that someone could help me out here, it would be very much appreciated.

Many Thanks,

P

My code is:

Code:
Sub Report_To_WdTemplate()


    Dim wdApp As Object
    Dim wd As Object
    Dim selectedRange As String


    On Error Resume Next
    
    Set wdApp = GetObject(, "Word.Application")
    
    If Err.Number <> 0 Then
        Set wdApp = CreateObject("Word.Application")
    End If
    
    On Error GoTo 0


    'Set page orientation in Word
    Const wdOrientPortrait = 0
    'Const wdOrientLandscape = 1


    wdApp.Visible = True
    'Set wd = wdApp.Documents.Add


    Set wd = wdApp.Documents.Open Filename:="C:\FilePath\ReportTemplate.dotx"   


    wd.PageSetup.Orientation = 0


    'Column "B" in worksheet to be set to 675


    wd.PageSetup.TopMargin = wdApp.InchesToPoints(0.6)
    wd.PageSetup.BottomMargin = wdApp.InchesToPoints(0.6)
    wd.PageSetup.LeftMargin = wdApp.InchesToPoints(0.6)
    wd.PageSetup.RightMargin = wdApp.InchesToPoints(0.6)
          
    Sheets("Report").Select
    ActiveWindow.DisplayGridlines = False
    Range("Name Range").Copy
    wdApp.Selection.Paste
    wdApp.Selection.TypeParagraph
    ActiveWindow.DisplayGridlines = True


    Application.CutCopyMode = False


    ' Save word document with predetermined name


     wd.SaveAs ("\\P:\FilePath\Filename.docx"), FileFormat:=wdFormatDocumentDefault


    ' ActiveWorkbook.Close False
 
    Call Next Routine
  
End Sub
 
Last edited:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,214,648
Messages
6,120,726
Members
448,987
Latest member
marion_davis

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