2016-06-10 87 views
2

我需要一些關於此VB腳本的幫助(編輯:它在QlikView中使用) - 它正在將文件複製到其他位置(檢查文件是否已存在目的地文件夾)。VB腳本引用從宏定義的變量

它的工作原始文件名和位置是硬編碼,但這將是一個變量,它是在不同的宏中定義的。

所以源文件名和位置將由varFileOpen定義。

而不是在代碼

基本上,:

SourceFile = "C:\file_path\file_name.txt" 

是這樣:

SourceFile = varFileOpen 

其中varFileOpen已經從不同的子定義(它是完整的文件路徑).. ..我無法得到它的工作?創建該varFileOpen

子:

'Sub to get open file dialog 
SUB ShowOpen 
OpenSave "varFileOpen", 0, "Text file (*.txt)|*.txt|All files (*.*)|*.*", "h:\", "Select a file to open" 
END SUB 
' Sub to show browse folder dialog 
SUB Folder (objVariable) 
ON ERROR RESUME NEXT 
SET objShell = CREATEOBJECT("Shell.Application") 
SET objFolder = objShell.BrowseForFolder (WINDOW_HANDLE, TITLE, OPTIONS, ROOT) 
SET objFolderItem = objFolder.Self 
strPathAndFile = objFolderItem.Path 
SET objSavePath = ActiveDocument.Variables(objVariable) 
objSavePath.SetContent strPathAndFile, TRUE 
ON ERROR GOTO 0 
END SUB 

' Sub to show open/save dialog 
SUB OpenSave (objVariable, intType, strFilter, strInitialDirectory, strDialogText) 
' Create objects 
SET objShell = CREATEOBJECT("WScript.Shell") 
SET objFSO = CREATEOBJECT("Scripting.FileSystemObject") 
strTempDir = objShell.ExpandEnvironmentStrings("%TEMP%") 
strTempFile = strTempDir & "\" & objFSO.GetTempName 
' Temporary powershell script file to be invoked 
strPSFile = tempFile & ".ps1" 
' Temporary file to store standard output from command 
strPSOutFile = tempFile & ".txt" 
' Create script to run 
strPSScript = strPSScript & "[System.Reflection.Assembly]::LoadWithPartialName(""System.windows.forms"") | Out-Null" & vbCRLF 
' Check type (Open (0) or Save (1)) 
IF intType = 1 THEN 
    strPSScript = strPSScript & "$dlg = New-Object System.Windows.Forms.SaveFileDialog" & vbCRLF 
ELSE 
    strPSScript = strPSScript & "$dlg = New-Object System.Windows.Forms.OpenFileDialog" & vbCRLF 
