以原始分辨率导出图片Excel VBA

此解决scheme: 导出图片Excel VBA

工作得很好,但它使用的图表方法被调整到表格中的图像来“截图”(在我的情况下,甚至包括表格边框),而不是实际导出图像本身。

当我通过将excel表格转换为html文件来获取图像时,他们甚至可以在文件夹中获得更好的分辨率。

有没有办法让自己的图像,他们的原始分辨率,而不是使用VBA(显然,我不只是需要的图片,否则我会满足于HTML转换方法)?

我的意思是可以在这里看到: http : //i.imgur.com/OUX9Iji.png左边的图片是我使用html转换方法得到的,右边的图片是我使用图表方法得到的。 正如你所看到的图表方法只是在Excel表格中截图,我需要它得到像左边的原始图片。

由于较新的文件types.xlsm和.xlsx实际上是一个zip文件,因此可以让工作簿保存其自身的副本,并将扩展名从.xlsm更改为.zip。 从那里,它可以查看zip的xl / media文件夹,并将包含元数据等的实际图像文件复制出来。

对于我的目的,因为它改变了图像文件名(而不是文件types)的zip内,我正在如何更具体地重新命名图像文件基于工作簿内容(即,他们在工作簿中的位置)为用户而出。

但是,是的,截图几乎没有真正的文件,这种方法做到了。 这个小组花了我相当多的时间来写,但我相信会被很多人使用!

 Private Sub ExtractAllPhotosFromFile() Dim oApp As Object, FileNameFolder As Variant, DestPath As String Dim num As Long, sZipFile As String, sFolderName As String ', iPos As Long, iLen As Long Dim vFileNameZip As Variant, strTmpFileNameZip As String, strTmpFileNameFld As String, vFileNameFld As Variant Dim FSO As Object, strTmpName As String, strDestFolderPath As String On Error GoTo EarlyExit strTmpName = "TempCopy" ' / Check requirements before beginning / / 'File must be .xlsm If Right(ActiveWorkbook.FullName, 5) <> ".xlsm" Then MsgBox ("This function cannot be completed because the filetype of this workbook has been changed from its original filetype of .xlsm" _ & Chr(10) & Chr(10) & "Save as a Microsoft Excel Macro-Enabled Workbook (*.xlsm) and try again.") Exit Sub End If 'User to choose destination folder strDestFolderPath = BrowseFolder("Choose a folder to Extract the Photos into", ActiveWorkbook.Path, msoFileDialogViewDetails) If strDestFolderPath = "" Then Exit Sub If Right(strDestFolderPath, 1) <> "\" Then strDestFolderPath = strDestFolderPath & "\" 'Prepare vars and Tmp destination strTmpFileNameZip = Environ("Temp") & "\" & strTmpName & ".zip" strTmpFileNameFld = Environ("Temp") & "\" & strTmpName Set FSO = CreateObject("Scripting.FileSystemObject") If FSO.FolderExists(strTmpFileNameFld) Then FSO.deletefolder strTmpFileNameFld End If If FSO.FileExists(strTmpFileNameZip) Then Kill strTmpFileNameZip End If Set FSO = Nothing 'Save current workbook to Temp dir as a zip file Application.StatusBar = "Saving copy of file to temp location as a zip" ActiveWorkbook.SaveCopyAs Filename:=strTmpFileNameZip 'Create a folder for the contents of the zip file strTmpFileNameFld = strTmpFileNameFld & "\" MkDir strTmpFileNameFld 'Pass String folder path variables to Variant type variables vFileNameFld = strTmpFileNameFld vFileNameZip = strTmpFileNameZip 'Count files/folders inside the zip Set oApp = CreateObject("Shell.Application") num = oApp.Namespace(vFileNameZip).Items.Count If num = 0 Then 'Empty Zip GoTo EarlyExit 'Skip if somehow is empty as will cause errors Else 'zip has files, copy out of zip into tmp folder Application.StatusBar = "Copying items from temp zip file to folder" oApp.Namespace(vFileNameFld).CopyHere oApp.Namespace(vFileNameZip).Items End If 'Copy the image files from the tmp folder to the Dest folder Application.StatusBar = "Moving Photos to selected folder" strTmpFileNameFld = strTmpFileNameFld & "xl\media\" CopyFiles strTmpFileNameFld, strDestFolderPath, "*.jpeg" CopyFiles strTmpFileNameFld, strDestFolderPath, "*.jpg" CopyFiles strTmpFileNameFld, strDestFolderPath, "*.png" CopyFiles strTmpFileNameFld, strDestFolderPath, "*.bmp" 'Function complete, cleanup 'Prepare vars and Tmp destination Application.StatusBar = "Cleaning up" strTmpFileNameZip = Environ("Temp") & "\" & strTmpName & ".zip" strTmpFileNameFld = Environ("Temp") & "\" & strTmpName Set FSO = CreateObject("Scripting.FileSystemObject") If FSO.FolderExists(strTmpFileNameFld) Then FSO.deletefolder strTmpFileNameFld End If If FSO.FileExists(strTmpFileNameZip) Then Kill strTmpFileNameZip End If Application.StatusBar = False MsgBox ("Photos extracted into the folder: " & strDestFolderPath) Set oApp = Nothing Set FSO = Nothing Exit Sub EarlyExit: Application.StatusBar = False Set oApp = Nothing Set FSO = Nothing MsgBox ("This function could not be completed.") End Sub 

我把副本移到它自己的子节点上,以节省我如何过滤文件types的空间,而不是最好的方式,但工程

 Private Sub CopyFiles(strFromPath As String, strToPath As String, FileExt As String) 'As function to get multiple filetypes Dim FSO As Object If Right(strFromPath, 1) <> "\" Then strFromPath = strFromPath & "\" On Error Resume Next Set FSO = CreateObject("scripting.filesystemobject") FSO.MoveFile Source:=strFromPath & FileExt, Destination:=strToPath Set FSO = Nothing On Error GoTo 0 End Sub 

我发现这个稳定的function在线来select一个目标文件夹,实际上很难find一个好的固体。

 Private Function BrowseFolder(Title As String, Optional InitialFolder As String = vbNullString, _ Optional InitialView As Office.MsoFileDialogView = msoFileDialogViewList) As String 'Used for the Extract Photos function Dim V As Variant Dim InitFolder As String With Application.FileDialog(msoFileDialogFolderPicker) .Title = Title .InitialView = InitialView If Len(InitialFolder) > 0 Then If Dir(InitialFolder, vbDirectory) <> vbNullString Then InitFolder = InitialFolder If Right(InitFolder, 1) <> "\" Then InitFolder = InitFolder & "\" End If .InitialFileName = InitFolder End If End If .Show On Error Resume Next Err.Clear V = .SelectedItems(1) If Err.Number <> 0 Then V = vbNullString End If End With BrowseFolder = CStr(V) End Function