Beispiel #1
0
        /// <summary>
        /// 设置文档格式并添加内容
        /// </summary>
        /// <param name="filePath">文件名</param>
        /// <returns></returns>
        public static bool AddContent(string filePath)
        {
            try
            {
                Object oMissing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word._Application WordApp = new Application();
                WordApp.Visible = true;
                object filename = filePath;
                Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing,
                                                                                         ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                                                                         ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                //设置居左
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;

                //设置文档的行间距
                WordApp.Selection.ParagraphFormat.LineSpacing = 15f;
                //插入段落
                //WordApp.Selection.TypeParagraph();
                Microsoft.Office.Interop.Word.Paragraph para;
                para = WordDoc.Content.Paragraphs.Add(ref oMissing);
                //正常格式
                para.Range.Text = "This is paragraph 1";
                //para.Range.Font.Bold = 2;
                //para.Range.Font.Color = WdColor.wdColorRed;
                //para.Range.Font.Italic = 2;
                para.Range.InsertParagraphAfter();

                para.Range.Text = "This is paragraph 2";
                para.Range.InsertParagraphAfter();

                //插入Hyperlink
                Microsoft.Office.Interop.Word.Selection mySelection = WordApp.ActiveWindow.Selection;
                mySelection.Start = 9999;
                mySelection.End   = 9999;
                Microsoft.Office.Interop.Word.Range myRange = mySelection.Range;

                Microsoft.Office.Interop.Word.Hyperlinks myLinks = WordDoc.Hyperlinks;
                object linkAddr = @"http://www.cnblogs.com/lantionzy";
                Microsoft.Office.Interop.Word.Hyperlink myLink = myLinks.Add(myRange, ref linkAddr,
                                                                             ref oMissing);
                WordApp.ActiveWindow.Selection.InsertAfter("\n");

                //落款
                WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();
                WordDoc.Paragraphs.Last.Alignment  = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;

                //保存
                WordDoc.Save();
                WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                return(false);
            }
        }
 private static void CreateWordDocument(string Link, string FilePath = "")
 {
     try
     {
         Microsoft.Office.Interop.Word._Application MSWORD = new Microsoft.Office.Interop.Word.Application();
         MSWORD.Visible = false;
         object missing = Type.Missing;
         Microsoft.Office.Interop.Word._Document MSWORDdoc = MSWORD.Documents.Add(ref missing, ref missing, ref missing, ref missing);
         Microsoft.Office.Interop.Word.Paragraph TEXTBLOCK = MSWORDdoc.Paragraphs.Add(ref missing);
         object style_name = "Heading 1";
         TEXTBLOCK.Range.set_Style(ref style_name);
         TEXTBLOCK.Range.InsertParagraphAfter();
         if (string.IsNullOrEmpty(FilePath))
         {
             TEXTBLOCK.Range.Text = RandomString_Contents(randLength.Next(5, 999));
         }
         else
         {
             TEXTBLOCK.Range.Text = RandomString_Words(FilePath);
         }
         TEXTBLOCK.Range.InsertParagraphAfter();
         object filename  = Directory.GetCurrentDirectory() + "\\" + RandomString_Contents(randLength.Next(3, 12)) + ".doc";
         string PREV_FONT = TEXTBLOCK.Range.Font.Name;
         TEXTBLOCK.Range.Font.Name = "Courier New";
         TEXTBLOCK.Range.InsertParagraphAfter();
         TEXTBLOCK.Range.Font.Name = PREV_FONT;
         Microsoft.Office.Interop.Word.Hyperlinks myLinks = MSWORDdoc.Hyperlinks;
         if (Link.Contains("http") == false && Link.Contains("https") == false)
         {
             Link = "http://" + Link;
         }
         object linkAddr = Link;
         Microsoft.Office.Interop.Word.Selection mySelection = MSWORDdoc.ActiveWindow.Selection;
         mySelection.Start = 9999;
         mySelection.End   = 9999;
         Microsoft.Office.Interop.Word.Range     myRange = mySelection.Range;
         Microsoft.Office.Interop.Word.Hyperlink myLink  = myLinks.Add(myRange, ref linkAddr, ref missing);
         MSWORDdoc.ActiveWindow.Selection.InsertAfter("\n");
         MSWORDdoc.SaveAs(ref filename, 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);
         object save_changes = false;
         MSWORDdoc.Close(ref save_changes, ref missing, ref missing);
         MSWORD.Quit(ref save_changes, ref missing, ref missing);
         Console.WriteLine("App done! .Doc created!");
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
         Console.WriteLine(e.StackTrace);
     }
 }
        private void btn_LinkText_Click(object sender, RibbonControlEventArgs e)
        {
            Word.Hyperlinks myLinks = Globals.ThisAddIn.Application.ActiveDocument.Hyperlinks;
            string          anchor  = InputDialog.ShowDialog("What text do you want linked (case sensitive)?", "Link Text");

            if (!String.IsNullOrEmpty(anchor))
            {
                string link     = InputDialog.ShowDialog("Where do you want the link to point (leave blank to remove link)?", "Link Text");
                object findText = anchor;
                Globals.ThisAddIn.Application.Selection.Find.Execute(findText, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true, Word.WdFindWrap.wdFindAsk, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                if (Globals.ThisAddIn.Application.Selection.Find.Found)
                {
                    while (Globals.ThisAddIn.Application.Selection.Find.Found)
                    {
                        if (String.IsNullOrEmpty(link))
                        {
                            if (Globals.ThisAddIn.Application.Selection.Hyperlinks.Count > 0)
                            {
                                DialogResult result = MessageBox.Show("You entered a blank link. Do you wish to remove existing links? Click Yes to continue. Click No to ignore this one instance. Click Cancel to abort the whole linking process.", "Link Text: Confirm Removal", MessageBoxButtons.YesNoCancel);
                                if (result == DialogResult.Yes)
                                {
                                    Debug.WriteLine("Removing link from the anchor '" + anchor + "'.");
                                    foreach (Word.Hyperlink objLink in Globals.ThisAddIn.Application.Selection.Hyperlinks)
                                    {
                                        objLink.Delete();
                                    }
                                }
                                else if (result == DialogResult.No)
                                {
                                    Debug.WriteLine("Skipping this instance of the anchor.");
                                }
                                else
                                {
                                    Debug.WriteLine("Aborting!");
                                    break;
                                }
                            }
                        }
                        else
                        {
                            if (Globals.ThisAddIn.Application.Selection.Hyperlinks.Count > 0)
                            {
                                DialogResult result = MessageBox.Show("This instance of the text is already linked. Do you want to update this link to '" + link + "'? Click Yes to proceed. Click No to skip this one instance. Click Cancel to abort the whole linking process.", "Link Text: Confirm Link", MessageBoxButtons.YesNoCancel);
                                if (result == DialogResult.Yes)
                                {
                                    Debug.WriteLine("Updating link to the anchor '" + anchor + "', pointing to '" + link + "'.");
                                    foreach (Word.Hyperlink objLink in Globals.ThisAddIn.Application.Selection.Hyperlinks)
                                    {
                                        objLink.Delete();
                                    }
                                    Word.Hyperlink mylink = myLinks.Add(Globals.ThisAddIn.Application.Selection.Range, link);
                                }
                                else if (result == DialogResult.No)
                                {
                                    Debug.WriteLine("Skipping this instance of the anchor.");
                                }
                                else
                                {
                                    Debug.WriteLine("Aborting!");
                                    break;
                                }
                            }
                            else
                            {
                                DialogResult result = MessageBox.Show("Do you wish to link the selected text? It will point to the following URL: '" + link + "'. Click Yes to proceed. Click No to skip this one instance. Click Cancel to abort the whole linking process.", "Link Text: Confirm Link", MessageBoxButtons.YesNoCancel);
                                if (result == DialogResult.Yes)
                                {
                                    Debug.WriteLine("Adding link to the anchor '" + anchor + "', pointing to '" + link + "'.");
                                    Word.Hyperlink mylink = myLinks.Add(Globals.ThisAddIn.Application.Selection.Range, link);
                                }
                                else if (result == DialogResult.No)
                                {
                                    Debug.WriteLine("Skipping this instance of the anchor.");
                                }
                                else
                                {
                                    Debug.WriteLine("Aborting!");
                                    break;
                                }
                            }
                        }
                        Globals.ThisAddIn.Application.Selection.Find.Execute(findText, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true, Word.WdFindWrap.wdFindAsk, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    }
                    MessageBox.Show("Linking process complete.");
                }
                else
                {
                    MessageBox.Show("No instances of '" + anchor + "' found. Remember that the search is case sensitive and, if text is selected, will only search inside that text.");
                }
            }
        }