Setting New Word Document Created From Excel VBA to Landscape Orientation

  Kiến thức lập trình

I have an excel table that I am attempting to create code to export that data to a new MSWord Document, two-columns per page, LANDSCAPE orientation. Admittedly, I am a noob so bear with me. My code is below. I am able to create the two columns in the doc without issues, but the document that is created is not being set to landscape orientation.

Please ignore all code beneath where I am attempting to format the document as that is still WIP. Thanks for any guidance (be gentle).

Sub generate_word_doc()

Dim objWord
Dim objDoc
Dim objSelection
Dim i, j As Integer
Dim ws As Worksheet
Dim row_count, col_count As Integer

Set ws = ThisWorkbook.Sheets("Roster")
ws.Activate
row_count = WorksheetFunction.CountA(Range("C1", Range("C1").End(xlDown)))
col_count = WorksheetFunction.CountA(Range("C1", Range("C1").End(xlToRight)))

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
Set objSelection = objWord.Selection

objWord.Visible = True
objWord.Activate

With objDoc
    .PageSetup.Orientation = wdOrientLandscape
    .PageSetup.TextColumns.SetCount NumColumns:=2
    .PageSetup.LeftMargin = 18
    .PageSetup.MirrorMargins = True

End With

Set RosterTable = objDoc.Tables.Add(objSelection.Range, row_count, col_count)

With RosterTable
    
    With .Borders
        .enable = True
        .outsidecolor = RGB(0, 0, 0)
        .insidecolor = RGB(0, 0, 0)
        
    End With
    .Rows(1).shading.backgroundpatterncolor = RGB(221, 221, 221)

    For i = 0 To row_count
       
        For j = 1 To col_count
        .cell(i, j).Range.InsertAfter ws.Cells(i + 2, j + 2).Text
        Next j
    
    Next i

End With

End Sub

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT