Excel VBA页脚图像

有没有办法使用Excel-VBA代码为了在图表中创build图片对象,以便将其作为页脚图片插入。 我试图通过创build图表对象并将其粘贴为图片格式,然后将图表导出为图像文件并将图像设置为页脚。 有没有更好的方法来插入一个图片对象作为页脚图像,如果是这样,我该怎么做?

我开始了macros录像机。 我点击Page Setup然后Header/Footer然后Custom Footer 。 我点击了中心部分,然后Format Picture (带有太阳在山上的图像的button)。 我浏览了一个图像,然后单击Insert 。 “&[图片]”出现在中间部分。 我点了OK两次。 我closures了macroslogging器。

我打印的页面和选定的图像出现在底部。

macroslogging器保存的重要代码是:

 ActiveSheet.PageSetup.CenterFooterPicture.Filename = _ "C:\Users\Public\Pictures\Sample Pictures\Desert Landscape.jpg" 

用你select的文件名replace"C:\Users\Public\Pictures\Sample Pictures\Desert Landscape.jpg"

微距logging器通常是发现这种情况的最简单的方法。

对于将来有人查看,我将分享我的代码复制一个范围,并将其保存为您的计算机上的文件,然后可以将其添加到页脚。 你可以消除你不想要的任何部分=)

  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 

尝试这个:

 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 

由macroslogging器生成的代码将使您获得部分途径,但往往如此,它不提供整体或最合适的解决scheme。 这也听起来像你正试图插入一个由Excel(如图表)生成的图像到页脚? 如果是这样的话,我相信你将不得不与图像相同的对象,然后引用该图像文件。