尝试在Access VB中创建用于Shell命令的字符串。该字符串在完整写入时有效,但如果我尝试在字符串中使用变量,则命令不起作用,即使变量的使用完全产生完整的字符串(在即时窗口中),也可以。
我怀疑在解释字符串版本的代码时会发生一些事情,但无法解决问题。
这是代码,我使用CHR(34)来生成我想在字符串中显示的引号。建议将非常感激 - 我没有多少头发了!
Private Sub temp()
Dim strFilePath As String
Dim strFileName As String
Dim strZipFilename As String
Dim strPDFfilename As String
Dim strShellString As String
昏暗的外壳作为对象
昏暗的结果As Long
Set shell = CreateObject("WScript.shell")
strFilePath = "E:\Documents\Excel Spreadsheets\Roz Theremas\Access\PDFs"
strFileName = "17-03-31temp"
strZipFilename = strFilePath & "\" & strFileName & ".zip"
strZipFilename = Chr(34) & strZipFilename & Chr(34)
strPDFfilename = strFilePath & "\" & strFileName & ".pdf"
strPDFfilename = Chr(34) & strPDFfilename & Chr(34)
strShellString = Chr(34) & Chr(34) & Chr(34) & "C:\Program Files\7-Zip\7z.exe" & Chr(34) & Chr(34) & " a -tzip " & Chr(34) & strZipFilename & Chr(34) & " " & Chr(34) & strPDFfilename & Chr(34) & Chr(34)
'非工作shell命令
result = shell.Run(strShellString,0,False)
'工作shell命令
result = shell.Run("""C:\Program Files\7-Zip\7z.exe"" a -tzip ""E:\Documents\Excel Spreadsheets\Roz Theremas\Access\PDFs\17-03-31temp.zip"" ""E:\Documents\Excel Spreadsheets\Roz Theremas\Access\PDFs\17-03-31temp.pdf""", 0, False)
'strShellString的立即窗口输出
'“”“C:\ Program Files \ 7-Zip \ 7z.exe”“a -tzip”“E:\ Documents \ Excel Spreadsheets \ Roz Theremas \ Access \ PDFs \ 17-03-31temp.zip” msgstr“”“E:\ Documents \ Excel Spreadsheets \ Roz Theremas \ Access \ PDFs \ 17-03-31temp.pdf”“”
End Sub
答案 0 :(得分:0)
我的头发得救了 - 我找到了答案!所以,对于后人来说,这是。
我修改了strFilePath,在两个word文件夹名称周围加上引号,并从strZipFilename和strPDFFilename行周围删除了引号。我猜壳牌的报价过高了。
在修改它的同时,我在zip文件中添加了一个密码,并在字符串命令的末尾将false更改为true,以便返回错误代码。
感谢任何花时间尝试解决我所做过的事情的人。
Public Sub temp()
Dim strFilePath As String
Dim strFileName As String
Dim strZipFilename As String
Dim strPDFfilename As String
Dim strPassword As String
昏暗的外壳作为对象
昏暗的结果As Long
Set shell = CreateObject("WScript.shell")
strPassword = "frog"
strFilePath = "E:\Documents\""Excel Spreadsheets""\""Roz Theremas""\Access\PDFs"
strFileName = "17-03-31temp"
strZipFilename = strFilePath & "\" & strFileName & ".zip"
strPDFfilename = strFilePath & "\" & strFileName & ".pdf"
result = shell.Run("""C:\Program Files\7-Zip\7z.exe"" a -tzip " & "-p" & strPassword & " " & strZipFilename & " " & strPDFfilename, 0, True)