user2984694
user2984694

Reputation: 11

open save and close text file with vbs (access)

The questions is how to open save and close a textfile from vbscript.

I need to open a specific txt file, save it and then close the text file.

I can open a file with:

Dim Objecttxt
Set Objecttxt = CreateObject("WScript.Shell")
Objecttxt.Run "notepad.exe d:\text.txt"

and then i try to command the txt file with:

Objecttxt.SendKeys "^(s)", True

and

Objecttxt.SendKeys "%({F4})", True

but the focus is not set to the notepad editor.

Also i add the line: Objecttxt.AppActivate "Text.txt - Kladblok", 800 but it seems like ths has no infuence. the command: WScript.Sleep 800 is not accepted (access 2007)

Can someone tell me:

Thanks

Upvotes: 1

Views: 7144

Answers (1)

Ragnar
Ragnar

Reputation: 1582

For example I'm changing a .png file to .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

Description : Sub will Recursevlly Check each folder for sub folders For Each Sub folder it will Extract the Files and Use the ChangeExtension to change the files Extension Input : Root Folder Object Output : Recursive Call For Each Sub Folder - Change Files Extension

 --- 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

Upvotes: 0

Related Questions