使用vbs打开保存并关闭文本文件(访问)

时间:2013-11-12 19:22:33

标签: vbscript sendkeys

问题是如何从vbscript打开保存和关闭文本文件。

我需要打开一个特定的txt文件,保存它然后关闭文本文件。

我可以打开一个文件: Dim Objecttxt 设置Objecttxt = CreateObject(“WScript.Shell”) Objecttxt.Run“notepad.exe d:\ text.txt”

然后我尝试用:

命令txt文件

Objecttxt.SendKeys“^(s)”,True 和 Objecttxt.SendKeys“%({F4})”,True

但焦点未设置为记事本编辑器。

另外我添加了这一行: Objecttxt.AppActivate“Text.txt - Kladblok”,800 但似乎没有任何影响。 命令:不接受WScript.Sleep 800(访问2007)

有人可以告诉我:

  • 这是正确的方式

  • 这种方法如何运作?

由于

1 个答案:

答案 0 :(得分:0)

例如我正在将.png文件更改为.jpg

Option Explicit
' Change the Extension you want to Replace
Const CHANGE_FROM = ".png"
Const CHANGE_TO = ".jpg"

' Var Declaration - Don't Change
Dim srcFolder
Dim objFSO, objFolder, oFolder
Dim colFiles

' Set the Source Folder to Begin With or you can dynamically find this if this is being used on multiple computers
srcFolder = "C:\Temp\Files"

' Object Sets - Don't Change
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(srcFolder)
Set colFiles = objFolder.Files

' Changes Files Extensions in Source Folder
ChangeExtension colFiles
' Change the Extension for Each Sub Folder in the Source Folder
GetFilesFromSubFolders objFolder

描述:Sub将Recursevlly检查每个文件夹中的子文件夹 对于每个子文件夹,它将提取文件并使用ChangeExtension 更改文件扩展名 输入:根文件夹对象 输出:每个子文件夹的递归调用 - 更改文件扩展名

 --- Subs ---
 Sub GetFilesFromSubFolders(objRootFolder)
 Dim oSubFolders, oSubFol
 Dim colSubFolFiles
 ' Get the Sub Folders of the Root Folder
 Set oSubFolders = objRootFolder.SubFolders
 ' Check that Folder has Sub Folders
 If oSubFolders.Count > 0 Then
 ' For Each Sub Folder Call Recursevlly 
 For Each oSubFol In oSubFolders
 GetFilesFromSubFolders oSubFol
 Next 
 End If
 ' Get the Files in the folder
 Set colSubFolFiles = objRootFolder.Files 
 ' Change Files Extensions in Folder
 ChangeExtension colSubFolFiles
End Sub

Sub ChangeExtension(collectionSet)
' Description : Sub will Change the Extension 
' for Each File that has the Requested Extension
' Input  : Collection Set Object of Files
' Output : Changes the File Extension
Dim objFile
 ' For Each File in the Files Collection
 For Each objFile In collectionSet
 ' Check if File has the Requested Extension
 If InStr(objFile.Name,CHANGE_FROM) Then
 ' Checge the Extension
 objFile.Name = Replace(objFile.Name,CHANGE_FROM,CHANGE_TO)
 End If
 Next 
End Sub