Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem programmatically opening docx file 1

Status
Not open for further replies.

moongirl129

Programmer
Sep 5, 2002
38
GB
I developed a C# app last year which has been used on 4 or 5 pcs regularly during that time with no problems. Part of the app copies a Word document (docx 2007) then dynamically appends a few pages then saves it in a set place on the PC.

The app has been installed on XP, Vista, with PCs that have Word 2003 installed. All fine.

Today I have installed the app on a new PC which has Vista and Word 2007 installed. The Word document is created fine and I can open it manually but when I click a button in the app to auto open the document Word 2007 opens and I see a msg saying the 'The file could not be found'. There are 2 buttons, the other references a dynamically created PDF and the docs are stored in exactly the same folder. I log the filepath when trying to open either and the folder paths are identical which is correct and the file names are both correct.

Why will the PDF open but the docx not open? Why am i having this trouble!!?? And how on earth do I fix it - I am stumped??!!

I don't know if it's relevant or not but the app does not have an installer - I place all relevant app files in the relevant folder in program files.

The path for these docs is in virtual store but I reference c:\program files\....... etc - works for pdf but not for docx??
 
MS Word 07 is the only variable that changed. Maybe the system cannot open Word 07 from the button click event.

this would explain why the file is generated correctly. can be opened manually. And why PDFs work without issue.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Yes that may be but I don't believe it is - has anyone else experienced this??
 
Yes that may be but I don't believe it is
have you confirmed this? if not don't rule it out. major changes were made with the office 07 suite. this could be one of them.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I am opening the document like this:
Code:
string fullPath = rootFolderPath + Properties.Settings.Default.reviewFolder + "\\" + thisReview.Reference + "\\";
                string WordfilePath = fullPath + thisReview.Reference + "_Manual.docx";
                
                               System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.EnableRaisingEvents = false;
                proc.StartInfo.FileName = WordfilePath;
                proc.Start();

where rootFolderPath is:
Code:
rootFolderPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).ToLower();
                rootFolderPath = rootFolderPath.Replace("file:\\", "");
                rootFolderPath = rootFolderPath + "\\";

The PDF file is referenced in exactly the same way - I had overlooked the fact that this is also the first PC I've installed this app on which runs Vista and this turns out to be the problem.

The rootfolderpath is c:\Program Files\appName but Vista stores all app created documents in:

C:\Users\User Name\AppData\Local\VirtualStore\Program Files\App Name\ etc

So my question is how do I programmatically reference the users Virtual store? I have hard coded the 'true' path in to my button click and it works fine so I know I'm on the right track here.

Any suggestions?
 
there may be an envorinment variable which references the user's virtual store. Or there may be a variable like %Temp% which could be used as well.

I'm assuming this is a desktop app. if so grab the Identity from the thread and insert into the directory path
Code:
IIdentity user = System.Security.Principal.WindowsIdentity.GetCurrent();
string.format(@"C:\Users\{0}\AppData\Local\VirtualStore\Program Files\App Name\", user.Name);
you may need to parse the name to split domain and id.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
But I try and avoid hard coding any file paths if I can - is there really no other way to reference this path? Doing it the way you have suggested will mean having to have an XP version of the software and a Vista version!

Any other suggestions?
 
you have a couple choice.
1. create a factory which creates the file path. the factory will determine if the machine is XP or Vista
Code:
public interface IWordDocumentFilePathFactory
{
   string Create();
}

public class XpWordDocumentFilePathFactory : IWordDocumentFilePathFactory
{
   public string Create()
   {
      return @"c:\path in xp\";
   }
}

public class VistaWordDocumentFilePathFactory : IWordDocumentFilePathFactory
{
   public string Create()
   {
     IIdentity user = System.Security.Principal.WindowsIdentity.GetCurrent();
     return string.format(@"C:\Users\{0}\AppData\Local\VirtualStore\Program Files\App Name\", user.Name);
}

public class OSWordDocumentFilePathFactory
{
   public static IWordDocumentFilePathFactory CreateFactory()
   {
      if (Envorinment.OS == "Vista") return new VistaWordDocumentFilePathFactory();
      return new XpWordDocumentFilePathFactory();
   }
}
I'm sure there is an Environment property/function which returns what operating system the code is running on. the above is only for demonstration.

you could then use the code above like this
Code:
string filePath = OSWordDocumentFilePathFactory.CreateFactory().Create();
File.Open(filePath);

2. use the app.config appsettings to configure the file path.
Code:
<appsettings>
   <add key="Word.Document.FilePath" value="c:\path to file" />
</appsettings>

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Jason, thanks for your suggestion, however......

Sorry I'm feeling a bit stunned. Surely one of you C# developers out there has developed an app that either saves or references a document on a Vista PC. Has no one else come across this? And I can't believe that there is no way of doing this without having to develop a class to build installation paths depending on the operating system.

Or am i missing something??!
 
moongirl:

Are you opening a .doc file or .docx file? .docx is based on the new OOXML (Highly Flawed) file format. Essentially the file is a zip file that contains xml and formatting information.

When you save the file by default in 2007 you will likely have a docx file where as 2003 will only produce a .doc file.

If this is not the case then Jason is correct in his second option - put the path in a config file. This is not hard coded as it resides in an xml file outside your application. You are already doing this in your original example: "Properties.Settings.Default.reviewFolder"

Look for the file that is yourprogramname.exe.config and the details are in there.

If I were you - I'd take advice from the guy at the top of the MVP list as he's probably done this before.

P.S. Meckley - I want my spot back :)

 
I honestly don't know how this happens. I think you and other are much more active then myself (same for asp.net)

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Ok it took a while to find out the correct answer to this- I'm posting the answer to hopefully help anyone else coming up against the same problem and not wanting to *hack* a rough solution. Here goes:

Firstly you should never reference a path in your code like:
Code:
@"C:\Users\UserName\AppData\Local\VirtualStore\Program Files\App Name\"

Neither should we be putting things in the VuirtualStore like I was!

It makes your app susceptible to compatibility problems in the future and possible variations from machine to machine.

On Vista we are almost forced into having to separate out the application files and any external files either referenced by, or created by our app. The application files go in c:\Program Files and any other data goes in C:\ProgramData.

To reference C:\ProgramData in your code do this:

Code:
Application.CommonAppDataPath

I guess the right thing to do is make sure that we check for Vista or XP and then use the correct folder references accordingly. Any thoughts on this?
 
that sounds like an excellent plan.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top