Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

the code suggested in the answer to this question (Exporting a sheet to a word table gives a weird outcome) does not make the table fit in the margins, resulting in part of the table being hidden enter image description here

How can I force the table to fit in the margins? I have tried objDoc.Tables.AutoFitBehavior (wdAutofitWindow) but it doesn't work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
143 views
Welcome To Ask or Share your Answers For Others

1 Answer

Sub Export_Click()
    Dim objWord As Word.Application
    Dim myDoc As Word.Document
    Dim myTable As Word.Table
    Dim myRange As Excel.Range
    Dim lastRow As Long

    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True

    Set myDoc = objWord.Documents.Add

    lastRow = Sheets("export").Range("$G$1").Value  'number of lines to export

    Set myRange = Range("A1:F" & lastRow)
    myRange.Copy

    myDoc.Paragraphs(1).Range.PasteExcelTable _
        LinkedToExcel:=False, _
        WordFormatting:=False, _
        RTF:=False

    Set WordTable = myDoc.Tables(1)
    WordTable.AutoFitBehavior (wdAutoFitWindow)

    Application.CutCopyMode = False         'clear the clipboard
End Sub

This code is based on your last question.

For above code to run you'll have to add reference of Microsoft Word 12.0 Object Library. You can do this in VBE. In Tools menu click References and select Microsoft Word 12.0 Object Library


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...