1. 程式人生 > >excel中vba將excel中數字和圖表輸出到word中

excel中vba將excel中數字和圖表輸出到word中

exe 如果 bmp view star path zed img delete

參考:https://wenku.baidu.com/view/6c60420ecc175527072208af.html


比如將選區變為圖片保存到桌面:

技術分享
 1 Sub 將選區轉為圖片存到桌面()
 2   Dim ans As Byte, Pic As String, Paths As String
 3   On Error Resume Next
 4   Paths = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\" 記錄“桌面”文件夾的路徑
 5 star:
 6   選擇導出圖片的格式
 7   ans = Application.InputBox("
輸入1:保存為BMP圖片;" + Chr(10) + "輸入2:保存為PNG圖片;" + Chr(10) + "輸入3:保存為JPG圖片。", "圖片格式", 1, , , , , 1) 8 If err <> 0 Then MsgBox "只能輸入1到3", 64, "提示": err.Clear: GoTo star 如果有誤(輸入的值在0-255之外)則返回重新輸入 9 If ans < 1 Or ans > 3 Then MsgBox "輸入錯誤": GoTo star 如果不等於1、2、3則重新輸入 10 Pic = Choose(ans, ".BMP
", ".PNG", ".JPG") 在三種格式間選擇 11 If TypeName(Selection) = "Range" Then 如果選擇的對象是單元格 12 Dim rng As Range 13 Set rng = Selection 將選區賦與變量rng 14 rng.CopyPicture Appearance:=xlScreen, Format:=xlPicture 將rng區域復制為圖片 15 ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count + 1).Cells(1
).Select 選擇一個空單元格 16 With ActiveSheet.ChartObjects.Add(0, 0, rng.Width, rng.Height).Chart 生成圖表 17 .Paste 將圖片粘貼到圖表中 18 .Export Paths & Replace(rng.Address(0, 0), ":", "-") & Pic 將圖表導出為圖片文件 19 .Parent.Delete 刪除圖表對象 20 End With 21 rng.Select 選擇rng對象 22 End If 23 Shell "EXPLORER.EXE " & Left(Paths, Len(Paths) - 1), vbMaximizedFocus 打開桌面 24 End Sub
View Code

excel中vba將excel中數字和圖表輸出到word中