Ejemplo n.º 1
0
        private void listBoxSearchResults_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string      curItem     = ((ListBoxItem)listBoxSearchResults.SelectedItem).Key;
            string      url         = string.Format("{0}?objectId={1}", DownloadUrl, curItem);
            HttpContent httpcontent = HttpClientGetAsync(url).Result;

            byte[] result = httpcontent.ReadAsByteArrayAsync().Result;

            var tempFileName = string.Format("{0}\\{1}.docx", Path.GetTempPath(), curItem);

            File.WriteAllBytes(tempFileName, result);

            StringBuilder content = new StringBuilder();

            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

            object miss     = System.Reflection.Missing.Value;
            object path     = tempFileName;
            object readOnly = true;

            Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss,
                                                                              ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
            Microsoft.Office.Interop.Word.Document  currentDocument = AddinModule.CurrentInstance.WordApp.ActiveDocument;
            Microsoft.Office.Interop.Word.Selection currentPos      = AddinModule.CurrentInstance.WordApp.Application.Selection;

            currentDocument.Range(currentPos.Range.Start, currentPos.Range.End).InsertAfter(docs.Content.Text);
            docs.Close();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// A function that merges Microsoft Word Documents that uses a template specified by the user
        /// </summary>
        /// <param name="filesToMerge">An array of files that we want to merge</param>
        /// <param name="outputFilename">The filename of the merged document</param>
        /// <param name="insertPageBreaks">Set to true if you want to have page breaks inserted after each document</param>
        /// <param name="documentTemplate">The word document you want to use to serve as the template</param>
        private void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks, string documentTemplate)
        {
            object defaultTemplate = documentTemplate;
            object missing         = System.Type.Missing;
            object pageBreak       = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
            object outputFile      = outputFilename;

            // Create  a new Word application
            Microsoft.Office.Interop.Word._Application wordApplication = new Microsoft.Office.Interop.Word.Application();
            if (filesToMerge.Count() == 1)
            {
                pageBreak = false;
            }
            try
            {
                // Create a new file based on our template
                Microsoft.Office.Interop.Word._Document wordDocument = wordApplication.Documents.Add(ref defaultTemplate, ref missing, ref missing, ref missing);

                // Make a Word selection object.
                Microsoft.Office.Interop.Word.Selection selection = wordApplication.Selection;

                int index = 0;

                // Loop thru each of the Word documents
                foreach (string file in filesToMerge)
                {
                    // Insert the files to our template
                    selection.InsertFile(file, ref missing, ref missing, ref missing, ref missing);

                    //Do we want page breaks added after each documents?
                    if (insertPageBreaks && index != filesToMerge.Count() - 1)
                    {
                        selection.InsertBreak(ref pageBreak);
                    }

                    index++;
                }

                // Save the document to it's output file.
                wordDocument.SaveAs(ref outputFile, 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);

                // Clean up!
                wordDocument.Close(ref missing, ref missing, ref missing);
                wordDocument = null;
            }
            catch (Exception ex)
            {
                //I didn't include a default error handler so i'm just throwing the error
                throw ex;
            }
            finally
            {
                // Finally, Close our Word application
                wordApplication.Quit(ref missing, ref missing, ref missing);
            }
        }
