在Word中使用Excel创建图片

r7knjye2  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(117)

我正在从Excel工作表制作Word文档。在这方面,我想创建一个首页,包括在工作表中找到的图片“Forside”。我想把图片填满整个页面,但一直没能做到。我试过删除页边距,但每次它都删除整个文档的页边距,而不仅仅是第一页。此外,我曾试图格式的图片成顶部和底部,然后调整大小,没有运气。我希望有人能帮忙我已经发布到下面的代码。(我不是Maven,这是我第一次写代码。所以,如果你有一些改进,请随时写出来)

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

它在问题的描述中指出

wfsdck30

wfsdck301#

有两种不同的方法可以将图片(一般来说,“形状”)放置在Word文档中:As inline shape 和as shape。内联形状与文本一起流动:如果您在图像之前添加内容,则图像将随文本一起向下移动。可以使用布局按钮设置(非内联)形状的格式,例如文本围绕它流动,或者文本打印在图像的上方或下方。
当您将图像粘贴到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

相关问题