Exporting to a Word Template

Status
Not open for further replies.

ph151

New Member
Joined
Jun 5, 2018
Messages
8
Hi All. (Apologies for double posting, realised had posted in wrong forum and cant see how to move it - Still learning)

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

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Duplicate: https://www.mrexcel.com/forum/gener...uestions/1059206-exporting-word-template.html

Please do not post the same question multiple times. All clarifications, follow-ups, and bumps should be posted back to the original thread. Per forum rules, posts of a duplicate nature will be locked or deleted (rule 12 here: Forum Rules).

If there is an issue with a thread (like it needs to be moved), click on the Report button in the lower left corner and add a message for the Moderator team to see.
However, I think your original post is fine where it is, since it involves MS Word.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,225,476
Messages
6,185,202
Members
453,283
Latest member
Shortm88

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