예제 #1
0
파일: Word.cs 프로젝트: Zagii/EAkzg
 public void odswiezSpisTresci()
 {
     //return;
     if (spisTresci != null)
     {
         spisTresci.LowerHeadingLevel = 2;
         spisTresci.Update();
     }
 }
예제 #2
0
파일: Word.cs 프로젝트: Zagii/EAkzg
        public void wstawSpisTresci(object bookmark)
        {
            //  return;
            Wordy.Range oRng = doc.Bookmarks.get_Item(ref bookmark).Range;
            oRng.Text = "";
            object oTrueValue  = true;
            object oFalseValue = false;

            oRng.Collapse();
            //   Wordy.Range rangeForTOC = doc.Range( oRng.Start, ref missing);

            /*spisTresci = doc.TablesOfContents.Add(oRng, ref oTrueValue, ref missing, ref missing,
             *            ref missing, ref missing, ref oTrueValue,
             *               ref oTrueValue, ref oTrueValue, ref oTrueValue,
             *               ref oTrueValue, ref oTrueValue);
             */
            spisTresci = doc.TablesOfContents.Add(oRng, ref oFalseValue, ref missing, ref missing,
                                                  ref missing, ref missing, ref oTrueValue,
                                                  ref oTrueValue, ref oTrueValue, ref oTrueValue,
                                                  ref oTrueValue, ref oTrueValue);

            object h1 = stylRozdz;
            object h2 = stylPodrozdz;
            object h3 = stylPodrozdz2;

            object h4 = stylPodrozdz3;

            /*       string txt="";
             * foreach (Wordy.HeadingStyle x in spisTresci.HeadingStyles)
             * {
             *     txt += x.ToString();
             * }*/


            spisTresci.HeadingStyles.Add(ref h1, 1);
            spisTresci.HeadingStyles.Add(ref h2, 2);
            //    spisTresci.HeadingStyles.Add(ref h3, 3);
            //    spisTresci.HeadingStyles.Add(ref h4, 4);
            spisTresci.LowerHeadingLevel = 2;
            spisTresci.Update();
        }
