Cách Sử Dụng VBA Điều Kiển WinRaR Nén Và Giải Nén Files

Liên hệ QC

Kiều Mạnh

I don't program, I beat code into submission!!!
Tham gia
9/6/12
Bài viết
5,421
Được thích
4,033
Giới tính
Nam
Mình có viết một code sau cho Ứng dụng của mình sử dụng VBA điều kiển WinRaR nén và giải nén file ...
Giải nén tốt nhưng sẻ lỗi với Folder là khoãng trắng hay Folder là Tiếng việt có dấu ...

VD: D:\Kieu Manh\

Loay hay tìm cách xử lý hoài chưa được Úp lên nhờ các Bạn trợ Giúp ... Xin Cảm ơn

Mã:
Private Function UnRaR(ByVal FileRaR As String) As Variant
    Dim Fso As Object, rarApp As String, Chk As Boolean
    Dim Folder_Path, RaR As String
    Set Fso = CreateObject("Scripting.FileSystemObject")
    Folder_Path = Fso.GetFile(FileRaR).ParentFolder & "\"
    RaR = Fso.GetExtensionName(FileRaR)
    ''// OK 1
    ''rarApp = CreateObject("Shell.Application").Namespace(&H26&).Self.path & "\WinRaR\WinRaR.exe"
    ''// OK 2 Su dung File cho RaR.exe DOS
    rarApp = CreateObject("Shell.Application").Namespace(&H26&).Self.path & "\WinRaR\UnRaR.exe"
    If Fso.FileExists(rarApp) Then Chk = True
    If Fso.FileExists(FileRaR) Then
        If Chk Then Shell rarApp & Space(1) & "X" & Space(1) & FileRaR & Space(1) & Folder_Path, vbHide
    End If
    Set Fso = Nothing
End Function


Private Sub Main()
    Dim vFile
    vFile = Application.GetOpenFilename("All Files, *.rar")
    If TypeName(vFile) = "String" Then UnRaR vFile
End Sub
 
Lần chỉnh sửa cuối:
Công viẹc này chả liên quan gì đến Excel, Access, ahy bất cứ 1 phần mềm nào của Office. Lý do tại sao không dùng VBS cho nó khoẻ?

(thật ra dùng Powwershell là đúng nhiệm vụ nhất)
 
Upvote 0
Công viẹc này chả liên quan gì đến Excel, Access, ahy bất cứ 1 phần mềm nào của Office. Lý do tại sao không dùng VBS cho nó khoẻ?

(thật ra dùng Powwershell là đúng nhiệm vụ nhất)
Nếu dùng Powwershell với file *.zip thì mình viết xử lý ok đó ....tại đang có vấn đề liên quan đến WinRar làm chưa được nên mới hỏi đó mà.....
 
Upvote 0
Mình có viết một code sau cho Ứng dụng của mình sử dụng VBA điều kiển WinRaR nén và giải nén file ...
Giải nén tốt nhưng sẻ lỗi với Folder là khoãng trắng hay Folder là Tiếng việt có dấu ...

VD: D:\Kieu Manh\

Loay hay tìm cách xử lý hoài chưa được Úp lên nhờ các Bạn trợ Giúp ... Xin Cảm ơn

Mã:
Private Function UnRaR(ByVal FileRaR As String) As Variant
    Dim Fso As Object, rarApp As String, Chk As Boolean
    Dim Folder_Path, RaR As String
    Set Fso = CreateObject("Scripting.FileSystemObject")
    Folder_Path = Fso.GetFile(FileRaR).ParentFolder & "\"
    RaR = Fso.GetExtensionName(FileRaR)
    ''// OK 1
    ''rarApp = CreateObject("Shell.Application").Namespace(&H26&).Self.path & "\WinRaR\WinRaR.exe"
    ''// OK 2 Su dung File cho RaR.exe DOS
    rarApp = CreateObject("Shell.Application").Namespace(&H26&).Self.path & "\WinRaR\UnRaR.exe"
    If Fso.FileExists(rarApp) Then Chk = True
    If Fso.FileExists(FileRaR) Then
        If Chk Then Shell rarApp & Space(1) & "X" & Space(1) & FileRaR & Space(1) & Folder_Path, vbHide
    End If
    Set Fso = Nothing
End Function


Private Sub Main()
    Dim vFile
    vFile = Application.GetOpenFilename("All Files, *.rar")
    If TypeName(vFile) = "String" Then UnRaR vFile
End Sub
Thực tế bạn đang sử dụng câu lệnh Winrar cho msdos. Như vậy đối với đường dẫn có khoảng trắng (dấu cách) cần phải để trong dấu ngoặc kép.
Ví dụ
Bạn có thể sử dụng 1 trong 2 cách sau:
c:\"a b"\"c d"\xyz.rar
hoặc
"c:\a b\c d\xyz.rar"
Hiện trên máy tôi không sử dụng winrar nên chưa kiểm tra được nhưng theo nhưng gì tôi thường làm với 7z.exe thì có thể nguyên nhân là như vậy.
 
