Open doc file in Winword via a batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
batchelor
Posts: 11
Joined: 12 Sep 2010 08:54

Open doc file in Winword via a batch file

#1 Post by batchelor » 12 Sep 2010 09:14

Hello!
I am not trained in batch programming...
In my Windows XP Pro SP3 I sometimes noticed that normal.dot was changed in a way I didn't want.
So I wrote this batch to always load a backed-up version of normal.dot when starting Winword 2003:

@echo off
del D:\Docume~1\janeri~1\Applic~1\Micros~1\Templa~1\~$Normal.dot /A:H
copy D:\normal.dot E:\Office\Office~1\Templa~1\1033\ /Y
copy D:\normal.dot D:\Docume~1\janeri~1\Applic~1\Micros~1\Templa~1 /Y
start E:\Office\Office~1\OFFICE11\WINWORD.EXE
cls
exit

Office 2003 is in E:\Office..., my WinXP in D: (for certain reasons) and the back-up as well as the batch are in D:

This works fine.

But -as expected - when I mark a doc file and choose "Open with" and there pick my batch file, it just opens Winword but not with that doc file displayed.

Somehow I would have to transfer the path with the file to append after WINWORD.EXE above.

I would very much appreciate your help!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Open doc file in Winword via a batch file

#2 Post by aGerman » 12 Sep 2010 15:01

Well, your batch file opens Word but without any argument. How should Word know which document you want to open?

OK. The solution should be simple. If you choose your batch file by "Open with" context, then your document is given as argument to the batch file. You will find it as %1. So change the command line:

Code: Select all

start E:\Office\Office~1\OFFICE11\WINWORD.EXE %1


Regards
aGerman

batchelor
Posts: 11
Joined: 12 Sep 2010 08:54

Re: Open doc file in Winword via a batch file

#3 Post by batchelor » 12 Sep 2010 15:29

That works just fine!
Thanks a lot!
batchelor

batchelor
Posts: 11
Joined: 12 Sep 2010 08:54

Re: Open doc file in Winword via a batch file

#4 Post by batchelor » 25 Sep 2010 02:27

This file, a bit modified, works fine in my XP Pro.
But a friend of mine tried it in Windows 7 and it didn't work well, even written without the "traditonal" 8+3 format:

@echo off
if exist C:\"Documents and Settings"\"[your name]"\"Application Data"\"Microsoft"\"Templates"\~$Normal.dot del C:\"Documents and Settings"\"[your name]"\"Application Data"\"Microsoft"\"Templates"\~$Normal.dot /A:H
if exist C:\"Program Files"\"Microsoft Office"\Office11\~$Normal.dot del C:\"Program Files"\"Microsoft Office"\Office11\~$Normal.dot /A:H
copy C:\normal.dot C:\"Documents and Settings"\"[your name]"\"Application Data"\"Microsoft"\"Templates" /Y
copy C:\normal.dot C:\"Program Files"\"Microsoft Office"\Office11\~$Normal.dot /Y
start C:\"Program Files"\"Microsoft Office"\Office11\WINWORD.EXE %1
cls
exit

(The "if exist" is, of course, not necessary, but added as a extra security against erasing a wrong file in case something doesn't work correctly. A normal.dot as I want it is backed up in C:\.)

Is there some difference between how XP and 7 run batch files?

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Open doc file in Winword via a batch file

#5 Post by amel27 » 25 Sep 2010 03:17

Code: Select all

@echo off

set ndot1=%APPDATA%\Microsoft\Templates\~$Normal.dot
set ndot2=%ProgramFiles%\Microsoft Office\Office11\~$Normal.dot

if exist "%ndot1%" del "%ndot1%" /A:H && copy C:\normal.dot "%ndot1%" /Y
if exist "%ndot2%" del "%ndot2%" /A:H && copy C:\normal.dot "%ndot2%" /Y

copy C:\normal.dot "%APPDATA%\Microsoft\Templates" /Y

start "%~1" "%ProgramFiles%\Microsoft Office\Office11\WINWORD.EXE" "%~1"


P.S. not tested
Last edited by amel27 on 25 Sep 2010 04:43, edited 2 times in total.

batchelor
Posts: 11
Joined: 12 Sep 2010 08:54

Re: Open doc file in Winword via a batch file

#6 Post by batchelor » 25 Sep 2010 03:31

Thanks!
But it seems that I need to explain why I delete ~$Normal.dot. This is a backup of normal.dot that Word makes when running and that normally disappears when Word is terminated.
It in rare cases happens that it is not deleted (for example, after a crash), and if not, it may lead to a modification of normal.dot next time Word is run.
So before I copy my backup of normal.dot to its proper location, I check if there is an undeleted ~$Normal.dot there and, in that case, delete it.

The next step is then to copy my backed-up normal.dot (being as I want to have it) to where it belongs.
This all works fine in my XP Pro but apparently not as well in Win7 ...

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Open doc file in Winword via a batch file

#7 Post by amel27 » 25 Sep 2010 04:01

batchelor wrote:This all works fine in my XP Pro but apparently not as well in Win7 ...
for instance %APPATH% is different in XP (c:\Documents and Settings\) and Win7 (c:\Users\)

P.S. previous post edited

batchelor
Posts: 11
Joined: 12 Sep 2010 08:54

Re: Open doc file in Winword via a batch file

#8 Post by batchelor » 25 Sep 2010 04:16

Thanks, I will suggest to my friend to try this.

Post Reply