Gowtham
Gowtham

Reputation: 252

Microsoft Word Command line Utility

I have a MS Word document whose path is set in the Environment variable.

How to open that document from Command line Utility?

"C:\Program Files\Microsoft Office\Office\Winword.exe" E:\hello.docx

I have the above command to open a document. But in the above command I have hardcoded the document's name and path.

All I want to do is to use an Environment variable to supply the document's name and path.

Upvotes: 1

Views: 2162

Answers (2)

Ken White
Ken White

Reputation: 125708

If you mean from a batch file, and MS Word is properly associated with the .doc and .docx file extensions on your system, it's very simple.

Put the following in a batch file (for instance, C:\Temp\StartHello.bat). I've used DocVar to be the path and filename of the document; replace it with whatever your environmental variable is named.

set DocVar="E:\Hello.docx"
%DocVar%

Run it

C:\Temp>StartHello

If the environmental variable is already set, just remove the first line from the batch file that assigns it. This leaves you with a single line:

%DocVar%

If you mean "directly from the command line", you can just skip the batch file part:

C:\Temp>%DocVar%

Upvotes: 1

Anthony Neace
Anthony Neace

Reputation: 26023

If you're open to using Powershell instead of the command prompt, you can complete with just a couple of setup steps.

First, create the environment variable for your word document. If you've already done this elsewhere, skip this step. Note that strings that are enclosed by double quotations will resolve variables, so in this example $env:username will resolve to your current Windows user. You could hard-code that too if you like, but this is helpful to generalize the example.

$env:WordDoc = "C:\Users\$env:username\Documents\myDocument.docx"

Next, you will need to add Office's directory to your path variable. You can search for winword.exe to find the location, but it will probably be one of the two below:

  • C:\Program Files\Microsoft Office\Office14
  • C:\Program Files (x86)\Microsoft Office\Office14

You can simply append that path to the environment variable, like so:

$env:Path += ";C:\Program Files\Microsoft Office\Office14"

Anyway, once that is set you can use winword from powershell to open word documents. Here's a simple example:

winword $env:WordDoc

A quick note about changing environment variables in this manner -- they are on the process level. That means that these changes will go away when you close your powershell session. Instead of typing them out each new session, you could save them to a powershell script and run that in the console. Here's a quick script that works on my machine:

param
(
    [string]$FilePath
    [string]$wordDir = "C:\Program Files (x86)\Microsoft Office\Office14"
)

$env:WordDoc = $FilePath
If(!($env:Path | Select-String -SimpleMatch $wordDir))
{
    $env:Path += ";$wordDir"
}
winword $env:WordDoc

Doing this in the command prompt would involve a similar procedure -- you still need to set your PATH environment variable to recognize Microsoft Office. This answer offers some insight on how to do that.

Upvotes: 2

Related Questions