Upvote 0
Thực tế bạn đang sử dụng câu lệnh Winrar cho msdos. Như vậy đối với đường dẫn có khoảng trắng (dấu cách) cần phải để trong dấu ngoặc kép.
Ví dụ
Bạn có thể sử dụng 1 trong 2 cách sau:
c:"a b""c d"\xyz.rar
hoặc
"c:\a b\c d\xyz.rar"
Hiện trên máy tôi không sử dụng winrar nên chưa kiểm tra được nhưng theo nhưng gì tôi thường làm với 7z.exe thì có thể nguyên nhân là như vậy.
Winrar hổ trợ cho bên thứ 3 xài một số tính năng mà VD như File Thư viện UnRaR.dll hay mấy File sau Mình úp lên ko cần thiết phải cài WinRaR vẫn xài vô tư

Bạn có thể vào link sau của WinRaR xem Example Code ...Tuy nhiên nó chỉ cho coi một chút thôi còn File UnRaR.dll Mình Add vào chương trình của mình để xem tham số của hàm thì nó khóa kỹ lắm ko xem được

http://www.rarlab.com/download.htm

Nếu lần đầu sử dụng File WinRaR.exe nó hỏi key thì kéo key vào là OK ...
Còn xài mấy file kia ko cần thiết Key

đường dẫn Folder WinRaR cho vào đâu Tùy Bạn
 

File đính kèm

  • WinRaR.rar
    1.1 MB · Đọc: 47
Upvote 0
Tuy nhiên nó chỉ cho coi một chút thôi còn File UnRaR.dll Mình Add vào chương trình của mình để xem tham số của hàm thì nó khóa kỹ lắm ko xem được
Ở trên, mình đang thấy bạn dùng command line với rar.exe hoặc unrar.exe.
Còn nếu dùng dll là dùng các hàm bên trong nó:
Unrar.dll có các hàm sau:

RARCloseArchive
RARGetDllVersion
RAROpenArchive
RAROpenArchiveEx
RARProcessFile
RARProcessFileW
RARReadHeader
RARReadHeaderEx
RARSetCallback
RARSetChangeVolProc
RARSetPassword
RARSetProcessDataProc

Cách sử dụng thì google là ra hết.
 
Upvote 0
Ở trên, mình đang thấy bạn dùng command line với rar.exe hoặc unrar.exe.
Còn nếu dùng dll là dùng các hàm bên trong nó:
Unrar.dll có các hàm sau:

RARCloseArchive
RARGetDllVersion
RAROpenArchive
RAROpenArchiveEx
RARProcessFile
RARProcessFileW
RARReadHeader
RARReadHeaderEx
RARSetCallback
RARSetChangeVolProc
RARSetPassword
RARSetProcessDataProc

Cách sử dụng thì google là ra hết.

Hàm này mình biết nhưng nó là API ...... Mình không xài Win64 nên không test được còn Win32 thì OK

Nhưng mình thấy xài mấy File kia tiện sử dụng hơn VD như: File WinRaR.exe sử dụng nó tạo Ribbon Excel hay Delete styles rất OK ....vv và nhiều thứ khác nữa ...còn xài *.dll không thực hiện được
 
Lần chỉnh sửa cuối:
Upvote 0
Hai hôm nay Mình kiếm tài liệu trên Internet và một số Web code két nước ngoài thì thấy code họ viết dài khủng khiếp ...và cũng tèo với Folder là tiếng việt có dấu mặc dù sử dụng cả một mớ API xử lý Folder UniCode ....

2/ Mạnh Tìm thấy một số tham số Của WinRaR cho DOS trong WinRaR help và ngoài Úp lên đây cho Bạn nào cần nghiên cứu viết phần mềm hay ứng dụng nhỏ có sử dụng thư viện WinRaR hay WinRaR cho DOS ...

2/ Mạnh Tải một file Access của trang PlanetSourceCode do người Việt Nam viết úp trên đó thấy Hay Up cho Bạn nào Cần thì tải về nghiên cứu ....tải file trên MediaFire và Google Drive
Link sau ....

https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=74547&lngWId=1

File Mạnh Úp cuối Bài

Nếu Bạn nào yêu thích nhiều ngôn ngữ lập trình thì vào trang PlanetSourceCode đó mà tải Example Code về mà coi OpenSoure đúng nghĩa của nó 100 % .....

