2016-06-10 91 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