open and save a xltm file - vbscript

I've used some code in VBScript to open a .xlsm file and save that file. Now I want to do the same thing as a .xltm file. I've tried to open the xltm file with script, it works fine. While saving that file, it refers the default location and default extension. I need to save the newly opened file with the extension ".xlsm" in the specified location. I don't know how to proceed. Please help me to solve this.
Set objExcel = CreateObject("Excel.Application")
Set WBTestFile = objExcel.Workbooks.Open(WScript.Arguments(0))'SourceFile
WBTestFile.save
WBTestFile.close
objExcel.Workbooks.Open(WScript.Arguments(0))
Here, I am passing the filename (with path) as an argument. I need to open the newly saved ".xlsm" file in the last statement. Argument: "c:\test\book1.xltm", my newly created file want to be saved in the location "C:\test\" with the extension "xlsm".

The Save method saves the file as-is. To save it in a different format you need to use the SaveAs method with the correct file format argument (in this case a macro-enabled Open XML template):
filename = WScript.Arguments(0)
templatename = Replace(filename, ".xlsm", ".xltm")
Set xl = CreateObject("Excel.Application")
Set wb = xl.Workbooks.Open(filename)
wb.SaveAs templatename, 53 'save as template
wb.Close
xl.Quit
To create a new workbook from an existing template you need to use the Add method with the path to the template as argument and then save the file as a macro-enabled Open XML workbook:
template = WScript.Arguments(0)
filename = Replace(template, ".xltm", ".xlsm")
Set xl = CreateObject("Excel.Application")
Set wb = xl.Workbooks.Add(template)
wb.SaveAs filename, 52 'save as workbook
wb.Close
xl.Quit

Related

Compare two files in two different folders and replace it by the newer

