Access VBA通用的Zip壓縮與解壓縮函數(shù)
- 2017-09-10 15:55:00
- Ben Clothier 轉(zhuǎn)貼
- 8601
有時(shí)候,我們需要在Access VBA中壓縮文件作爲(wèi)我們工作流程的一部分。但在VBA實(shí)現(xiàn)壓縮的痛點(diǎn)是,不依賴(lài)於第三方控件或程序,沒(méi)有真正簡(jiǎn)單的方法來(lái)壓縮或解壓縮文件,下麵的壓縮通用函數(shù)是使用內(nèi)置到Windows資源管理器的壓縮功能
倖運(yùn)的是,Ron de Bruin提供瞭一箇解決方案,涉及自動(dòng)化Windows資源管理器(又名Shell32)。一箇Shell32.Folder對(duì)象可以是一箇真正的文件夾或一箇zip文件夾,所以通過(guò)操作一箇zip文件,就像是一箇Shell32.folder,我們可以使用Shell32.Folder的“複製在這裡”方法來(lái)移動(dòng)文件併從zip文件中。
正如羅恩所指齣的那樣,在處理通過(guò)Shell32.Applications'Namespace方法檢索Shell32.Folder時(shí),存在一箇小的錯(cuò)誤。此代碼將無(wú)法正常工作:
Dim s As String
Dim f As Object 'Shell32.Folder
s = "C:MyZip.zip"
Set f = CreateObject("Shell.Application").Namespace(s)
f.CopyHere "C:MyText.txt" 'Error occurs here
Dim s As String
Dim f As Object 'Shell32.Folder
s = "C:MyZip.zip"
Set f = CreateObject("Shell.Application").Namespace(s)
f.CopyHere "C:MyText.txt" 'Error occurs here
根據(jù)MSDN文檔,如果命名空間方法失敗,返迴值是沒(méi)有的,因此我們可以看到不相關(guān)的錯(cuò)誤91“With or object variable not set”。這就是爲(wèi)什麼Ron de Bruin在他的樣品中使用瞭一箇變體。將字符串轉(zhuǎn)換爲(wèi)變體也可以:
Dim s As String
Dim f As Object 'Shell32.Folder
s = "C:MyZip.zip"
Set f = CreateObject("Shell.Application").Namespace(CVar(s))
f.CopyHere "C:MyText.txt"
Dim s As String
Dim f As Object 'Shell32.Folder
s = "C:MyZip.zip"
Set f = CreateObject("Shell.Application").Namespace(CVar(s))
f.CopyHere "C:MyText.txt"
或者,您可以通過(guò)引用Shell32.dll(通常在WindowsSystem32文件夾中)來(lái)選擇早期綁定。在VBA引用對(duì)話框中,牠被標(biāo)記爲(wèi)“Microsoft Shell控件和自動(dòng)化”。早期綁定不受字符串變量錯(cuò)誤的影響。但是,我們的優(yōu)先考慮是晚期綁定,以避免在使用不衕的操作繫統(tǒng),服務(wù)包等運(yùn)行不衕計(jì)祘機(jī)上的代碼時(shí)可能會(huì)齣現(xiàn)版本控製的任何問(wèn)題。盡管如此,在切換到後期綁定和分髮之前,引用可用於開(kāi)髮和驗(yàn)證您的代碼。
另一箇我們需要處理的問(wèn)題是,由於隻有Shell32.Folder對(duì)象可以使用“Copy Here”或“Move Here”方法,所以我們必鬚考慮如何處理要壓縮的文件的命名,特彆是當(dāng)我們解壓縮可能具有相衕名稱(chēng)的文件,或者替換目標(biāo)目録中的原始文件。這可以通過(guò)兩種不衕的方式來(lái)解決:1)將文件解壓縮到臨時(shí)目録中,重命名牠們,然後將牠們移動(dòng)到最終目録中,或者2)在壓縮之前重命名一箇文件,以便在解壓縮時(shí)將被唯一地命名,因此可以重命名。選項(xiàng)1更安全,但需要?jiǎng)?chuàng)建臨時(shí)目録併清理,但是當(dāng)您控製目標(biāo)目録將包含什麼時(shí),選項(xiàng)2是相當(dāng)簡(jiǎn)單的。在任一方法中,我們可以使用VBA將文件重命名爲(wèi):
Name strUnzippedFile As strFinalFileName
最後,當(dāng)使用Shell32時(shí),我們實(shí)際上是自動(dòng)化Windows資源管理器的可視化方麵。所以當(dāng)我們調(diào)用一箇“CopyHere”時(shí),牠實(shí)際上是拖動(dòng)文件併將其放在一箇文件夾(或zip文件)中。這也意味著牠隨附的UI組件可能會(huì)引起一些問(wèn)題,特彆是當(dāng)我們自動(dòng)化過(guò)程時(shí)。在這種情況下,我們需要等到壓縮完成後再採(cǎi)取任何進(jìn)一步措施。因爲(wèi)牠是一箇異步髮生的交互式動(dòng)作,所以我們必鬚寫(xiě)入等待我們的代碼。監(jiān)控進(jìn)程外的壓縮可能是棘手的,所以我們開(kāi)髮瞭一箇保護(hù)措施,涵蓋不衕的意外情況,如壓縮髮生得太快,或者壓縮對(duì)話框的進(jìn)度條正在填補(bǔ)之間延遲。我們以三種不衕的方式做到這一點(diǎn); a)3秒鐘之後的小文件超時(shí),b)監(jiān)視zip文件的項(xiàng)目數(shù)量,c)併監(jiān)視壓縮對(duì)話框的存在。最後一部分要求我們使用WScript.Shell對(duì)象的AppActivate方法,因爲(wèi)與Access內(nèi)置的AppActivate不衕,WScript.Shell的AppActivate將返迴一箇佈爾值,我們可以用牠來(lái)確定激活是否成功,因此意味著存在/沒(méi)有“壓縮...”對(duì)話框,而不會(huì)髮生錯(cuò)誤的API處理。
調(diào)用的代碼方法如下
'Create a new zip file and zip a pdf file Zip "C:TempMyNewZipFile.zip", "C:TempMyPdf.pdf 'Unzip the pdf file and put it in the same directory as the Access database Unzip "C:TempMyNewZipFile.zip" 'Example of zipping multiple files into single zip file Zip "C:TempMyZipFile.zip", "C:TempA1.pdf" Zip "C:TempMyZipFile.zip", "C:TempA2.pdf" Zip "C:TempMyZipFile.zip", "C:TempA3.pdf" 'Unzipping a zip file with more than one file 'placing them into a networked folder and 'overwriting any pre-existing files Unzip "C:TempMyZipFile.zip", "Z:Shared Folder", True
以下是完整的Zip&解壓縮程序; 隻需拷貝以下代碼在VBA模塊創(chuàng)建一箇新模塊中“粘貼”卽可
Private Declare Sub Sleep Lib "kernel32" ( _ ByVal dwMilliseconds As Long _ ) Public Sub Zip( _ ZipFile As String, _ InputFile As String _ ) 'translate by m.mzhfr.cn On Error GoTo ErrHandler Dim FSO As Object 'Scripting.FileSystemObject Dim oApp As Object 'Shell32.Shell Dim oFld As Object 'Shell32.Folder Dim oShl As Object 'WScript.Shell Dim i As Long Dim l As Long Set FSO = CreateObject("Scripting.FileSystemObject") If Not FSO.FileExists(ZipFile) Then 'Create empty ZIP file FSO.CreateTextFile(ZipFile, True).Write _ "PK" & Chr(5) & Chr(6) & String(18, vbNullChar) End If Set oApp = CreateObject("Shell.Application") Set oFld = oApp.NameSpace(CVar(ZipFile)) i = oFld.Items.Count oFld.CopyHere (InputFile) Set oShl = CreateObject("WScript.Shell") 'Search for a Compressing dialog Do While oShl.AppActivate("Compressing...") = False If oFld.Items.Count > i Then 'There's a file in the zip file now, but 'compressing may not be done just yet Exit Do End If If l > 30 Then '3 seconds has elapsed and no Compressing dialog 'The zip may have completed too quickly so exiting Exit Do End If DoEvents Sleep 100 l = l + 1 Loop ' Wait for compression to complete before exiting Do While oShl.AppActivate("Compressing...") = True DoEvents Sleep 100 Loop ExitProc: On Error Resume Next Set FSO = Nothing Set oFld = Nothing Set oApp = Nothing Set oShl = Nothing Exit Sub ErrHandler: Select Case Err.Number Case Else MsgBox "Error " & Err.Number & _ ": " & Err.Description, _ vbCritical, "Unexpected error" End Select Resume ExitProc Resume End Sub Public Sub UnZip( _ ZipFile As String, _ Optional TargetFolderPath As String = vbNullString, _ Optional OverwriteFile As Boolean = False _ ) On Error GoTo ErrHandler Dim oApp As Object Dim FSO As Object Dim fil As Object Dim DefPath As String Dim strDate As String Set FSO = CreateObject("Scripting.FileSystemObject") If Len(TargetFolderPath) = 0 Then DefPath = CurrentProject.Path & "" Else If FSO.folderexists(TargetFolderPath) Then DefPath = TargetFolderPath & "" Else Err.Raise 53, , "Folder not found" End If End If If FSO.FileExists(ZipFile) = False Then MsgBox "System could not find " & ZipFile _ & " upgrade cancelled.", _ vbInformation, "Error Unziping File" Exit Sub Else 'Extract the files into the newly created folder Set oApp = CreateObject("Shell.Application") With oApp.NameSpace(ZipFile & "") If OverwriteFile Then For Each fil In .Items If FSO.FileExists(DefPath & fil.Name) Then Kill DefPath & fil.Name End If Next End If oApp.NameSpace(CVar(DefPath)).CopyHere .Items End With On Error Resume Next Kill Environ("Temp") & "Temporary Directory*" 'Kill zip file Kill ZipFile End If ExitProc: On Error Resume Next Set oApp = Nothing Exit Sub ErrHandler: Select Case Err.Number Case Else MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Unexpected error" End Select Resume ExitProc Resume End Sub
- office課程播放地址及課程明細(xì)
- Excel Word PPT Access VBA等Office技巧學(xué)習(xí)平颱
- 將( .accdb) 文件格式數(shù)據(jù)庫(kù)轉(zhuǎn)換爲(wèi)早期版本(.mdb)的文件格式
- 將早期的數(shù)據(jù)庫(kù)文件格式(.mdb)轉(zhuǎn)換爲(wèi) (.accdb) 文件格式
- KB5002984:配置 Jet Red Database Engine 數(shù)據(jù)庫(kù)引擎和訪問(wèn)連接引擎以阻止對(duì)遠(yuǎn)程數(shù)據(jù)庫(kù)的訪問(wèn)(remote table)
- Access 365 /Access 2019 數(shù)據(jù)庫(kù)中哪些函數(shù)功能和屬性被沙箱模式阻止(如未啟動(dòng)宏時(shí))
- Access Runtime(運(yùn)行時(shí))最全的下載(2007 2010 2013 2016 2019 Access 365)
- access vba代碼太長(zhǎng),換行,分行的寫(xiě)法
- VB6 VBA Access真正可用併且完美支持中英文的 URLEncode 與 URLDecode 函數(shù)源碼
- 自定義VB中的urlencode函數(shù),將URL中特殊部分進(jìn)行編碼
- Access 函數(shù)簡(jiǎn)化串接sql字符串,減少符號(hào)導(dǎo)緻的書(shū)寫(xiě)錯(cuò)誤
- vba完全關(guān)閉IE瀏覽器及調(diào)用IE瀏覽器的簡(jiǎn)單應(yīng)用
- 利用FollowHyperlink方法打開(kāi)超鏈接提示“無(wú)法下載您要求的信息”的解決方案
- 在access中用代碼打開(kāi)文本框中超鏈接地址
- Activex控件或Dll 在某些電腦無(wú)法正常註冊(cè)的解決辦法(regsvr32註冊(cè)時(shí)卡住)
- office使用部分控件時(shí)提示“您沒(méi)有使用該ActiveX控件許可的問(wèn)題”的解決方法
- RTF文件(富文本格式)的一些解析
- Access樹(shù)控件(treeview) 64位Office下齣現(xiàn)橫曏滾動(dòng)條不會(huì)自動(dòng)定位的解決辦法
- Access中國(guó)樹(shù)控件 在win10電腦 節(jié)點(diǎn)行間距太小的解決辦法
- EXCEL 2019 64位版(Office 2019 64位)早就支持64位Treeview 樹(shù)控件 ListView列錶等64位MSCOMMCTL.OCX控件下載
- VBA或VB6調(diào)用WebService(直接Post方式)併解析返迴的XML
- 早期PB程序連接Sqlserver齣現(xiàn)錯(cuò)誤
- MMC 不能打開(kāi)文件C:/Program Files/Microsoft SQL Server/80/Tools/Binn/SQL Server Enterprise Manager.MSC 可能是由於文件不存在,不是一箇MMC控製颱,或者用後來(lái)的MMC版
- sql server連接不瞭的解決辦法
- localhost與127.0.0.1區(qū)彆
- Roych的淺談數(shù)據(jù)庫(kù)開(kāi)髮繫列(Sql Server)
- sqlserver 自動(dòng)備份對(duì)備份目録沒(méi)有存取權(quán)限的解決辦法
- 安裝Sql server 2005 express 和SQLServer2005 Express版企業(yè)管理器 SQLServer2005_SSMSEE
- 金蝶KIS旂艦版 登録時(shí)“類(lèi)型不匹配”
- access行業(yè)交流QQ群-部分行業(yè)交流群(倉(cāng)庫(kù) 人事 工資 考勤 CRM HRM MRP ERP 等)
- access垃圾分類(lèi)數(shù)據(jù)庫(kù)
- Office提高企業(yè)辦公管理效率
- Access交流網(wǎng)Acccess通用開(kāi)髮平颱樹(shù)導(dǎo)航齣錯(cuò)的解決辦法
- Access交流網(wǎng)Access通用開(kāi)髮平颱的使用幫助教程及FAQ
- Access採(cǎi)購(gòu)倉(cāng)庫(kù)繫統(tǒng)作品源代碼
聯(lián)繫人: | 王先生 |
---|---|
Email: | 18449932@qq.com |
QQ: | 18449932 |
微博: | officecn01 |