[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Opening .txt document in MS Word programmatically

Posted on 2011-03-25
5
Medium Priority
?
417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

656 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