[英]Excel VBA Footer Image
有没有一种方法可以使用Excel-VBA代码在表中制作图片对象,以便将其作为页脚图像插入。 我试图通过创建图表对象并将其粘贴为图片格式,然后将图表导出到图像文件并将图像设置为页脚来做到这一点。 有没有更好的方法将图片对象作为页脚图像插入,如果是的话,我该怎么做?
对于以后再查看此文档的任何人,我将共享我的代码以复制范围并将其另存为计算机上的文件,然后可以将其添加到页脚中。 您可以消除不需要的任何内容=)
Dim objPic As Shape
Dim objChart As Chart
Dim strTimeStamp As String
Dim strFileDest As String
20 Sheets(2).Activate
30 Sheets(2).Columns("R:T").AutoFit
40 Sheets(2).Rows("17:21").AutoFit
50 ActiveWindow.DisplayGridlines = False
60 Call Sheets(2).Range("S17", "U21").CopyPicture(xlScreen, xlPicture)
70 ActiveWindow.DisplayGridlines = True
80 Sheets(2).Shapes.AddChart
90 Sheets(2).Activate
100 Sheets(2).Shapes.Item(1).Select
110 Set objChart = ActiveChart
120 ActiveChart.Parent.Name = "FooterChart"
' For some reason, Excel occasionally tries to make an actual chart out of these strings.
' It's just a nonsensical chart that messes the footer up but I'm having trouble duplicating the issue and figuring out what causes it.
' This should always work. Don't use .Clear, it crashes.
130 ActiveChart.ChartArea.ClearContents
140 objChart.Paste
150 Selection.Name = "FooterImage"
160 ActiveSheet.ChartObjects("FooterChart").Activate
170 Sheets(2).Shapes.Item(1).Line.Visible = msoFalse
180 Sheets(2).Shapes.Item(1).Height = Range("S17", "U21").Height
190 Sheets(2).Shapes.Item(1).Width = Range("S17", "U21").Width
200 ActiveChart.Shapes.Range(Array("FooterImage")).Height = Range("S17", "U21").Height
210 ActiveChart.Shapes.Range(Array("FooterImage")).Width = Range("S17", "U21").Width
220 Sheets(2).Shapes.Item(1).Height = Sheets(2).Shapes.Item(1).Height * 1.25
230 Sheets(2).Shapes.Item(1).Width = Sheets(2).Shapes.Item(1).Width * 1.25
240 ActiveChart.Shapes.Range(Array("FooterImage")).Height = ActiveChart.Shapes.Range(Array("FooterImage")).Height * 1.2
250 ActiveChart.Shapes.Range(Array("FooterImage")).Width = ActiveChart.Shapes.Range(Array("FooterImage")).Width * 1.2
260 strTimeStamp = CStr(Format(Now(), "yyyymmddHhNnSs"))
270 strFileDest = "D:\Temp" & strTimeStamp & ".jpg"
280 objChart.Export strFileDest
290 InsertPicture strFileDest
300 If Len(Dir$(strFileDest)) > 0 Then
310 Kill strFileDest
320 End If
330 Sheets(2).Shapes.Item(1).Delete
我启动了宏记录器。 我单击Page Setup
然后单击Header/Footer
然后单击Custom Footer
。 我单击了中间部分,然后单击“设置Format Picture
(带有太阳在山上的图像的按钮)。 我浏览了图片,然后单击“ Insert
。 “&[图片]”出现在中间部分。 我单击OK
两次。 我关闭了宏录制器。
我打印了页面,选定的图像显示在底部。
宏记录器保存的重要代码是:
ActiveSheet.PageSetup.CenterFooterPicture.Filename = _
"C:\Users\Public\Pictures\Sample Pictures\Desert Landscape.jpg"
用您选择的文件名替换"C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert Landscape.jpg"
。
宏记录器通常是发现这样的语句的最简单方法。
尝试这个:
Dim ws as Worksheet
Set ws = Worksheets("YourWorksheetName")
With ws.PageSetup
.CenterFooterPicture = "&G" 'Specifies that you want an image in your footer
.CenterFooterPicture.Filename = "C:\Pictures\MyFooterImage.jpg" 'specifies the image file you want to use
End With
宏记录器生成的代码可以帮助您解决问题,但通常情况下,它不能提供完整或最合适的解决方案。 听起来您还想将Excel生成的图像(例如图表)插入页脚? 如果是这样,我认为您必须将对象与图像相同,然后再引用该图像文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.