END IF     
' Set initial directory 
strPSScript = strPSScript & "$dlg.initialDirectory = " & CHR(34) & strInitialDirectory & CHR(34) & vbCRLF 
' Set file filter/s 
strPSScript = strPSScript & "$dlg.filter = " & CHR(34) & strFilter & CHR(34) & vbCRLF 
strPSScript = strPSScript & "$dlg.FilterIndex = 1" & vbCRLF 
' Set dialog text 
strPSScript = strPSScript & "$dlg.Title = " & CHR(34) & strDialogText & CHR(34) & vbCRLF 
' Show help (seems it must be set to true) 
strPSScript = strPSScript & "$dlg.ShowHelp = $True" & vbCRLF 
' Show the dialog 
strPSScript = strPSScript & "$dlg.ShowDialog() | Out-Null" & vbCRLF 
strPSScript = strPSScript & "Set-Content """ &strPSOutFile & """ $dlg.FileName" & vbCRLF 
' Write result 
SET objResultFile = objFSO.CreateTextFile(strPSFile, TRUE) 
objResultFile.WriteLine(strPSScript) 
objResultFile.Close 
SET objResultFile = NOTHING 
' Run command in PowerShell 
strPSCMD = "powershell -ExecutionPolicy unrestricted &'" & strPSFile & "'" 
objShell.Run strPSCMD, 0, TRUE 
' Open result file and read result 
SET objResultFile = objFSO.OpenTextFile(strPSOutFile, 1, 0, -2) 
strPathAndFile = objResultFile.ReadLine 
objResultFile.Close 
SET objResultFile = NOTHING 
' Add to result to variable 
SET objSavePath = ActiveDocument.Variables(objVariable) 
objSavePath.SetContent strPathAndFile, TRUE 
' Delete temp-files 
objFSO.DeleteFile(strPSFile) 
objFSO.DeleteFile(strPSOutFile) 
END SUB 

上面的代碼打開資源管理器&你可以選擇一個文件,該路徑被複制 - varFileOpen。

以下子移動文件:

SUB movefile 
Const DestinationFile = "c:\destfolder\anyfile.txt" 
Const SourceFile = "C:\file_path\file_name.txt" 

Set fso = CreateObject("Scripting.FileSystemObject") 
'Check to see if the file already exists in the destination folder 
If fso.FileExists(DestinationFile) Then 
    'Check to see if the file is read-only 
    If Not fso.GetFile(DestinationFile).Attributes And 1 Then 
     'The file exists and is not read-only. Safe to replace the file. 
     fso.CopyFile SourceFile, "C:\destfolder\", True 
    Else 
     'The file exists and is read-only. 
     'Remove the read-only attribute 
     fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes - 1 
     'Replace the file 
     fso.CopyFile SourceFile, "C:\destfolder\", True 
     'Reapply the read-only attribute 
     fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes + 1 
    End If 
Else 
    'The file does not exist in the destination folder. Safe to copy file to this folder. 
    fso.CopyFile SourceFile, "C:\destfolder\", True 
End If 
Set fso = Nothing 
END SUB 

回答

0

你需要傳遞值到Sub它有範圍,這意味着你需要定義這樣子,使其接受參數

Public Sub MySub(byVal SourceFile) 

ByVal只是意味着你傳遞變量而不是實際的變量本身的價值。

而且你將與

MySub varFileOpen 

編輯調用它從其他子:基於上面的代碼顯示,你需要改變Sub movefileSub movefile(byVal SourceFile)並刪除Const delaration的SourceFile的。一旦完成了,你所要做的就是改變任何呼叫movefile(我在你發佈的代碼中看不到任何東西?)用movefile varToOpen來代替

+0

感謝您的答覆戴夫,但我無法得到它的工作。我已經將代碼添加到了我的問題中,可否指出我應該在哪裏放置新的子文件?非常感謝 – Jammy

+0

查看編輯答案的詳細信息。我無法看到任何實際上調用'movefile'子文件的代碼文章中的任何內容? – Dave

+1

我不明白'SourceFile'現在等同於'varFileOpen'是什麼?這個腳本是在QlikView中進行的,並且這個宏是從一個按鈕中調用的,該按鈕被命名爲SUB名稱......它被稱爲「MoveFile」。謝謝 – Jammy

0

試試我的CustomFileDialog。

用法:

Dim fDialog 
Set fDialog = New CustomFileDialog 
fDialog.FilterString = "Text Files (*.txt)|*.txt" 
fDialog.InitialDirectory = "C:\" 
fDialog.DialogText = "Select a file to open" 
fDialog.Show 
fDialog.MoveFile "C:\stackoverflow\temp\New File Name.TXT" 

CustomFileDialog

Class CustomFileDialog 
 
\t Public SourceFile 
 
\t Public FilterString 
 
\t Public InitialDirectory 
 
\t Public DialogText 
 
\t 
 
\t Public Sub Show 
 
\t \t Set toolkit = CreateObject("Vbsedit.toolkit") 
 
\t \t Files = toolkit.OpenFileDialog(InitialDirectory, FilterString, False, DialogText) 
 
\t \t If UBound(Files) >= 0 Then 
 
\t \t \t SourceFile = Files(0) \t 
 
\t \t Else 
 
\t \t \t SourceFile = "" 
 
\t \t End If 
 
\t End Sub 
 
\t 
 
\t Public Sub MoveFile(DestinationFile) 
 
\t \t Set fso = CreateObject("Scripting.FileSystemObject") 
 
\t \t If fso.FileExists(DestinationFile) Then \t \t fso.DeleteFile DestinationFile, True 
 
\t \t fso.CopyFile SourceFile, DestinationFile, True 
 
\t End Sub 
 
End Class

+0

感謝您的回覆托馬斯。你說運行子移動文件.......但「C:\ file_path \ file_name.txt」(源文件)是未知的 - Sub OpenSave允許我選擇一個我想移動的文件夾。 – Jammy