Ejemplo n.º 3
0
        //</Snippet2>


        //---------------------------------------------------------------------
        //<Snippet3>
        private void AddData(System.Data.DataRow row, string companyName)
        {
            object missing = System.Type.Missing;

            // Create a table if it doesn't already exist.
            if (Globals.ThisDocument.Tables.Count == 0)
            {
                try
                {
                    // Create a table.
                    Microsoft.Office.Interop.Word.Table tbl = Globals.ThisDocument.Tables.Add
                                                                  (Globals.ThisDocument.Application.Selection.Range, 1, 4, ref missing, ref missing);

                    // Insert headings.
                    SetHeadings(tbl.Cell(1, 1), "Company Name");
                    SetHeadings(tbl.Cell(1, 2), "Product Name");
                    SetHeadings(tbl.Cell(1, 3), "Quantity");
                    SetHeadings(tbl.Cell(1, 4), "Unit Price");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Problem creating Products table: " + ex.Message,
                                    "Actions Pane", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            // Add data from data row to the table.
            Microsoft.Office.Interop.Word.Selection selection = Globals.ThisDocument.Application.Selection;

            if (selection.Tables.Count > 0)
            {
                Microsoft.Office.Interop.Word.Row newRow = Globals.ThisDocument.Tables[1].Rows.Add(ref missing);

                newRow.Range.Font.Bold = 0;

                newRow.Range.ParagraphFormat.Alignment =
                    Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;

                newRow.Cells[4].Range.ParagraphFormat.Alignment =
                    Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;

                newRow.Cells[1].Range.Text = companyName;
                newRow.Cells[2].Range.Text = row["ProductName"].ToString();
                newRow.Cells[3].Range.Text = row["QuantityPerUnit"].ToString();
                newRow.Cells[4].Range.Text = Math.Round(Convert.ToDouble(row["UnitPrice"])).ToString("#,##0.00");
            }
            else
            {
                MessageBox.Show("Cursor must be within a table.",
                                "Actions Pane", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void listBoxSearchResults_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string      curItem     = ((ListBoxItem)listBoxSearchResults.SelectedItem).Key;
            string      url         = string.Format("{0}?objectId={1}", DownloadUrl, curItem);
            HttpContent httpcontent = HttpClientGetAsync(url).Result;

            byte[] result = httpcontent.ReadAsByteArrayAsync().Result;

            var tempFileName = string.Format("{0}\\{1}.docx", Path.GetTempPath(), curItem);

            File.WriteAllBytes(tempFileName, result);

            StringBuilder content = new StringBuilder();

            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

            object miss     = System.Reflection.Missing.Value;
            object path     = tempFileName;
            object readOnly = true;

            Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss,
                                                                              ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
            Microsoft.Office.Interop.Word.Document  currentDocument = Globals.ThisAddIn.Application.ActiveDocument;
            Microsoft.Office.Interop.Word.Selection currentPos      = Globals.ThisAddIn.Application.Selection;

            currentDocument.Range(currentPos.Range.Start, currentPos.Range.End).InsertAfter(docs.Content.Text);

            //for (int i = 0; i < docs.Paragraphs.Count; i++)
            //{
            //    //content.Append("\r\n" + docs.Paragraphs[i + 1].Range.Text.ToString());
            //    currentDocument.Range(currentPos.Range.Start, currentPos.Range.End).InsertAfter(docs.Content.Text);
            //}

            docs.Close();

            //Object start = currentDocument.Content.Start;
            //Object end = currentDocument.Content.End;
            //currentDocument.Range(ref start, ref end).Text = content.ToString();
            //currentDocument.Range(currentPos.Range.Start, currentPos.Range.End).Text = content.ToString();
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
            Object MISSING = System.Reflection.Missing.Value;

            Microsoft.Office.Interop.Word.Document aDoc = null;
            aDoc            = wordApp.Documents.Open("D:\\SampleTemplate.docx");
            wordApp.Visible = false;
            FindAndReplace(wordApp, "<name>", "Test Name");
            wordApp.ActiveDocument.Characters.Last.Select();
            Microsoft.Office.Interop.Word.Selection selection = wordApp.Selection;
            Microsoft.Office.Interop.Word.Range     range     = selection.Range;
            Microsoft.Office.Interop.Word.Bookmarks bookmarks = aDoc.Bookmarks;
            int count = bookmarks.Count;

            Microsoft.Office.Interop.Word.Bookmark bookmark = bookmarks.Add("bookmark2", range);
            wordApp.Selection.Collapse();
            wordApp.Selection.TypeText("appended text........");
            aDoc.Save();
            aDoc.Close();
            Console.WriteLine("Hello World!");
        }
Ejemplo n.º 6
0
 private void Application_WindowSelectionChange(Microsoft.Office.Interop.Word.Selection Sel)
 {
     SetLocation();
 }
Ejemplo n.º 7
0
        public void Merge() // Shall be called on a separate thread and shall make use of the member variables
        {
            object missing    = System.Type.Missing;
            object pageBreak  = Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage;
            object outputFile = outputFilePath;

            // Create a new Word application
            Microsoft.Office.Interop.Word._Application wordApplication = new Microsoft.Office.Interop.Word.Application();

            bSuccessfulOperation = true;
            try
            {
                // Create a new file
                Microsoft.Office.Interop.Word.Document wordDocument = wordApplication.Documents.Add(
                    ref missing
                    , ref missing
                    , ref missing
                    , ref missing);

                // Make a Word selection object.
                Microsoft.Office.Interop.Word.Selection selection = wordApplication.Selection;

                //Count the number of documents to insert;
                int documentCount = fileList.Length;

                //A counter that signals that we shoudn't insert a page break at the end of document.
                int breakStop = 0;

                int iCounter   = 0;
                int iFileCount = fileList.Length;

                // Iterate through the entire list of files
                foreach (string file in fileList)
                {
                    Console.WriteLine("Merging file: " + file);
                    breakStop++;
                    // Insert the files to our template
                    selection.InsertFile(
                        file
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing);

                    //Do we want page breaks added after each documents?
                    if (insertPageBreaks && breakStop != documentCount)
                    {
                        selection.InsertBreak(ref pageBreak);
                    }
                    iCounter++;
                    UpdateProgressBar(((float)iCounter / iFileCount) * 100);
                }

                // Save the document to it's output file.
                wordDocument.SaveAs(
                    ref outputFile
                    , 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);

                // Get some stats about the merged file
                lineCount = wordDocument.Sentences.Count;
                wordCount = wordDocument.Words.Count;

                Microsoft.Office.Interop.Word.WdStatistic stat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages;
                pageCount = wordDocument.ComputeStatistics(stat, ref missing);
                wordDocument.Save();

                // Clean up!
                wordDocument = null;
            }
            catch (Exception ex)
            {
                //I didn't include a default error handler so i'm just throwing the error
                Console.WriteLine(ex);
                bSuccessfulOperation = false;
            }
            finally
            {
                // Finally, Close our Word application
                wordApplication.Quit(ref missing, ref missing, ref missing);
            }


            DisplayResult(); // Done, show the results
        }
Ejemplo n.º 8
0
 void _WordApplication_WindowBeforeRightClick(Microsoft.Office.Interop.Word.Selection Sel, ref bool Cancel)
 {
     DisplayInWatchWindow(countWindowBeforeRightClick++, System.Reflection.MethodInfo.GetCurrentMethod().Name);
 }
Ejemplo n.º 9
0
 void _WordApplication_WindowSelectionChange(Microsoft.Office.Interop.Word.Selection Sel)
 {
     DisplayInWatchWindow(countWindowSelectionChange++, System.Reflection.MethodInfo.GetCurrentMethod().Name);
 }
Ejemplo n.º 10
0
 void _WordApplication_XMLSelectionChange(Microsoft.Office.Interop.Word.Selection Sel, Microsoft.Office.Interop.Word.XMLNode OldXMLNode, Microsoft.Office.Interop.Word.XMLNode NewXMLNode, ref int Reason)
 {
     DisplayInWatchWindow(countXMLSelectionChange++, System.Reflection.MethodInfo.GetCurrentMethod().Name);
 }