Commands WinRaR For DOS
Mã:
  a             Add files to archive <-------------------> Look here
  c             Add archive comment
  cf            Add files comment
  ch            Change archive parameters
  cw            Write archive comment to file
  d             Delete files from archive
  e             Extract files to current directory
  f             Freshen files in archive
  i[par]=<str>  Find string in archives
  k             Lock archive
  l[t,b]        List archive [technical, bare]
  m[f]          Move to archive [files only]
  p             Print file to stdout
  r             Repair archive
  rc            Reconstruct missing volumes
  rn            Rename archived files
  rr[N]         Add data recovery record
  rv[N]         Create recovery volumes
  s[name|-]     Convert archive to or from SFX
  t             Test archive files
  u             Update files in archive
  v[t,b]        Verbosely list archive [technical,bare]
  x             Extract files with full path


<Switches>
  -             Stop switches scanning
  ac            Clear Archive attribute after compression or extraction
  ad            Append archive name to destination path
  ag[format]    Generate archive name using the current date
  ao            Add files with Archive attribute set
  ap<path>      Set path inside archive
  as            Synchronize archive contents
  av            Put authenticity verification (registered versions only)
  av-           Disable authenticity verification check
  c-            Disable comments show
  cfg-          Disable read configuration
  cl            Convert names to lower case
  cu            Convert names to upper case
  df            Delete files after archiving <--------------------------------------- And here
  dh            Open shared files
  ds            Disable name sort for solid archive
  e[+]<attr>    Set file exclude and include attributes
  ed            Do not add empty directories
  en            Do not put 'end of archive' block
  ep            Exclude paths from names
  ep1           Exclude base directory from names
  ep2           Expand paths to full
  ep3           Expand paths to full including the drive letter
  f             Freshen files
  hp[password]  Encrypt both file data and headers
  id[c,d,p,q]   Disable messages
  ieml[addr]    Send archive by email
  ierr          Send all messages to stderr
  ilog[name]    Log errors to file (registered versions only)
  inul          Disable all messages
  ioff          Turn PC off after completing an operation
  isnd          Enable sound
  k             Lock archive
  kb            Keep broken extracted files
  m<0..5>       Set compression level (0-store...3-default...5-maximal)
  mc<par>       Set advanced compression parameters
  md<size>      Dictionary size in KB (64,128,256,512,1024,2048,4096 or A-G)
  ms[ext;ext]   Specify file types to store
  mt<threads>   Set the number of threads
  n<file>       Include only specified file
  n@            Read file names to include from stdin
  n@<list>      Include files in specified list file
  o+            Overwrite existing files
  o-            Do not overwrite existing files
  oc            Set NTFS Compressed attribute
  or            Rename files automatically
  os            Save NTFS streams
  ow            Save or restore file owner and group
  p[password]   Set password
  p-            Do not query password
  r             Recurse subdirectories
  r0            Recurse subdirectories for wildcard names only
  ri<P>[:<S>]   Set priority (0-default,1-min..15-max) and sleep time in ms
  rr[N]         Add data recovery record
  rv[N]         Create recovery volumes
  s[<N>,v[-],e] Create solid archive
  s-            Disable solid archiving
  sc<chr>[obj]  Specify the character set
  sfx[name]     Create SFX archive
  si[name]      Read data from standard input (stdin)
  sl<size>      Process files with size less than specified
  sm<size>      Process files with size more than specified
  t             Test files after archiving
  ta<date>      Process files modified after <date> in YYYYMMDDHHMMSS format
  tb<date>      Process files modified before <date> in YYYYMMDDHHMMSS format
  tk            Keep original archive time
  tl            Set archive time to latest file
  tn<time>      Process files newer than <time>
  to<time>      Process files older than <time>
  ts<m,c,a>[N]  Save or restore file time (modification, creation, access)
  u             Update files
  v             Create volumes with size autodetection or list all volumes
  v<size>[k,b]  Create volumes with size=<size>*1000 [*1024, *1]
  vd            Erase disk contents before creating volume
  ver[n]        File version control
  vn            Use the old style volume naming scheme
  vp            Pause before each volume
  w<path>       Assign work directory
  x<file>       Exclude specified file
  x@            Read file names to exclude from stdin
  x@<list>      Exclude files in specified list file
  y             Assume Yes on all queries
  z[file]       Read archive comment from file

Kèm Một File Help Tiếng Việt ...Hay tham khảo Thêm Link Sau
http://acritum.com/software/manuals/winrar/

Nghiên Cứu xong vụ này chắc Mạnh trở thành chuyên Viên WinRaR quá ...và cmd khá lên một Chút....--=0+-+-+-+|||||
 

File đính kèm

  • democAuth.rar
    249.8 KB · Đọc: 23
  • WinRAR_VN.rar
    340.4 KB · Đọc: 27
Upvote 0
Web KT
Back
Top Bottom