With this VBScript code I was able to copy files. If the file exists it does nothing, if not it will copy the files needed.
Dim Photo
SourceFolder = "C:\Photo1"
DistinationFolder = "C:\Photo2"
Set ObjPhoto = CreateObject("Scripting.FileSystemObject")
For Each Photo In ObjPhoto.GetFolder( SourceFolder).Files
If Not ObjPhoto.FileExists(ObjPhoto.BuildPath(DistinationFolder, Replace(Photo.Name, ".jpg", ".bmp"))) Then
photo.Copy ObjPhoto.BuildPath(DistinationFolder, Photo.Name), True
End If
Next
I want to compare the files if the source files also exists in the destination folder and replace it by the newer.
If you want to overwrite based on the last modified date, then the File object has the property you want: DateLastModified. (You can check all properties of the File object here.)
You already have access to the source file objects (your code's Photo variable) so you just need to get the target's file object.
Something like this should work:
Dim Photo
Dim targetFile, bmpTargetFilename, jpgTargetFilename
SourceFolder = "C:\Photo1"
DistinationFolder = "C:\Photo2"
Set ObjPhoto = CreateObject("Scripting.FileSystemObject")
For Each Photo In ObjPhoto.GetFolder(SourceFolder).Files
bmpTargetFilename = ObjPhoto.BuildPath(DistinationFolder, Replace(Photo.Name, ".jpg", ".bmp"))
jpgTargetFilename = ObjPhoto.BuildPath(DistinationFolder, Photo.Name)
If ObjPhoto.FileExists(bmpTargetFilename) Then
' Get the target file object
Set targetFile = ObjPhoto.GetFile(jpgTargetFilename)
' Now compare the last modified dates of both files
If Photo.DateLastModified > targetFile.DateLastModified Then
Photo.Copy jpgTargetFilename, True
End If
Else
Photo.Copy jpgTargetFilename, True
End If
Next
A couple of notes:
It seems you are checking for the existence of a .BMP file yet copying a .JPG file, so I made it explicit by using two variables.
I am also assuming you want to compare the JPG files, since those are the ones being copied.

Using VB Script to read contents of .msg file

I have a folder with thousands of Outlook .msg files.
I'd like to know if it's possible to write a VB Script that can read the sender and receiver from each file, and move the .msg file to a folder based on this info?
Thanks
You shouldn't ask yes/no questions unless you expect the answer to be either "yes" or "no".
Set ol = CreateObject("Outlook.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder("C:\some\folder").Files
If LCase(fso.GetExtensionName(f)) = "msg" Then
Set msg = ol.CreateItemFromTemplate(f.Path)
WScript.Echo msg.Sender.Name
For Each rcpt In msg.Recipients
WScript.Echo rcpt.Name
Next
End If
Next
For reading the contents of a .msg file, I have used the following approach.
use CreateItemFromTemplate for the msg file using the outlook object
Use this outlook object to save the data into a .txt file
Once the .txt file is created read it and use the data as required
Script:
Dim OL : Set OL=CreateObject("Outlook.Application")
Dim Msg ':Set Msg= CreateObject("Outlook.MailItem")
Set Msg = OL.CreateItemFromTemplate("C:\test.msg")
'MsgBox Msg.Subject
Msg.saveAs "C:\test.txt", olDoc
'The above statement will save the contents of .msg file into the designate .txt file
Set OL = Nothing
Set Msg = Nothing
Once the .txt file is created use it as required for your computations.

Copy info from txt and paste in another vbscript

I'm trying to copy a line of text from a .txt -> paste into another file and save. The code I have keeps giving me errors at the paste section. I am completely new at this and learning as I go. My main goal is to paste the info after Host= in another file. But I need to get this down first.
Here is my code so far
///OPEN FILE and READ
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\vnc\vnc.txt",1)
strFileText = objFileToRead.ReadAll()
objFileToRead.Close
' ///PASTE
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileToWrite = objFSO.OpenTextFile("c:\vnc\testfile.vnc", 2)
objFileToWrite.Write strFileText
objFileToWrite.Close
This works for me:
'//OPEN FILE and READ
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\vnc.txt",1)
strFileText = objFileToRead.ReadAll()
objFileToRead.Close
' ///PASTE
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileToWrite = objFSO.OpenTextFile("c:\vnc.vnc", 2)
objFileToWrite.Write strFileText
objFileToWrite.Close
The only difference I made was remove the subfolder, and put in the root of C: The script worked.
I manually created both the source file and target file. If both files exist, and are not locked (as if you had it open / locked in another application), then the permissions of that VNC folder must be the issue.
If still your file permission denied you to write then you have to do change the security of that file using right click of mouse, and update advanced setting.

Copy folder contents to a created .zip file: 'file not found or no read permissions'

I'm trying to create a .zip file from an existing folder using JScript and it seems that my copyHere function is not copying to the .zip folder. Instead I get a popup box titled 'Compressed (zipped) Folder Error' with the message 'file not found or no read permissions' even though I have read/write privileges on the file according to the value of my file.attributes property (32).
Here is the script I'm using:
//Get commman line arguments
var objArgs = WScript.Arguments;
var zipPath = objArgs(0);
var sourcePath = objArgs(1);
//Create empty ZIP file and open for adding
var fso = new ActiveXObject("Scripting.FileSystemObject");
var file = fso.CreateTextFile(zipPath, true);
// Create twenty-two byte "fingerprint" for .zip
file.write("PK");
file.write(String.fromCharCode(5));
file.write(String.fromCharCode(6));
file.write('\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0');
var objShell = new ActiveXObject("shell.application");
var zipFolder = new Object;
zipFolder = objShell.NameSpace(zipPath);
sourceItems = objShell.NameSpace(sourcePath).items();
if (zipFolder != null)
{
zipFolder.CopyHere(sourceItems);
WScript.Sleep(1000);
}
Now the CopyHere function works for copying the contents of the sourcePath to a normal folder but when I try to create a .zip file and copy the contents to that, nothing happens. Any ideas on why copyHere is not copying the contents of the sourcePath to the .zip?
An Example for calling this script would be:
cscript win-zip.js C:\desired\zip\file.zip C:\path\to\source\folder
And the desired outcome would be that file.zip was created and now contains the contents of the source folder. Could this be a problem with permissions? What might cause this behavior?
Side Note, using a vbScript and the same commands I can successfully create and populate a .zip, so why doesn't it work using jscript!
Set objArgs = WScript.Arguments
ZipFile = objArgs(0)
SourceFolder = objArgs(1)
' Create empty ZIP file and open for adding
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set zip = CreateObject("Shell.Application").NameSpace(ZipFile)
' Get items in source folder
Set sourceItems = CreateObject("Shell.Application").NameSpace(SourceFolder).Items
' Add all files/directories to the .zip file
zip.CopyHere(sourceItems)
WScript.Sleep 1000 'Wait for items to be copied
Any helpful comments are greatly appreciated, thanks!
I encountered the same problem (file not found or no read permissions' even though I have read/write privileges on the file according to the value of my file.attributes property).
The problem disapeared as soon as I found and suppress a 0 length file somewhere in the directory to be copied with the copyhere method.
As Raymond said, the problem was that I had an open reference to the .zip folder in the file variable that I created (which had a lock on the folder so I could not copy any contents to it). The solution is to call
file.Close();
after writing to the file, that way we can access the file to copy contents to it :)

creation of a text file in a specified location and remove the old one

I need to create a text file "setup.txt" in the location C:\Documents and Settings\All Users\Application Data\xerox\setapp in VB script
the location C:\Documents and Settings\All Users\Application Data is common apllication data folder here we can
use word "CSIDL_COMMON_APPDATA" or &H23& for that.Here the other which i need to take care is if any setup.txt file are
already there in that location i need to remove that old one i need to insert the new "setup.txt" which contatin blank
values means new one
I am new to this vbscript ,and i wanted an optimized code to acheive that functionality
You could open the file for writing and write a blank line, this will create the new text file and overwrite any previous versions.
strFilename = " C:\Documents and Settings\All Users\Application Data\xerox\setapp.txt"
Set objFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(strFilename,2,true)
'Write stuff to the file here
objFile.Close
Set objFile = nothing
It's not clear if your script is creating the log file, if not and you want to copy the file and overwrite any previous ones you could do this (setting the last argument in the CopyFile method to true will overwrite older versions).
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile Source, Destination, true
Set fso = Nothing
If you just want to delete the previous file if it exists you could do this (here setting the last argument in the DeleteFile method to true will force deletion of the file).
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(file) Then
fso.DeleteFile file, true
End If
Set fso = Nothing
Wow, VBScript, it was a long time ago ... But I'll give it a shot.
What you need, if I understand you correctly, is to use the FileSystemObject it contains methods for deleting, creating and copying text files.
I hope this at least gives you some pointers to get you started.

Resources