質問させて下さい。
①Excelでファイルを別BOOKとして保存
②別BOOKとして保存を、同一フォルダ内にZipファイルとして圧縮。
③そのあと、別BOOKを削除
上記①と③はできましたが、2.の圧縮についてが、理解できずにおります。
ご存知の方がいましたら、ご教授いただければ幸いです。
自力で検索してみた、サンプルソースを作ってみたが、
質問させてください。
①なぜか”オブジェクトが必要です”でエラーが発生した
Wscript.sleep 100のあたり。
②圧縮後のファイルにパスワードをかけたいのですが、可能でしょうか。
Private Sub CommandButton1_Click()
Dim files(0)
files(0) = "D:\foo\zipsample"
' Call MakeZip("圧縮後のファイル名を絶対パスで記述",files)
zipfilea = "D:\foo\zipsample\tar123.zip"
Call MakeZip(zipfilea, files)
End Sub
Sub MakeZip(ByVal ZipPath, ByRef FileArray)
Dim sfo, app, file, num, zipFolder
Set sfo = CreateObject("Scripting.FileSystemObject")
Set app = CreateObject("Shell.Application")
If sfo.FileExists(ZipPath) = True Then
sfo.DeleteFile ZipPath
End If
With sfo.CreateTextFile(ZipPath, True)
.Write "PK" & Chr(5) & Chr(6) & String(18, 0)
.Close
End With
num = 0
Set zipFolder = app.Namespace(sfo.GetAbsolutePathName(ZipPath))
For Each file In FileArray
If CStr(file) <> "" Then
file = sfo.GetAbsolutePathName(file)
zipFolder.CopyHere (file)
num = num + 1
End If
Next
Do Until zipFolder.Items().Count = num
Wscript.sleep 100
Loop
Set sfo = Nothing
Set app = Nothing
End Sub
コメント(1件)
①なぜか”オブジェクトが必要です”でエラーが発生した
Wscript.sleep 100のあたり。
<<
Wscript.sleep
は
http://www.atmarkit.co.jp/ait/articles/0707/26/news128_2.html
vbsなどスクリプト言語で使用している例だと思うので、
vba で sleep 使う
で検索すると、
http://officetanaka.net/excel/vba/tips/tips116.htm
https://www.moug.net/tech/acvba/0130003.html
などサンプルがいろいろと見つかります。
一番上などに
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
定義して
sleep 100
とWscript.をとって使ってみては?