Solved

Opening .txt document in MS Word programmatically

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now