Create a new Word document without invoking Word - vbscript

I want to write a VBScript that will create a new Word document in the current directory. I've tried this sort of thing:
Set word = CreateObject("Word.Application")
Set document = word.Documents.Add()
document.SaveAs(filename)
It basically works, but I want to do it without invoking the Word application.
When I right-click in a File Explorer window, I get a pop-up menu that includes a "New" option from which I can select "Microsoft Word Document". This creates a new Word document in the current directory without invoking the Word application, and this is the action that I would like to perform in my VBScript.
Does anyone know how I can write that?
As a workaround, my existing VBScript copies an existing blank Word file to the current directory. This works pretty well. The one drawback is that the newly created file has the creation time and/or last-modified time of the original file. How can I "touch" the newly created file (again, without invoking Word) so that it appears to have been created "right now"?

What the New → Microsoft Word Document context menu entry does is basically a combination of your workaround and the answer Robin Mackenzie provided.
New document creation via the Explorer context menu is governed by these registry keys:
HKCR\.doc\Word.Document.8\ShellNew (Word 97/2003 documents)
HKCR\.docx\Word.Document.12\ShellNew (OOXML documents)
…
If the keys contain a string value FileName and the directory %windir%\ShellNew contains a file winword8.doc (for Word 97/2003 documents) and winword12.docx (for OOXML documents) the new document will be created as a copy of that file.
If no matching file exists in %windir%\ShellNew or the registry key contains an empty string value NullFile instead of the value FileName, new files will be created as a zero-length files (basically empty ANSI text file). Word automatically converts these files when opening them.
If the registry key contains neither a value FileName nor a value NullFile no context menu entry is displayed for the given file type.
If your script just needs to create a new empty document without any particular content or formatting I'd go with the approach Robin suggested. Otherwise stick with your current method of copying a template file.

You can try this which creates an empty text file and changes the extension to .docx. It's not a 'proper' empty docx file, but it will open as a blank new Word document.
Dim objFSO, strDoc, objFile
' create object to interact with file system
Set objFSO = CreateObject("Scripting.FileSystemObject")
'name of word doc to create
strDoc = "D:\test.docx"
' create blank file and close
Set objFile = objFSo.CreateTextFile(strDoc)
objFile.Close
' clean up
Set objFile = Nothing
Set objFSO = Nothing

Related

VBScript set IconLocation with commas in the file path name

So I created this program that loops though all the lnk files in its folder and inserts an environmental variable into both the shortcut's target path and icon path. The problem is some of these target icons have a comma in the name, so when I use .IconLocation the file path cuts off after the comma and assumes the rest of the string is the icon index. Hence my question is how do you use .IconLocation and have a comma in the file path without it cutting of the rest of the string after the comma?

Need to strip out invalid characters in CSV file

I am generating a CSV file from a Microsoft SQL database that was provided to me, but somehow there are invalid characters in about two dozen places throughout the text (there are many thousands of lines of data). When I open the CSV in my text editor, they display as red, upside-down question marks (there are two of them in the attached screenshot).
When I copy the character and view the "find/replace" dialog in my text editor, I see this:
\x{0D}
...but I have no idea what that means. I need to modify my script that generates the CSV so it strips these characters out, but I don't know how to identify them. My script is written in Classic ASP.
You can also use RegEx to remove unwanted characters:
Set objRegEx = CreateObject(“VBScript.RegExp”)
objRegEx.Global = True
objRegEx.Pattern = “[^A-Za-z]”
strCSV = objRegEx.Replace(strCSV, “”)
This code is from the following article which explains in details what it does:
How Can I Remove All the Non-Alphabetic Characters in a String?
In your case you will want to add some characters to the Pattern:
^[a-zA-Z0-9!##$&()\\-`.+,/\"]*$
You can simply use the Replace function and specify Chr(191) (or "¿" directly):
Replace(yourCSV, Chr(191), "")
or
Replace(yourCSV, "¿", "")
This will remove the character. If you need to replace it with something else, change the last parameter from "" to a different value ("-" for example).
In general, you can use charmap.exe (Character Map) from Run menu, select Arial, find a symbol and copy it to the clipboard. You can then check its value using Asc("¿"), this will return the ASCII code to use with Chr().

Regular expression in file path

I need to enter regular expression in a file path name inside tfileinputdelimited
I have a file watcher that is looking inside a directory for new files to be added. I want the new files have similar word for example file 1 is called apple1.csv and the other file that will be added at another time is called apple2.csv I want a way in order to tell talend to extract the file that contains the word apple, regardless of whats before or after.
Right now I have:
twaitforfile -> tflowtoiterate -> tfileinputdelimited -> tmap -> tdboutput
I believe tfileinputdelimited should have a regular expression as right now I selected a specific file
For the twaitforfile component, in the File Mask field specify the following: "*apple*.csv". This should grab only the files that have the word apple in them.
To dynamically grab these files base on their file names through tfileinputdelimited, you can use the FILENAME global parameter of the twaitforfile to achieve this:

How to read an Excel file from VBScript?

I am trying to read an Excel file in VBScript, but in the file.Readline I am getting strange characters. Do you have any idea how you could get the value of the cells correctly? Without Excel libraries.
Dim fso,file
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set dict = CreateObject("Scripting.Dictionary")
Set file = fso.OpenTextFile ("C:\myFile.xlsx",1)
row = 0
Do Until file.AtEndOfStream
line = file.Readline
dict.Add row, line
row = row + 1
Loop
file.Close
if you are writing a macro in excel (Also visual basic script) there are more than one way getting a cell value.
There are range function
(example from web:) Worksheets("Sheet1").Range("A5").Value
There is cells function (example from web) Cells(1, 1).
The excel file (xslx) should be actually zip file where data is xml.
I think thats why you can't read it if you are using VB compiler.
You most likely need to have set the encoding for the page to UTF-8. See the links before for a simple description:
Classic ASP text substitution and UTF-8 encoding
https://www.w3schools.com/asp/prop_charset.asp
So it would look something like below, located near the top of the page:
Response.Charset = "UTF-8"
I have solved my problem using the extension .CSV, since this allows me to read the information as a .txt in which each column is separated with commas by default, so my code works normally.

Wildcard searching with Batch

Essentially I need to search for a word which can end and start with different things but always contains the same content in the middle.
i.e. 345345hello356
a0a0aphello553
I need to search through the registry for this key under:
HKCU\Software\[KeyBeingLookedFor]
So essentially, get the batch file to look through every key in HKCU\Sotware and if it finds it, spit out Key found via Echo.

Resources