VB脚本引用从宏定义的变量

时间:2016-06-10 12:24:05

标签: vbscript

我需要一些关于这个VB脚本的帮助(编辑:它正在QlikView中使用) - 它正在将文件复制到另一个位置(检查文件是否已经存在于目标文件夹中)。

当源文件名和位置被硬编码时,它会起作用,但这将是一个在不同的宏中定义的变量。

因此源文件名和位置将由varFileOpen定义。

基本上在代码中,而不是:

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

是这样的:

SourceFile = varFileOpen

其中varFileOpen是从不同的SUB定义的(它是完整的文件路径)....我无法让它工作?

创建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移动文件:

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

2 个答案:

答案 0 :(得分:0)

您需要将值传递给Sub以使其具有范围,这意味着您需要像这样定义子,以便它接受参数

Public Sub MySub(byVal SourceFile)

ByVal只意味着您传递变量的值而不是实际变量本身。

你可以用

从另一个子组中调用它
MySub varFileOpen

编辑:根据上面显示的代码,您需要将Sub movefile更改为Sub movefile(byVal SourceFile)并删除SourceFile的Const版本。完成后,您所要做的就是更改正在调用movefile的任何内容(我在您发布的代码中看不到任何内容吗?)以movefile varToOpen来代替< / p>

答案 1 :(得分: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
	Public SourceFile
	Public FilterString
	Public InitialDirectory
	Public DialogText
	
	Public Sub Show
		Set toolkit = CreateObject("Vbsedit.toolkit")
		Files = toolkit.OpenFileDialog(InitialDirectory, FilterString, False,  DialogText)
		If UBound(Files) >= 0 Then
			SourceFile = Files(0)	
		Else 
			SourceFile = ""
		End If
	End Sub
	
	Public Sub MoveFile(DestinationFile)
		Set fso = CreateObject("Scripting.FileSystemObject")
		If fso.FileExists(DestinationFile) Then 		fso.DeleteFile DestinationFile, True
		fso.CopyFile SourceFile, DestinationFile, True
	End Sub
End Class