【问题标题】:Change margins in a Word.Application document from Excel从 Excel 更改 Word.Application 文档中的边距
【发布时间】:2015-04-18 23:02:09
【问题描述】:

我有 Excel VBA 可以从 Excel 复制并粘贴到 Word。更改行距的代码可以正常工作,但更改边距的代码却不行。

Option Explicit

Sub CopyXLStoDOC()

' ****************************************************************************
' *      Make sure to set a reference to Microsoft Word Object Library!!!    *
' ****************************************************************************

'  Source of code:  http://mrspreadsheets.com/1/post/2012/09/vba-code-snippet.html

    'declare local variables and constants
    Dim oDoc As Word.Document
    Dim oWord As Word.Application
    Dim rRange1 As Range, rRange2 As Range
    Const sDocPath As String = "D:\Corinne\5   RALF WORK\A_RALFS Jobs\Document2.docx"

    'set ranges to copy
    Set rRange1 = Worksheets("4_Data Form").Range("B2:K68")
    Set rRange2 = Worksheets("4_Transport").Range("C13:J53")

    'open the Word document, if it doesn't exist, then create one
    On Error Resume Next
    Set oDoc = GetObject(sDocPath)
    Set oWord = oDoc.Parent
    If Err <> 0 Then
        Set oWord = CreateObject("Word.Application")
        Set oDoc = oWord.Documents.Add
    End If
    oWord.Visible = True

    'Change Word sheet settings

    'set Page Setup properties - Page Orientation, Page Size & Margins:


     With oWord.ActiveDocument.PageSetup
        .TopMargin = CentimetersToPoints(1.8)
        .BottomMargin = CentimetersToPoints(1.8)
        .LeftMargin = CentimetersToPoints(1.8)
        .RightMargin = CentimetersToPoints(1.8)
     End With


    'copy and paste first range into Word
     rRange1.Copy
     oDoc.ActiveWindow.Selection.Paste


    'copy and paste second range into Word after pagebreak
    rRange2.Copy
    'remove the next line if you want to paste rRange2 directly after rRange1
    oDoc.ActiveWindow.Selection.InsertBreak Type:=wdPageBreak
    oDoc.ActiveWindow.Selection.Paste



    With oDoc.Content.ParagraphFormat
        .LineSpacingRule = wdLineSpaceSingle
        .SpaceAfter = 0
        .SpaceAfter = False
    End With


    'Clean up objects
    Set oDoc = Nothing
    Set rRange1 = Nothing
    Set rRange2 = Nothing

End Sub

【问题讨论】:

    标签: vba excel ms-word margins


    【解决方案1】:

    当您将范围从 Excel 复制并粘贴到 Word 文档中时,它会作为表格插入。生成的表格将调整它自己的边距以适应正在粘贴的列中的数据。您可能必须获取对结果表的引用并采用以下格式:

    '...
    
    'copy and paste first range into Word
    rRange1.Copy
    oDoc.ActiveWindow.Selection.Paste
    
    With oDoc.Tables(1)
       'apply table formatting here.
    End With
    
    'copy and paste second range into Word after pagebreak
    rRange2.Copy
    'remove the next line if you want to paste rRange2 directly after rRange1
    oDoc.ActiveWindow.Selection.InsertBreak Type:=wdPageBreak
    oDoc.ActiveWindow.Selection.Paste
    
    With oDoc.Tables(2)
        'apply table formatting here.
    End With
    
    '...
    

    【讨论】:

    • 谢谢,这将帮助我现在了解在复制/粘贴操作期间控制格式的因素
    猜你喜欢
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    相关资源
    最近更新 更多