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

Aug 21, 2008 at 5:19pm
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
Aug 21, 2008 at 7:35pm
You just want to execute it so it opens up in Word?
Aug 22, 2008 at 5:15am
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...
Aug 22, 2008 at 5:59am
 
system("START word.docx"); 

?...O.o...hehe. I know, I know. system is evil...^_^.
Aug 22, 2008 at 4:56pm
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
Aug 22, 2008 at 5:04pm
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!
Aug 22, 2008 at 6:30pm
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
Aug 22, 2008 at 6:32pm
You most certainly can. But like I said, the Word file formats are not simple.
Aug 22, 2008 at 6:34pm
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...
Aug 23, 2008 at 11:44am
system("mspaint"); ?

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

You do know that Word is an OLE-Automatable application?
Aug 25, 2008 at 9:28am
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
Aug 25, 2008 at 1:19pm
Lol with that sort of program put in the startup you are sure gonna SLOW systems down. Hehehe
Aug 25, 2008 at 9:06pm
Mathias, it appears that when you run system("mspaint"), it will wait for the paint program to be exited to run again.
Aug 25, 2008 at 9:14pm
CreateProcess is non-blocking. System is a blocking command typically.
Aug 26, 2008 at 12:17pm
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 Aug 26, 2008 at 12:18pm
Aug 27, 2008 at 4:53pm
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.