Solved

Opening .txt document in MS Word programmatically

Posted on 2011-03-25
5
387 Views
Last Modified: 2012-05-11
Dear experts,

I need to open a text file in MS Word programmatically.
When I use
ShellExecute(NULL, "open", "Winword.exe", pSheet->m_strDestinationFilePath, NULL, SW_SHOWNORMAL);
I get error messages, first "The file could not be found. (C:\Documents.doc) (I have no idea where this path comes from), then "The file could not be found. (C:\Documents and Settings\...\and.doc - don't know this one either), then "The directory name is not valid. (C:\Documents and Settings\...\myTextFile.txt).
If I use "notepad.exe" instead of "Winword.exe", the file opens correctly in Notepad.
What am I doing wrong?
Thanks.
0
Comment
Question by:tantormedia
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:GeoffHarper
ID: 35217541
Try:

     ShellExecute(NULL, "open", "Winword.exe", """" & pSheet->m_strDestinationFilePath & """", NULL, SW_SHOWNORMAL);
0
 
LVL 4

Accepted Solution

by:
GeoffHarper earned 500 total points
ID: 35217546
Sorry, that was VB!

ShellExecute(NULL, "open", "Winword.exe", "\"" + pSheet->m_strDestinationFilePath + "\"", NULL, SW_SHOWNORMAL);
0
 

Author Comment

by:tantormedia
ID: 35217615
Thank you very much! It worked, though I still have some problems. Before opening the file, I get a "File in Use" message: "myfile.txt is locked for editing by 'another user'. Do you want to:
- Open a Read Only copy
- Create a local copy and merge you changes later
- Receive notification when the original copy is available.
I didn't have this with notepad. Is it possible to avoid this error? I don't want a read only copy, I want to be able to edit the file.
After I pick e.g. the first option, I have a file conversion dialog:
Select the encoding that makes your document readable: Windows(default), MS DOS, Other encoding.
Is it possible to automatically make it Windows without seeing this dialog?
Thanks.
0
 
LVL 4

Expert Comment

by:GeoffHarper
ID: 35217740
There are various command line switches for MS Word but none will convert a .txt file for you automatically (I don't think).

Make sure you don't actually have the file open elsewhere when you run your program.  Check in Task Manager for another instance of your program which may not have terminated, leaving the file still open.
0
 

Author Closing Comment

by:tantormedia
ID: 35217751
Thank you very much.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question