How do I open a microsoft word document from a console program?

Hi folks!

I want to know if it's possible writing a program, that just opens a Microsoft Word Document, and how. I really hope some of you guys will answer my question, cause I can't find any sites who explain it - only by using C# :(

Best regards
Mathias
You just want to execute it so it opens up in Word?
Umm...double click on it?? I don't really know why you would want to open a word doc with a program...just use a shortcut or barring that, a batch file...
 
system("START word.docx"); 

?...O.o...hehe. I know, I know. system is evil...^_^.
I think you misunderstood me... I want to know how to create a c++ program, that can open word. Fx:

#include <iostream>
using namespace std;

int main()
{
char a;
cout << "Press A to open word, or press E to quit";
cin >> a;
if(a=='a')
---open word---
else if(a=='e')
return 0;
else
return 0;
}

Hope you will answer once again...
Mathias
Word files are a proprietary format, and are not trivial to read and write.

I suggest you find a library to do it for you. The following link is a good place to start.
http://en.wikipedia.org/wiki/DOC_(computing)

Good luck!
Thank you very much! :p

Are you absolutely certain, that it's not possible to write something in a console program, that opens a Word Document?

Mathias
You most certainly can. But like I said, the Word file formats are not simple.
Now that was a quick answer! Nice :D, well do you then know the code for opening of any program? Could be Internet Explorer or Paint :S, anything...
system("mspaint"); ?

On Windows, you would do better to use CreateProcess().

You do know that Word is an OLE-Automatable application?
That's working! But not as perfect as I suspected. The command opens Microsoft Paint, but actually I have placed it in a while-loop:

#include <iostream>
#include <time.h>
#include <Windows.h>
using namespace std;

int main()
{
char hat;
int x=0;
cout << "Press A to start" << endl;
cin >> hat;
if(hat=='a')
{
x=1;
while(x==1){
system("mspaint");
}
return 0;
}

It should open A LOT of Microsoft Paint windows, but it only opens a single one. When I try to close down Paint, it opens again... and when I repeat that it opens again... so the loop is working in some way.

However, I want it to open again and again and again, so it opens about 10 or 200 times per second - I don't know how fast it will go...

Mathias
Lol with that sort of program put in the startup you are sure gonna SLOW systems down. Hehehe
Mathias, it appears that when you run system("mspaint"), it will wait for the paint program to be exited to run again.
CreateProcess is non-blocking. System is a blocking command typically.
If you want to use the system() and not the CreateProcess(), in order to open many windows (and not just one and wait to close it) then you can use system("start mspaint"); system("mspaint"); runs mspaint but your program is halted until the mspaint is exited, when you close it it continues.
system("start mspaint"); runs mspaint but your program keeps running, so it executes the same command again and again, so you have many mspaint windows.

I suggest not to use
1
2
while(true)
   system("start mspaint");

except if you want to crash a system... and putting it in the startup applications will probably slow down the computer alot.
While trying it use a for-loop with 5-6 repeats...
Last edited on
Thank you very much Mitsakos, you've solved the problem. it works as i should. Now my program is completed, and I've tested it - it's nice!

Mathias
Topic archived. No new replies allowed.