简体   繁体   中英

VBA copy and paste macro != manual copy paste

Im trying to copy and paste a table from excel into a word document.

I can do it manually - highlight the cell, CTRL+C, go to word, CTRL+V. it works fine.

But when I write a macro to do it the cells are twice the height, like the line height in each cell gets changed for some reason. why is it different? I recorded the manual procedure and it is the same function (PasteExcelTable) being called.

Set wordDoc = wordApp.Documents.Open(wordDocPath)

With wordDoc
    ' cost report
    Dim wordRng As Word.Range
    Dim xlRng As Excel.Range
    Dim sheet As Worksheet
    Dim i As Integer
    Dim r As String

    'Copy the cost report from excel sheet
    Set sheet = ActiveWorkbook.Sheets("COST REPORT")
    i = sheet.Range("A:A").Find("TOTAL PROJECT COST", Range("A1"), xlValues, xlWhole, xlByColumns, xlNext).row
    r = "A11:M" + Trim(Str(i))

    Set xlRng = sheet.Range(r)
    xlRng.Copy

    'Copy and Paste Cost report from Excel
    Set wordRng = .Bookmarks("CostReport").Range 'remember original range

    If .Bookmarks("CostReport").Range.Information(wdWithInTable) Then
        .Bookmarks("CostReport").Range.Tables(1).Delete
    End If

    .Bookmarks("CostReport").Range.PasteExcelTable False, False, False
    .Bookmarks.Add "CostReport", wordRng    'reset range to its original positions
End With

Here is my solution:

With wordDoc
    'Paste table from Excel
    Set wordRng = .Bookmarks(bookMarkName).range 'remember original range

    If .Bookmarks(bookMarkName).range.Information(wdWithInTable) Then
        .Bookmarks(bookMarkName).range.Tables(1).Delete
    End If

    .Bookmarks(bookMarkName).range.PasteExcelTable False, False, False
    .Bookmarks.Add bookMarkName, wordRng    'reset range to its original positions

    Dim paraFmt As ParagraphFormat
    Set paraFmt = .Bookmarks(bookMarkName).range.Tables(1).range.ParagraphFormat

    paraFmt.SpaceBefore = 0
    paraFmt.SpaceBeforeAuto = False
    paraFmt.SpaceAfter = 0
    paraFmt.SpaceAfterAuto = False
    paraFmt.LineSpacingRule = wdLineSpaceSingle
    paraFmt.WidowControl = True
    paraFmt.KeepWithNext = False
    paraFmt.KeepTogether = False
    paraFmt.PageBreakBefore = False
    paraFmt.NoLineNumber = False
    paraFmt.Hyphenation = True
    paraFmt.OutlineLevel = wdOutlineLevelBodyText
    paraFmt.CharacterUnitLeftIndent = 0
    paraFmt.CharacterUnitRightIndent = 0
    paraFmt.CharacterUnitFirstLineIndent = 0
    paraFmt.LineUnitBefore = 0
    paraFmt.LineUnitAfter = 0
    paraFmt.MirrorIndents = False
    paraFmt.TextboxTightWrap = wdTightNone
    paraFmt.Alignment = wdAlignParagraphLeft

    .Bookmarks(bookMarkName).range.Tables(1).AutoFitBehavior (wdAutoFitWindow)

End With

Try this sample piece of code for me please. I tested it From VBA Excel with different table types and it gave me satisfactory results. Please amend it whereever required... for example File Name / Sheet name etc...

Sub Sample()
    Dim oWordApp As Object, oWordDoc As Object
    Dim FlName As String

    FlName = "C:\MyDoc.doc"

    '~~> Establish an Word application object
    On Error Resume Next
    Set oWordApp = GetObject(, "Word.Application")

    If Err.Number <> 0 Then
        Set oWordApp = CreateObject("Word.Application")
    End If
    Err.Clear
    On Error GoTo 0

    oWordApp.Visible = True

    Set oWordDoc = oWordApp.Documents.Open(FlName)

    With oWordDoc
        Dim xlRng As Range

        Set xlRng = Sheets(1).Range("A1:D10")
        xlRng.Copy

        .Bookmarks("CostReport").Range.PasteSpecial Link:=False, _
        Placement:=wdInLine, DisplayAsIcon:=False
    End With
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM