user1517951
user1517951

Reputation: 756

Manage Word documents on a WPF application

I'm creating a WPF application that loads and edits a Word document. The code is this bellow:

            public bool Generate(string path, ProjectVO project)
            {
                saveAs = path;
                projectName = project.Name;
                projectVersion = project.Version;

                object missing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
                //Setup our Word.Document class we'll use.
                Microsoft.Office.Interop.Word.Document aDoc = null;


                // Check to see that file exists
                if (File.Exists((string)fileName))
                {
                    DateTime today = DateTime.Now;

                    object readOnly = false;
                    object isVisible = false;
                    try
                    {

                        //Set Word to be not visible.
                        wordApp.Visible = false;

                        //Open the word document
                        aDoc = wordApp.Documents.Open(ref fileName, ref missing,
                            ref readOnly, ref missing, ref missing, ref missing,
                            ref missing, ref missing, ref missing, ref missing,
                            ref missing, ref isVisible, ref missing, ref missing,
                            ref missing, ref missing);

                        // Activate the document
                        aDoc.Activate();

                        // Find Place Holders and Replace them with Values.
                        this.FindAndReplace(wordApp, "{Name}", projectName);
                        this.FindAndReplace(wordApp, "{Version}", projectVersion);



                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        //Close the document
                        aDoc.Close(ref missing, ref missing, ref missing);
                        return false;


                    }


                }
                else
                {
                   return false;
                }

                //Save the document as the correct file name.
                aDoc.SaveAs(ref saveAs, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing);




                //Close the document
                aDoc.Close(ref missing, ref missing, ref missing);
                return true;

        }

it works ok, but when the client doesn't have the Microsoft Word, the Word document is not created. Is there any way to create it, and save on a folder, even if the client can not open it? Or is there any way to save it as PDF or TXT as an alternative form for when Word is not installed? Thanks a lot!

Upvotes: 0

Views: 2675

Answers (2)

Xilmiki
Xilmiki

Reputation: 1502

You can generate a docx without word using "open xml sdk" but is not very easy.

http://www.microsoft.com/en-us/download/details.aspx?id=5124

Sdk include a tool to auto generate the code from an existing docx file.

To can generate a pdf i suggest to use "iTextSharp" libray http://itextpdf.com/

Upvotes: 2

xing
xing

Reputation: 447

You can't create a Word document if the client machine doesn't have Word installed.

You can certainly create a txt file but just not through Office Interop.

Upvotes: 2

Related Questions