예제 #3
0
        private void WordBtn_Click(object sender, RoutedEventArgs e)
        {
            //先选择文件保存的位置
            Forms.SaveFileDialog dialog = new Forms.SaveFileDialog();
            if (dialog.ShowDialog() != Forms.DialogResult.OK)
            {
                return;
            }
            String outputFile = dialog.FileName;

            dialog.Dispose();

            MsWord.Application oWordApplic = null;
            MsWord.Document    odoc;
            try
            {
                //创建操作Word文档的项目
                oWordApplic = new MsWord.Application();
                object missing = Missing.Value;
                String filedir = Directory.GetCurrentDirectory(); // debug目录
                Console.WriteLine("当前文件目录: " + filedir);

                #region 创建Word文档的小节
                MsWord.Range curRange;
                object       curTxt;
                int          curSectionNum = 1;
                odoc = oWordApplic.Documents.Add(ref missing, ref missing, ref missing, ref missing);
                odoc.Activate();
                Console.WriteLine("正在生成文档小节");
                object section_nextPage = MsWord.WdBreakType.wdSectionBreakNextPage;
                object page_break       = MsWord.WdBreakType.wdPageBreak;
                //添加4个分节符,共5个小节,小节的类型是下一页
                for (int si = 0; si < 4; si++)
                {
                    //注意:Paragraphs的下标从 1 开始而不是 0
                    odoc.Paragraphs[1].Range.InsertParagraphAfter();
                    odoc.Paragraphs[1].Range.InsertBreak(ref section_nextPage);
                }
                #endregion

                #region 插入摘要文本并设置文本格式
                //从文件 abstract.txt中读取文本内容作为摘要部分内容,Word文档的内容都是具有格式的。
                Console.WriteLine("正在插入摘要内容");
                curSectionNum = 1;
                curRange      = odoc.Sections[curSectionNum].Range.Paragraphs[1].Range;
                curRange.Select();
                string one_str, key_word;
                //摘要的文本来自 abstract.txt文件
                StreamReader file_abstract = new StreamReader("../../../COM_source/abstract.txt");
                oWordApplic.Options.Overtype = false; //overtype改写模式
                MsWord.Selection curSelection = oWordApplic.Selection;

                if (curSelection.Type == MsWord.WdSelectionType.wdSelectionNormal)
                {
                    one_str = file_abstract.ReadLine();  //读入题目
                    curSelection.TypeText(one_str);
                    curSelection.TypeParagraph();        //添加段落标记
                    curSelection.TypeText(" 摘要");
                    curSelection.TypeParagraph();        //添加段落标志,进入下一段
                    key_word = file_abstract.ReadLine(); //读入关键字
                    one_str  = file_abstract.ReadLine(); //读入段落文本(例子有 3段)
                    while (one_str != null)
                    {
                        curSelection.TypeText(one_str);
                        curSelection.TypeParagraph(); //读入一个段落就写一段
                        one_str = file_abstract.ReadLine();
                    }
                    curSelection.TypeText("关键字: ");
                    curSelection.TypeText(key_word); ///写关键字
                    curSelection.TypeParagraph();
                }
                file_abstract.Close();

                //下面开始设置摘要的格式(上面是内容的读取)
                //摘要的标题
                curRange           = odoc.Sections[curSectionNum].Range.Paragraphs[1].Range;
                curTxt             = curRange.Paragraphs[1].Range.Text;
                curRange.Font.Name = "宋体";
                curRange.Font.Size = 22;
                curRange.Paragraphs[1].Alignment = MsWord.WdParagraphAlignment.wdAlignParagraphCenter; //中间对齐

                //“摘要” 二字
                curRange = odoc.Sections[curSectionNum].Range.Paragraphs[2].Range; //第二段
                curRange.Select();                                                 //成为选择区(在其上进行后续的操作)
                curRange.Paragraphs[1].Alignment = MsWord.WdParagraphAlignment.wdAlignParagraphCenter;
                curRange.Font.Name = "黑体";
                curRange.Font.Size = 16;

                //摘要正文
                //自己用一个变量存起来这个常用的数据
                MsWord.Range curSectionRange = odoc.Sections[curSectionNum].Range;

                odoc.Sections[curSectionNum].Range.Paragraphs[3].Alignment = MsWord.WdParagraphAlignment.wdAlignParagraphCenter;
                for (int i = 3; i < odoc.Sections[curSectionNum].Range.Paragraphs.Count; i++)
                {
                    curRange = odoc.Sections[curSectionNum].Range.Paragraphs[i].Range;
                    curTxt   = curRange.Paragraphs[1].Range.Text;
                    curRange.Select();

                    curRange.Font.Name = "宋体";
                    curRange.Font.Size = 12;
                    odoc.Sections[curSectionNum].Range.Paragraphs[i].LineSpacingRule = MsWord.WdLineSpacing.wdLineSpaceMultiple;
                    //多倍行距,1.25倍,这里的浮点值是以point为单位的,不是行距倍数 ?????-----
                    odoc.Sections[curSectionNum].Range.Paragraphs[i].LineSpacing = 15f;
                    curSectionRange.Paragraphs[i].IndentFirstLineCharWidth(2); //段落开头的缩进
                }
                //设置“关键字:”为黑体
                curRange = curRange.Paragraphs[curRange.Paragraphs.Count].Range; //最后一段的 range
                curTxt   = curRange.Paragraphs[1].Range.Text;                    //能不能直接用 curRange.Text ??????---------
                object range_start, range_end;
                range_start = curRange.Start;
                range_end   = curRange.Start + 4;// ??
                curRange    = odoc.Range(ref range_start, ref range_end);
                curTxt      = curRange.Text;
                curRange.Select();
                curRange.Font.Bold = 1;
                curRange.Font.Name = "黑体"; //应该加上

                #endregion

                #region 插入目录并设置目录格式

                Console.WriteLine("正在插入目录");
                curSectionNum = 2; //转到第二小节了
                curRange      = odoc.Sections[curSectionNum].Range.Paragraphs[1].Range;
                curRange.Select(); //变成选择区

                //插入目录时指定的参数
                object useheading_styles     = true; //使用内置的目录标题样式
                object upperheading_level    = 1;    //最高的标题级别 (大纲视图的选择)
                object lowerheading_level    = 3;    //最低标题级别
                object useelds               = 1;    //true表示创建的是目录 ?????
                object tableid               = 1;
                object RightAlignPageNumbers = true; //右边距对齐的页码
                object IncludePageNumbers    = true; //目录中包含页码

                curSelection = oWordApplic.Selection;
                curSelection.TypeText("目录"); //第一段写标题
                curSelection.TypeParagraph();
                curSelection.Select();

                curRange = odoc.Sections[curSectionNum].Range.Paragraphs[2].Range; //转到第二段
                //插入的表格会替代当前range
                //range为非折叠时,TablesOfContents会代替range,引起小节数减少
                curRange.Collapse();
                //在这里添加了目录,记得在最后需要更新目录
                MsWord.TableOfContents tablesOfContents = odoc.TablesOfContents.Add(curRange, ref useheading_styles, ref upperheading_level, ref lowerheading_level, ref useelds, ref tableid, ref RightAlignPageNumbers,
                                                                                    ref IncludePageNumbers, ref missing, ref missing, ref missing, ref missing);


                odoc.Sections[curSectionNum].Range.Paragraphs[1].Alignment       = MsWord.WdParagraphAlignment.wdAlignParagraphCenter; //设置"目录"两个字的样式
                odoc.Sections[curSectionNum].Range.Paragraphs[1].Range.Font.Bold = 1;
                odoc.Sections[curSectionNum].Range.Paragraphs[1].Range.Font.Name = "黑体";
                odoc.Sections[curSectionNum].Range.Paragraphs[1].Range.Font.Size = 16;
                #endregion

                #region 插入第一章正文并设置正文格式
                curSectionNum = 3;
                odoc.Sections[curSectionNum].Range.Paragraphs[1].Range.Select();
                curRange = odoc.Sections[curSectionNum].Range.Paragraphs[1].Range;
                Console.WriteLine("正在设置标题样式");
                object wdFontSizeIndex;
                //此序号在Word中的编号是格式->显示格式->样式和格式->显示所有样式的序号
                //14是标题一,一级标题:三号黑体
                wdFontSizeIndex = 14;
                oWordApplic.ActiveDocument.Styles.get_Item(ref wdFontSizeIndex).ParagraphFormat.Alignment = MsWord.WdParagraphAlignment.wdAlignParagraphCenter;
                oWordApplic.ActiveDocument.Styles.get_Item(ref wdFontSizeIndex).Font.Name = "黑体";
                oWordApplic.ActiveDocument.Styles.get_Item(ref wdFontSizeIndex).Font.Size = 16; //三号
                wdFontSizeIndex = 15;                                                           //15是标题二,二级标题:小三号黑体
                oWordApplic.ActiveDocument.Styles.get_Item(ref wdFontSizeIndex).Font.Name = "黑体";
                oWordApplic.ActiveDocument.Styles.get_Item(ref wdFontSizeIndex).Font.Size = 15; //小三号

                //用指定的标题来设置文本格式
                object Style1 = MsWord.WdBuiltinStyle.wdStyleHeading1; //一级标题:三号黑体
                object Style2 = MsWord.WdBuiltinStyle.wdStyleHeading2; //二级标题:小三号黑体

                odoc.Sections[curSectionNum].Range.Select();
                curSelection = oWordApplic.Selection;
                //读入第一章文本信息
                StreamReader file_content = new StreamReader("../../../COM_source/content.txt");
                one_str = file_content.ReadLine(); // 一级标题
                curSelection.TypeText(one_str);
                curSelection.TypeParagraph();
                one_str = file_content.ReadLine(); //二级标题
                curSelection.TypeText(one_str);
                curSelection.TypeParagraph();
                one_str = file_content.ReadLine(); //正文
                while (one_str != null)
                {
                    curSelection.TypeText(one_str);
                    curSelection.TypeParagraph();
                    one_str = file_content.ReadLine(); //正文
                }
                file_content.Close();
                //段落的对齐方式
                curRange = odoc.Sections[curSectionNum].Range.Paragraphs[1].Range;
                curRange.set_Style(ref Style1);
                odoc.Sections[curSectionNum].Range.Paragraphs[1].Alignment = MsWord.WdParagraphAlignment.wdAlignParagraphCenter;
                curRange = odoc.Sections[curSectionNum].Range.Paragraphs[2].Range;
                curRange.set_Style(ref Style2);
                //第一章正文文本格式
                for (int i = 3; i < odoc.Sections[curSectionNum].Range.Paragraphs.Count; i++)
                {
                    curRange = odoc.Sections[curSectionNum].Range.Paragraphs[i].Range;
                    curRange.Select();
                    curRange.Font.Name = "宋体";
                    curRange.Font.Size = 12;
                    odoc.Sections[curSectionNum].Range.Paragraphs[i].LineSpacingRule = MsWord.WdLineSpacing.wdLineSpaceMultiple;
                    //多倍行距,1.25倍
                    odoc.Sections[curSectionNum].Range.Paragraphs[i].LineSpacing = 15f;
                    odoc.Sections[curSectionNum].Range.Paragraphs[i].IndentFirstLineCharWidth(2);
                }
                #endregion

                #region 插入表格并设置表格格式
                Console.WriteLine("正在插入第二章内容-表格");
                curSectionNum = 4;
                odoc.Sections[curSectionNum].Range.Select(); //调整了 oWordApplic.Selection
                curRange     = odoc.Sections[curSectionNum].Range.Paragraphs[1].Range;
                curSelection = oWordApplic.Selection;
                curSelection.TypeText("2 表格");
                curSelection.TypeParagraph();
                curSelection.TypeText("表格示例");
                curSelection.TypeParagraph();
                curSelection.TypeParagraph();
                curRange = odoc.Sections[curSectionNum].Range.Paragraphs[3].Range;
                odoc.Sections[curSectionNum].Range.Paragraphs[3].Range.Select();
                curSelection = oWordApplic.Selection;
                MsWord.Table oTable;
                oTable = curRange.Tables.Add(curRange, 5, 3, ref missing, ref missing); //添加表格
                oTable.Range.ParagraphFormat.Alignment = MsWord.WdParagraphAlignment.wdAlignParagraphCenter;
                oTable.Range.Rows.Alignment            = MsWord.WdRowAlignment.wdAlignRowCenter;
                oTable.Columns[1].Width         = 80;
                oTable.Columns[2].Width         = 180;
                oTable.Columns[3].Width         = 80;
                oTable.Cell(1, 1).Range.Text    = " 字段";
                oTable.Cell(1, 2).Range.Text    = " 描述";
                oTable.Cell(1, 3).Range.Text    = " 数据类型";
                oTable.Cell(2, 1).Range.Text    = "ProductID";
                oTable.Cell(2, 2).Range.Text    = " 产品标识";
                oTable.Cell(2, 3).Range.Text    = " 字符串";
                oTable.Borders.InsideLineStyle  = MsWord.WdLineStyle.wdLineStyleSingle;
                oTable.Borders.OutsideLineStyle = MsWord.WdLineStyle.wdLineStyleSingle;
                curRange = odoc.Sections[curSectionNum].Range.Paragraphs[1].Range;
                curRange.set_Style(ref Style1);
                curRange.ParagraphFormat.Alignment = MsWord.WdParagraphAlignment.wdAlignParagraphCenter;

                #endregion

                #region 插入图片
                Console.WriteLine("正在插入第三章内容-插入图片");
                curSectionNum = 5;
                odoc.Sections[curSectionNum].Range.Paragraphs[1].Range.Select();  // understand what Select() means for ----------------------------- dif with oWordApplic.Selection
                curRange     = odoc.Sections[curSectionNum].Range.Paragraphs[1].Range;
                curSelection = oWordApplic.Selection;
                curSelection.TypeText("3 图片");
                curSelection.TypeParagraph();
                curSelection.TypeText("图片示例");
                curSelection.TypeParagraph();
                curSelection.InlineShapes.AddPicture(@"F:\VS2017 code\WinProcedure\COM_source\whu.png", ref missing, ref missing, ref missing); //TODO:插入图片,图片名无法根据相对路径来找到
                curRange = odoc.Sections[curSectionNum].Range.Paragraphs[1].Range;
                curRange.set_Style(ref Style1);
                curRange.ParagraphFormat.Alignment = MsWord.WdParagraphAlignment.wdAlignParagraphCenter;

                #endregion

                #region 设置各小节的页眉页脚
                Console.WriteLine("正在设置第1节摘要的页眉内容");
                //设置页脚 section 1 摘要
                curSectionNum = 1;
                odoc.Sections[curSectionNum].Range.Select();
                //进入页脚视图
                oWordApplic.ActiveWindow.View.SeekView = MsWord.WdSeekView.wdSeekCurrentPageFooter;
                odoc.Sections[curSectionNum].Headers[MsWord.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.
                Borders[MsWord.WdBorderType.wdBorderBottom].LineStyle = MsWord.WdLineStyle.wdLineStyleNone;
                oWordApplic.Selection.HeaderFooter.PageNumbers.RestartNumberingAtSection = true;
                oWordApplic.Selection.HeaderFooter.PageNumbers.NumberStyle    = MsWord.WdPageNumberStyle.wdPageNumberStyleUppercaseRoman;
                oWordApplic.Selection.HeaderFooter.PageNumbers.StartingNumber = 1;
                //切换到文档
                oWordApplic.ActiveWindow.ActivePane.View.SeekView = MsWord.WdSeekView.wdSeekMainDocument;
                Console.WriteLine("正在设置第 2节目录页眉内容");
                //设置页脚section 2目录
                curSectionNum = 2;
                odoc.Sections[curSectionNum].Range.Select();
                //进入页脚视图
                oWordApplic.ActiveWindow.ActivePane.View.SeekView = MsWord.WdSeekView.wdSeekCurrentPageFooter;
                odoc.Sections[curSectionNum].Headers[MsWord.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Borders[MsWord.WdBorderType.wdBorderBottom].LineStyle = MsWord.WdLineStyle.wdLineStyleNone;
                oWordApplic.Selection.HeaderFooter.PageNumbers.RestartNumberingAtSection = false;
                oWordApplic.Selection.HeaderFooter.PageNumbers.NumberStyle = MsWord.WdPageNumberStyle.wdPageNumberStyleUppercaseRoman;

                //切换到文档
                oWordApplic.ActiveWindow.ActivePane.View.SeekView = MsWord.WdSeekView.wdSeekMainDocument;
                //第 1章页眉页码设置
                curSectionNum = 3;
                odoc.Sections[curSectionNum].Range.Select();
                //切换进入页脚视图
                oWordApplic.ActiveWindow.ActivePane.View.SeekView = MsWord.WdSeekView.wdSeekCurrentPageFooter;
                curSelection = oWordApplic.Selection;
                curRange     = curSelection.Range;
                //本节页码不续上节
                oWordApplic.Selection.HeaderFooter.PageNumbers.RestartNumberingAtSection = true;
                //页码格式为阿拉伯数字
                oWordApplic.Selection.HeaderFooter.PageNumbers.NumberStyle = MsWord.WdPageNumberStyle.wdPageNumberStyleArabic;
                //起始页码为 1
                oWordApplic.Selection.HeaderFooter.PageNumbers.StartingNumber = 1;
                //添加页码域
                object fieldpage = MsWord.WdFieldType.wdFieldPage;
                oWordApplic.Selection.Fields.Add(oWordApplic.Selection.Range, ref fieldpage, ref missing, ref missing);
                //居中对齐
                oWordApplic.Selection.ParagraphFormat.Alignment = MsWord.WdParagraphAlignment.wdAlignParagraphCenter;
                //本小节不链接到上一节
                odoc.Sections[curSectionNum].Headers[MsWord.WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious = false;
                //切换进入正文视图
                oWordApplic.ActiveWindow.ActivePane.View.SeekView = MsWord.WdSeekView.wdSeekMainDocument;

                #endregion

                //更新目录
                Console.WriteLine("正在更新目录");
                tablesOfContents.Update();
                //tablesOfContents.UpdatePageNumbers();

                #region Word文档保存
                Console.WriteLine("正在保存文档");
                //object fileName = filedir + "/My_doc.doc";
                object fileName = outputFile;

                odoc.SaveAs2(fileName);
                odoc.Close();
                //Word文档任务完成后,需要释放Document对象和Application对象
                Console.WriteLine("正在释放 COM 资源");
                //释放COM资源
                Marshal.ReleaseComObject(odoc);
                odoc = null;
                oWordApplic.Quit();
                Marshal.ReleaseComObject(oWordApplic);
                oWordApplic = null;
                #endregion
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.Message);
            }
            finally
            {
                #region 终止Word进程
                Console.WriteLine("正在结束Word进程");
                //关闭Word进程
                System.Diagnostics.Process[] allProcess = System.Diagnostics.Process.GetProcesses();
                for (int i = 0; i < allProcess.Length; i++)
                {
                    string procName = allProcess[i].ProcessName;
                    if (String.Compare(procName, "WINWORD") == 0)
                    {
                        if (allProcess[i].Responding && !allProcess[i].HasExited)
                        {
                            allProcess[i].Kill();
                        }
                    }
                }
                #endregion

                //启动word
                if (!outputFile.Contains(".doc"))
                {
                    outputFile = String.Concat(outputFile, ".docx");
                }
                if (wordCheckBox.IsChecked.Value && File.Exists(outputFile))
                {
                    Process.Start(outputFile);
                }
            }
        }
예제 #4
0
        public static bool CreateWordDocParagraph(string qname, string[][] qdata, IVsThreadedWaitDialog2 dlg)
        {
            //InitializeVariables();
            System.Net.WebClient webclient = new System.Net.WebClient();
            webclient.Credentials = System.Net.CredentialCache.DefaultCredentials;

            var           application = new MSWord.Application();
            var           document    = new MSWord.Document();
            object        missing     = System.Reflection.Missing.Value;
            int           pagewidth   = 800;
            bool          bcanceled;
            int           progress = 1;
            StringBuilder strbld   = new StringBuilder();

            MSWord.WdBuiltinStyle[] hstyles = new MSWord.WdBuiltinStyle[8];
            hstyles[0] = MSWord.WdBuiltinStyle.wdStyleHeading1;
            hstyles[1] = MSWord.WdBuiltinStyle.wdStyleHeading2;
            hstyles[2] = MSWord.WdBuiltinStyle.wdStyleHeading3;
            hstyles[3] = MSWord.WdBuiltinStyle.wdStyleHeading4;
            hstyles[4] = MSWord.WdBuiltinStyle.wdStyleHeading5;
            hstyles[5] = MSWord.WdBuiltinStyle.wdStyleHeading6;
            hstyles[6] = MSWord.WdBuiltinStyle.wdStyleHeading7;
            hstyles[7] = MSWord.WdBuiltinStyle.wdStyleHeading8;

            application.Visible       = true;
            application.WindowState   = MSWord.WdWindowState.wdWindowStateMinimize;
            application.DisplayAlerts = MSWord.WdAlertLevel.wdAlertsNone;
            document = application.Documents.Add();
            //document.PageSetup.Orientation = MSWord.WdOrientation.wdOrientLandscape;
            document.PageSetup.LeftMargin   = 20;
            document.PageSetup.RightMargin  = 20;
            document.PageSetup.TopMargin    = 20;
            document.PageSetup.BottomMargin = 20;
            document.PageSetup.PageWidth    = pagewidth + 40;

            MSWord.Paragraph prg = document.Paragraphs.Add();
            prg.Range.Text = "Query results for " + qname + " [" + DateTime.Now + "]";
            prg.Range.set_Style(MSWord.WdBuiltinStyle.wdStyleTitle);
            //prg.Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
            prg.SpaceAfter  = 100;
            prg.SpaceBefore = 100;
            prg.Range.InsertParagraphAfter();

            prg            = document.Paragraphs.Add();
            prg.Range.Text = "Table of Contents";
            prg.Range.set_Style(MSWord.WdBuiltinStyle.wdStyleTocHeading);
            prg.Range.InsertParagraphAfter();

            prg            = document.Paragraphs.Add();
            prg.Range.Text = "TOC";
            prg.Range.InsertParagraphAfter();
            prg.Range.InsertBreak(MSWord.WdBreakType.wdPageBreak);

            prg = document.Paragraphs.Add();
            MSWord.Table qtable = document.Tables.Add(prg.Range, qdata.Length, 1);
            prg.Range.InsertParagraphAfter();

            prg            = document.Paragraphs.Add();
            prg.Range.Text = "Appendix";
            prg.Range.set_Style(MSWord.WdBuiltinStyle.wdStyleTitle);
            prg.Range.InsertParagraphAfter();

            object styleTypeTable = MSWord.WdStyleType.wdStyleTypeTable;

            MSWord.Style styl = document.Styles.Add("New Table Style", ref styleTypeTable);
            styl.ParagraphFormat.LineSpacingRule = MSWord.WdLineSpacing.wdLineSpaceSingle;
            styl.ParagraphFormat.SpaceAfter      = 0;
            styl.ParagraphFormat.SpaceBefore     = 0;
            styl.Table.TopPadding    = 0;
            styl.Table.BottomPadding = 0;
            styl.Table.LeftPadding   = 0;
            styl.Table.RightPadding  = 0;
            //styl.Table.Borders.Enable = 1;
            qtable.Range.set_Style(styl);

            MSWord.Cell cell = qtable.Cell(1, 1);

            int headerwidth = 85;
            int levelwidth  = 100;

            object rows, cols;

            for (int i = 0; i < qdata.Length; i++)
            {
                int level = int.Parse(qdata[i][1]);
                if (level > 0)
                {
                    rows = 1;
                    cols = 2;
                    cell.Split(ref rows, ref cols);
                    cell.Range.Cells.SetWidth(level * levelwidth, MSWord.WdRulerStyle.wdAdjustSameWidth);
                    cell = cell.Next;
                }

                rows = 1 + (string.IsNullOrWhiteSpace(qdata[i][0]) ? 0 : 1) + (string.IsNullOrWhiteSpace(qdata[i][6]) ? 0 : 1);
                cols = 2;
                cell.Split(ref rows, ref cols);
                cell.Merge(cell.Next);

                string title = String.Format("{0} {1} ({2})",
                                             qdata[i][2],
                                             (qdata[i][5].Length > 128 ? qdata[i][5].Remove(128) : qdata[i][5]).Replace("\n", "").Replace("\r", "").Replace("\t", ""),
                                             qdata[i][4]);

                cell.Range.Text      = title;
                cell.Range.Font.Bold = 1;
                cell.Range.set_Style(hstyles[level < 8 ? level:7]);
                cell = cell.Next;

                dlg.UpdateProgress("Exporting Work Item query to Microsoft Word document", "Adding to Word document " + qdata[i][3] + " #" + qdata[i][4], "status", progress++, 100, false, out bcanceled);
                if (progress == 100)
                {
                    progress = 0;
                }
                if (bcanceled)
                {
                    application.Visible = true;
                    Marshal.ReleaseComObject(document);
                    Marshal.ReleaseComObject(application);

                    return(true);
                }

                /*cell.Range.Text = "Title";
                 * cell.Range.Cells.SetWidth(headerwidth, MSWord.WdRulerStyle.wdAdjustSameWidth);
                 * cell.Range.Font.Bold = 1;
                 * cell = cell.Next;
                 *
                 * cell.Range.Text = qdata[i][4];
                 * cell = cell.Next;*/

                /*cell.Range.Text = "Description";
                 * cell.Range.Cells.SetWidth(headerwidth, MSWord.WdRulerStyle.wdAdjustSameWidth);
                 * cell.Range.Font.Bold = 1;
                 * cell = cell.Next;*/

                if (!string.IsNullOrWhiteSpace(qdata[i][6]))
                {
                    cell.Merge(cell.Next);
                    cell.Range.Text = qdata[i][6];
                    cell            = cell.Next;
                }

                if (!string.IsNullOrWhiteSpace(qdata[i][0]))
                {
                    cell.Range.Text = "Attachments";
                    cell.Range.Cells.SetWidth(headerwidth, MSWord.WdRulerStyle.wdAdjustSameWidth);
                    cell.Range.Font.Bold = 1;
                    cell = cell.Next;

                    var query = qdata[i][0]
                                .Split(';')
                                .Where(x => !string.IsNullOrWhiteSpace(x))
                                .Select(x =>
                    {
                        string[] ch = x.Split('~');
                        return(new { name = ch[0], value = ch[1] });
                    }).ToArray();
                    cell.Split(query.Length, 1);

                    foreach (var kvp in query)
                    {
                        string localpath = Path.GetTempFileName() + "." + kvp.name;
                        //try { File.Delete(localpath); }
                        //catch { }

                        try
                        {
                            webclient.DownloadFile(kvp.value, localpath);
                        }
                        catch (Exception ex)
                        {
                            localpath = "";
                            Utilities.OutputCommandString(ex.ToString());
                        }

                        prg            = document.Paragraphs.Add();
                        prg.Range.Text = kvp.name;
                        prg.Range.set_Style(MSWord.WdBuiltinStyle.wdStyleHeading3);

                        cell.Range.Text = kvp.name;
                        document.Hyperlinks.Add(cell.Range, missing, prg.Range);

                        prg.Range.InsertParagraphAfter();
                        document.InlineShapes.AddHorizontalLineStandard(prg.Range);
                        prg = document.Paragraphs.Add();

                        if (!string.IsNullOrEmpty(localpath))
                        {
                            try
                            {
                                Image img = Image.FromFile(localpath);
                                img.Dispose();
                                document.InlineShapes.AddPicture(localpath, false, true, prg.Range);
                            }
                            catch
                            {
                                if (Path.GetExtension(kvp.name).Equals(".sql", StringComparison.InvariantCultureIgnoreCase) ||
                                    Path.GetExtension(kvp.name).Equals(".txt", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    prg.Range.InsertFile(localpath);//, prg.Range, false, true, false);
                                }
                                else
                                {
                                    MSWord.InlineShape shape = document.InlineShapes.AddOLEObject(missing, localpath, false, false, missing, missing, missing, prg.Range);
                                    if (shape.OLEFormat.ClassType.ToString() != "Package")
                                    {
                                        shape.Width = document.PageSetup.PageWidth - 40;
                                    }
                                }
                            }
                        }
                        cell = cell.Next;
                    }
                    if (query.Length == 0)
                    {
                        cell = cell.Next;
                    }
                }
            }

            object styleTypePara = MSWord.WdStyleType.wdStyleTypeParagraph;

            MSWord.Style styl2 = document.Styles.Add("New Paragraph Style", ref styleTypePara);
            //styl2.ParagraphFormat.set_Style(MSWord.WdBuiltinStyle.wdStyleNormal);
            styl2.ParagraphFormat.LeftIndent      = 100;
            styl2.ParagraphFormat.RightIndent     = 100;
            styl2.ParagraphFormat.LineSpacingRule = MSWord.WdLineSpacing.wdLineSpaceSingle;
            styl2.ParagraphFormat.SpaceAfter      = 0;
            styl2.ParagraphFormat.SpaceBefore     = 0;

            MSWord.Paragraph       tocpara = document.Paragraphs[3];
            MSWord.TableOfContents tblct   = document.TablesOfContents.Add(tocpara.Range, missing, 1, 1);
            tblct.Update();
            //tblct.Range.set_Style(styl2);

            //application.Visible = true;

            Marshal.ReleaseComObject(document);
            Marshal.ReleaseComObject(application);

            return(false);
        }