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