0% found this document useful (0 votes)
3 views3 pages

Sub CreateWordTableFromExcelDataWithBordersAndWidthInCentimeters

This document contains a VBA macro that creates a Word table from a specified Excel range, formatting it with borders and specific column widths in centimeters. It sets the Word document to landscape orientation and populates the table with data from Excel. Finally, it saves the document to a specified file path and cleans up the objects used.

Uploaded by

Roshan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Sub CreateWordTableFromExcelDataWithBordersAndWidthInCentimeters

This document contains a VBA macro that creates a Word table from a specified Excel range, formatting it with borders and specific column widths in centimeters. It sets the Word document to landscape orientation and populates the table with data from Excel. Finally, it saves the document to a specified file path and cleans up the objects used.

Uploaded by

Roshan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Sub CreateWordTableFromExcelDataWithBordersAndWidthInCentimeters()

Dim objWord As Object

Dim objDoc As Object

Dim ExcelRng As Range

Dim WordTable As Object

Dim i As Long, j As Long

' Create a new Word application

On Error Resume Next ' Ignore errors if Word is not open

Set objWord = GetObject(, "Word.Application")

On Error GoTo 0

If objWord Is Nothing Then

Set objWord = CreateObject("Word.Application")

End If

objWord.Visible = False ' You can set this to False to run Word in the background

' Add a new document

Set objDoc = objWord.Documents.Add

' Set page orientation to landscape

objDoc.PageSetup.Orientation = 1 ' 1 represents landscape, 0 represents portrait

' Define the Excel range containing the data

Set ExcelRng = ThisWorkbook.Sheets("Sheet3").Range("A3:I18") ' Change the sheet and range as


needed

' Create a table in Word at the beginning of the document


Set WordTable = objDoc.Tables.Add(objDoc.Range(0, 0), ExcelRng.Rows.Count,
ExcelRng.Columns.Count)

' Populate the Word table with data from Excel and apply borders

WordTable.Borders.Enable = True ' Enable table borders

' Set font size and font for the entire table

WordTable.Range.Font.Size = 9

WordTable.Range.Font.Name = "Times New Roman"

' Set the width of specific columns in centimeters (adjust the values as needed)

WordTable.Columns(1).Width = objWord.CentimetersToPoints(0.7) ' Adjust the width as needed


(3 centimeters)

WordTable.Columns(2).Width = objWord.CentimetersToPoints(2.25) ' Adjust the width as needed


(3 centimeters)

WordTable.Columns(3).Width = objWord.CentimetersToPoints(1.99) ' Adjust the width as needed


(3 centimeters)

WordTable.Columns(4).Width = objWord.CentimetersToPoints(2.99) ' Adjust the width as needed


(3 centimeters)

WordTable.Columns(5).Width = objWord.CentimetersToPoints(2.99) ' Adjust the width as needed


(3 centimeters)

' Populate the Word table with data from Excel and add borders

For i = 1 To ExcelRng.Rows.Count

For j = 1 To ExcelRng.Columns.Count

WordTable.Cell(i, j).Range.Text = ExcelRng.Cells(i, j).Value

Next j

Next i
' Loop to add extra columns

For j = 1 To 3

WordTable.Columns.Add

Next j

' Save the Word document

objDoc.SaveAs "C:\Users\RIO\Desktop\GIST\FileName.docx" ' Change the file path and name

' Close and clean up

objDoc.Close

objWord.Quit

Set objDoc = Nothing

Set objWord = Nothing

End Sub

You might also like