2012-03-29 96 views
0

有沒有辦法使用Excel-VBA代碼來製作圖紙中的圖片對象,以便將其作爲頁腳圖像插入。我試圖通過創建一個圖表對象,並在圖片格式粘貼,然後圖表導出爲圖像文件,並設置圖像的頁腳這樣做。有沒有更好的方法來將圖片對象作爲頁腳圖片插入,如果是的話,我該怎麼做?Excel VBA頁腳圖像

+1

@roshanK:好像你忽視了http://stackoverflow.com/questions/9902462/excel-vba-insert-footer-image我的評論: ) – 2012-03-29 14:01:33

+0

@SiddharthRout no..i確實關閉了所有的問題 – roshanK 2012-03-29 14:35:53

+0

是:)我提醒你後:) – 2012-03-29 16:14:34

回答

1

我開始了宏記錄器。我點擊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"與您所選擇的文件名。

微距記錄器通常是發現這樣的語句的最簡單的方法。

+0

我想要從excel表格中的圖片對象插入footerimage而不是外部圖片 – roshanK 2012-05-02 07:24:37

0

試試這個:

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生成的圖像(如圖表)插入頁腳?如果是這樣的話,我相信你將不得不與圖像相同的對象,然後引用該圖像文件。

1

對於任何人在將來查看此內容,我將分享我的代碼以複製範圍並將其另存爲計算機上的文件,然後將其添加到頁腳。您可以消除您不想要的任何位=)

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