Solved

Opening .txt document in MS Word programmatically

Posted on 2011-03-25
5
407 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 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

717 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