在 Word 中设置图片格式以填充文档的整个第一页

问题描述 投票:0回答:1

我正在努力从 Excel 工作表制作 Word 文档。

我想要一个由“Forside”工作表中的图片组成的首页。
我希望图片填满整个页面。

我尝试删除页边距,但这会删除整个文档中的页边距,而不是仅删除第一页。
我尝试将图片格式化为顶部和底部,然后调整其大小。


Sub PrintAfWorddokument()
    Dim wdApp As Word.Application
    Dim ws As Worksheet
    Dim wdDoc As Word.Document
    Dim cht1 As ChartObject
    Dim cht2 As ChartObject
    Dim cht3 As ChartObject ' New chart object
    Dim wdRng As Word.Range
    Dim rngA25 As Range
    Dim pic As Shape ' New picture shape
    Dim img As Object

    
    ' Create a new instance of Word and make it visible
    Set wdApp = New Word.Application
    wdApp.Visible = True
    
    ' Activate Word
    wdApp.Activate
    
    ' Create a new Word document
    Set wdDoc = wdApp.Documents.Add
       
    Set ws = ThisWorkbook.Sheets("Forside")
    
    On Error Resume Next
    Set img = ws.Shapes("Picture 1")
    
    If img Is Nothing Then
        MsgBox "Picture not found in the 'Forside' sheet.", vbExclamation
        wdDoc.Close False
        wdApp.Quit
        Set wdDoc = Nothing
        Set wdApp = Nothing
        Exit Sub
    End If
    
    img.Copy
    
    wdApp.Selection.Paste

    
    ' Paste data from cell A1:A21
    Set ws = ThisWorkbook.Sheets("Til Word Dokument")
    ws.Range("A1:A21").Copy
    wdApp.Selection.Paste
    
    ' Copy the charts
    Set cht1 = ws.ChartObjects("Chart1")
    Set cht2 = ws.ChartObjects("Chart2")
    
    ' Add a new page for the charts
    wdDoc.Sections.Add
    
    ' Insert the text "Formue oversigt" above the charts with formatting
    Set wdRng = wdDoc.Sections(wdDoc.Sections.Count).Range
    wdRng.Text = "Formue oversigt"
    wdRng.Font.Name = "Calibri"
    wdRng.Font.Size = 20
    wdRng.Font.Bold = True
    wdRng.Font.Underline = wdUnderlineSingle
    wdRng.ParagraphFormat.Alignment = wdAlignParagraphCenter
    wdRng.InsertParagraphAfter
    
    ' Set the width and height of both charts (adjust as needed)
    cht1.Width = InchesToPoints(3) ' Adjust as needed
    cht1.Height = InchesToPoints(3) ' Adjust as needed
    cht2.Width = InchesToPoints(3) ' Adjust as needed
    cht2.Height = InchesToPoints(3) ' Adjust as needed

    ' Insert the first chart
    wdRng.Collapse Direction:=wdCollapseEnd
    cht1.Copy
    wdRng.Paste
    wdRng.ParagraphFormat.Alignment = wdAlignParagraphCenter
    
    ' Insert the second chart beside the first chart
    cht2.Copy
    wdRng.Collapse Direction:=wdCollapseEnd
    wdRng.Paste
    wdRng.ParagraphFormat.Alignment = wdAlignParagraphCenter
    
       ' Insert a paragraph break after the new chart
    wdRng.Collapse Direction:=wdCollapseEnd
    wdRng.InsertParagraphAfter
    
        ' Set the range for cell A25
    Set ws = ThisWorkbook.Sheets("Til Word Dokument") ' Switch back to the original worksheet
    Set rngA25 = ws.Range("A25")
    
    ' Copy cell A25 with the same formatting as "Formue oversigt"
    rngA25.Copy
    wdRng.Collapse Direction:=wdCollapseEnd
    wdRng.Paste
    wdRng.ParagraphFormat.Alignment = wdAlignParagraphCenter
    
    ' Insert a paragraph break to separate the charts from the text
    wdRng.Collapse Direction:=wdCollapseEnd
    wdRng.InsertParagraphAfter
    
    ' Insert the text "Chart from Beregning (optimal)" on a new page
    wdDoc.Sections.Add
    Set wdRng = wdDoc.Sections(wdDoc.Sections.Count).Range
    wdRng.Text = "Formue udvikling"
    wdRng.Font.Name = "Calibri"
    wdRng.Font.Size = 20
    wdRng.Font.Bold = True
    wdRng.Font.Underline = wdUnderlineSingle
    wdRng.ParagraphFormat.Alignment = wdAlignParagraphCenter
    wdRng.InsertParagraphAfter
    
    ' Set the new width and height of the chart (adjust as needed)
    Set ws = ThisWorkbook.Sheets("Beregning (optimal)") ' Use the correct worksheet name
    Set cht3 = ws.ChartObjects("Chart 1") ' Change to the correct chart name
    cht3.Width = InchesToPoints(7) ' Adjust as needed
    cht3.Height = InchesToPoints(6) ' Adjust as needed
    
    ' Insert the new chart
    cht3.Copy
    wdRng.Collapse Direction:=wdCollapseEnd
    wdRng.Paste
    wdRng.ParagraphFormat.Alignment = wdAlignParagraphCenter
    
End Sub
excel vba ms-word
1个回答
0
投票

有两种不同的方法可以在 Word 文档上放置图片(一般来说,“形状”)之类的内容:作为 内联形状 和作为 形状。内联形状随文本流动:如果在图像之前添加某些内容,图像将随文本一起向下移动。可以使用布局按钮设置(非内联)形状的格式,例如文本围绕其流动,或者文本打印在图像上方或下方。

当您将图像粘贴到Word中时,它将被粘贴为内联形状。由于内联形状与文本一起流动,因此它们也尊重页边距,并且您不能使它们大于文本所在的区域(您不能将它们放置在页边距上)。您唯一的选择是为包含图像的页面定义一个自己的部分,边距 = 0。

(非内联)形状可以调整为您想要的任何大小。您所要做的就是将内联形状转换为形状 - 这很简单,只需使用

ConvertToShape
方法即可。之后,很容易设置该形状的大小。

以下代码片段会将第一个内联形状转换为形状并将其大小调整为全屏。之后,它会添加一个分页符,这样后面的内容就不会放在同一页上。

' (your code): 
img.Copy
wdApp.Selection.Paste

' Get shape object. If inline shape, convert it to shape
If wdDoc.InlineShapes.Count > 0 Then 
    Dim sh As Shape
    Set sh = wdDoc.InlineShapes(1).ConvertToShape
    ' Shape fullsize
    With wdDoc.PageSetup
        sh.Left = -.LeftMargin
        sh.Width = .PageWidth
        sh.Top = -.TopMargin
        sh.Height = .PageHeight
    End With

    ' Add a space and a Pagebreak to continue on the next page
    wdApp.Selection.TypeText Text:=" "
    wdApp.Selection.InsertBreak Type:=wdPageBreak
End If
© www.soinside.com 2019 - 2024. All rights reserved.