2015-10-14 97 views
0

我有一個excel用戶窗體,它允許您將excel圖表保存在某個工作表中,並將其保存到.jpg圖像中,同時在用戶窗體中顯示圖表。但是我發現很難控制所述圖像的分辨率。分辨率似乎取決於工作表上的縮放量。當我放大時,我會得到高質量的圖像,而當我縮小時分辨率變得非常差。有沒有辦法使用vba代碼來控制它?導出圖表的控制分辨率

我使用了出口的代碼如下:

Private Sub CmdBrowse_Click() 
Dim Directory1 As String 

With Application.FileDialog(msoFileDialogFolderPicker) 
    .AllowMultiSelect = False 
    .Show 
    On Error Resume Next 
    Directory1 = .SelectedItems(1) 
    Err.Clear 
    On Error GoTo 0 
End With 

ChartDest.Value = Directory1 
End Sub 

Private Sub CmdLoad_Click() 
Dim FilePath As String 
Dim Imagename As String 
Dim ChartNumber As Integer 

If ChartDest = "Select chart destination folder" Then 
    MsgBox "Select chart destination" 
    Exit Sub 
End If 

ChartNumber = ChartList.ListIndex + 1 

'saving chart to image 
Imagename = ChartList.Value 
FilePath = ChartDest & Imagename & ".jpeg" 
ThisWorkbook.Worksheets("Blad3").ChartObjects(ChartNumber).Chart.Export FilePath, "jpg" 
'loading image 
UserForm4.ChartImage.Picture = LoadPicture(FilePath) 
End Sub 

ChartDest與目標路徑文本框。 Chartlist與繳費圖表列表的列表框

回答

1

在CmdLoad試試這個

Private Sub CmdLoad_Click() 
    Dim fPath As String, imgName As String, chartID As Long 

    If chartDest = "Select chart destination folder" Then 
     MsgBox "Select chart destination" 
     Exit Sub 
    End If 
    chartID = ChartList.ListIndex + 1 

    'saving chart to image 
    imgName = ChartList.Value 
    fPath = chartDest & imgName & ".jpeg" 
    With ThisWorkbook.Worksheets("Blad3").ChartObjects(chartID) 
     ActiveWindow.Zoom = 175 
     .Chart.Export fPath, "jpg" 
     ActiveWindow.Zoom = 100 
    End With 

    'loading image 
    UserForm4.ChartImage.Picture = LoadPicture(fPath) 
End Sub 
+0

我選擇使用'activewindow.zoom'方法,因爲'scaleheight'和'scalewidth'是對工作方法形狀,而不是圖表或圖表對象。感謝您的解決方案。 – Antares797