Ejemplo n.º 1
0
        /// <summary>
        /// 复制其他文件内容到当前文件
        /// </summary>
        /// <returns></returns>
        public string CopyOtherFileContentToWord(string filePth, string bookmark, bool isCloseTheFile = true)
        {
            try
            {
                Document htmldoc = OpenWord(filePth);
                htmldoc.Content.Copy();
                Range range = GetBookmarkRank(_currentWord, bookmark);
                range.Select();
                range.Paste();
                range.Select();
                int tableCount = _wordApp.Selection.Tables.Count;
                if (tableCount > 0)
                {
                    foreach (Table table in _wordApp.Selection.Tables)
                    {
                        table.AutoFitBehavior(WdAutoFitBehavior.wdAutoFitWindow);
                    }
                }

                if (isCloseTheFile)
                {
                    CloseWord(htmldoc, filePth);
                }
            }
            catch (Exception ex)
            {
                _needWrite = false;
                Dispose();
                throw new Exception(string.Format("错误信息:{0}.{1}", ex.StackTrace.ToString(), ex.Message));
            }

            return("保存成功");
        }
Ejemplo n.º 2
0
        private void MoveText(CardControl TOPCARD, CardControl BOTTOMCARD)
        {
            //Globals.ThisAddIn.userControlWPF.ListCardControls.IndexOf(card)


            if (int.Parse(BOTTOMCARD.IDfield) != 1)// if it is not the first card
            {
                string nobookmark = "None";

                if ((TOPCARD.Bookmarkfield.ToUpper() != nobookmark.ToUpper()) && (BOTTOMCARD.Bookmarkfield.ToUpper() != nobookmark.ToUpper()))     // only if both are linked to a bookmark - to upper to maintain compatibility with old format
                {
                    Word.Bookmark TOPbmk   = Globals.ThisAddIn.Application.ActiveDocument.Bookmarks[TOPCARD.Bookmarkfield];
                    Word.Range    TOPRange = TOPbmk.Range;

                    // move the text
                    Word.Bookmark BOTTOMbmk   = Globals.ThisAddIn.Application.ActiveDocument.Bookmarks[BOTTOMCARD.Bookmarkfield];
                    Word.Range    BOTTOMRange = BOTTOMbmk.Range;
                    BOTTOMRange.Cut();

                    Word.Range temprange         = Globals.ThisAddIn.Application.ActiveDocument.Range(TOPRange.Start, TOPRange.End); // store range of prevbookmark
                    int        topbookmarkLenght = TOPRange.End - TOPRange.Start;                                                    // calculate lenght of prevbookmark subtracting end and start
                    string     topbkmName        = TOPbmk.Name;                                                                      // save name of prevbookmark
                    TOPbmk.Delete();                                                                                                 // delete prevbookmark
                    // add a temporary space character at the start of the previous bookmark
                    // paste the text
                    Word.Range newrange = Globals.ThisAddIn.Application.ActiveDocument.Range(temprange.Start, temprange.Start);
                    newrange.Paste();
                    Word.Range    newbkmrkrange = Globals.ThisAddIn.Application.ActiveDocument.Range(newrange.End, newrange.End + topbookmarkLenght);
                    Word.Bookmark newbookmark   = Globals.ThisAddIn.Application.ActiveDocument.Bookmarks.Add(topbkmName, newbkmrkrange);  // create new bookmark with start at the end of the bookmark and end at start+lenght

                    //BOTTOMbmk.Range.Select();
                }
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// 拷贝粘贴表格
 /// </summary>
 /// <param name="WordRange"></param>
 /// <param name="CopyDoc"></param>
 /// <param name="TableIndex"></param>
 public static void CopyPasteTable(ref Microsoft.Office.Interop.Word.Range WordRange, ref Microsoft.Office.Interop.Word.Document CopyDoc, int TableIndex)
 {
     CopyDoc.Tables[TableIndex].Range.Copy();
     WordRange.Paste();
     System.Windows.Forms.Clipboard.Clear();
     clsWordControl.CloseDoc(CopyDoc, false);
 }
Ejemplo n.º 4
0
 private static void WriteLine(this LinePicture lp, MWord.Range range)
 {
     if (lp.PictureContent == null)
     {
         return;
     }
     Clipboard.SetImage(lp.PictureContent);
     range.Paste();
 }
Ejemplo n.º 5
0
        private List <string> G_Str_Files = new List <string>(); //定义字符串集合

        private void btn_split_Click(object sender, EventArgs e)
        {
            btn_Merge.Enabled = false;                                  //停用合并按钮
            ThreadPool.QueueUserWorkItem(                               //开始线程池
                (pp) =>                                                 //使用lambda表达式
            {
                G_wa = new Microsoft.Office.Interop.Word.Application(); //创建应用程序对象
                Word.Document P_MainDocument =                          //新建合并文档对象
                                               G_wa.Documents.Add(ref G_missing, ref G_missing
                                                                  , ref G_missing, ref G_missing);
                foreach (string P_Str in G_Str_Files)               //遍历文档的集合
                {
                    object P_strs            = P_Str;               //创建object对象
                    Word.Document P_Document = G_wa.Documents.Open( //打开Word文档
                        ref P_strs, ref G_missing, ref G_missing, ref G_missing
                        , ref G_missing, ref G_missing, ref G_missing, ref G_missing
                        , ref G_missing, ref G_missing, ref G_missing, ref G_missing
                        , ref G_missing, ref G_missing, ref G_missing, ref o);
                    Word.Range P_Range_temp =                                        //得到文档全部范围
                                              P_Document.Range(ref G_missing, ref G_missing);
                    P_Range_temp.Select();                                           //选择文档全部范围
                    P_Range_temp.Copy();                                             //复制文档全部范围
                    Word.Range P_Range_temp2 =                                       //得到文档的范围
                                               P_MainDocument.Range(ref G_missing, ref G_missing);
                    object P_end = Word.WdCollapseDirection.wdCollapseEnd;           //创建object对象
                    P_Range_temp2.Collapse(ref P_end);                               //折叠文档范围
                    P_Range_temp2.Select();                                          //选择档的最后位置
                    P_Range_temp2.Paste();                                           //粘贴文档内容
                    ((Word._Document)P_Document).Close(ref G_missing, ref G_missing, //关闭文档
                                                       ref G_missing);
                }
                object P_SavePath = G_SaveFileDialog.FileName; //创建object对象
                P_MainDocument.SaveAs(                         //保存合并后的文档
                    ref P_SavePath, ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                    ref G_missing, ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                    ref G_missing, ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                    ref G_missing);
                ((Word._Application)G_wa.Application).Quit( //退出应用程序
                    ref G_missing, ref G_missing, ref G_missing);
                this.Invoke(                                //调用窗体线程
                    (MethodInvoker)(() =>                   //使用lambda表达式
                {
                    Clipboard.Clear();                      //清空剪切板
                    MessageBox.Show(                        //提示已经创建Word
                        "成功合并Word文档!", "提示!");
                    btn_Merge.Enabled = true;               //启用合并按钮
                }));
            });
        }
Ejemplo n.º 6
0
        private List <string> G_Str_Files = new List <string>(); //定義字串集合

        private void btn_split_Click(object sender, EventArgs e)
        {
            btn_Merge.Enabled = false;                                  //停用合併按鈕
            ThreadPool.QueueUserWorkItem(                               //開始線程池
                (pp) =>                                                 //使用lambda表達式
            {
                G_wa = new Microsoft.Office.Interop.Word.Application(); //建立應用程式物件
                Word.Document P_MainDocument =                          //新建合併文件檔物件
                                               G_wa.Documents.Add(ref G_missing, ref G_missing
                                                                  , ref G_missing, ref G_missing);
                foreach (string P_Str in G_Str_Files)               //深度搜尋文件檔的集合
                {
                    object P_strs            = P_Str;               //建立object物件
                    Word.Document P_Document = G_wa.Documents.Open( //打開Word文件檔
                        ref P_strs, ref G_missing, ref G_missing, ref G_missing
                        , ref G_missing, ref G_missing, ref G_missing, ref G_missing
                        , ref G_missing, ref G_missing, ref G_missing, ref G_missing
                        , ref G_missing, ref G_missing, ref G_missing, ref G_missing);
                    Word.Range P_Range_temp =                                        //得到文件檔全部範圍
                                              P_Document.Range(ref G_missing, ref G_missing);
                    P_Range_temp.Select();                                           //選擇文件檔全部範圍
                    P_Range_temp.Copy();                                             //複製文件檔全部範圍
                    Word.Range P_Range_temp2 =                                       //得到文件檔的範圍
                                               P_MainDocument.Range(ref G_missing, ref G_missing);
                    object P_end = Word.WdCollapseDirection.wdCollapseEnd;           //建立object物件
                    P_Range_temp2.Collapse(ref P_end);                               //折疊文件檔範圍
                    P_Range_temp2.Select();                                          //選擇檔的最後位置
                    P_Range_temp2.Paste();                                           //貼上文件檔內容
                    ((Word._Document)P_Document).Close(ref G_missing, ref G_missing, //關閉文件檔
                                                       ref G_missing);
                }
                object P_SavePath = G_SaveFileDialog.FileName; //建立object物件
                P_MainDocument.SaveAs(                         //儲存合併後的文件檔
                    ref P_SavePath, ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                    ref G_missing, ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                    ref G_missing, ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                    ref G_missing);
                ((Word._Application)G_wa.Application).Quit( //退出應用程式
                    ref G_missing, ref G_missing, ref G_missing);
                this.Invoke(                                //呼叫視窗線程
                    (MethodInvoker)(() =>                   //使用lambda表達式
                {
                    Clipboard.Clear();                      //清空剪下板
                    MessageBox.Show(                        //提示已經建立Word
                        "成功合併Word文件檔!", "提示!");
                    btn_Merge.Enabled = true;               //啟用合併按鈕
                }));
            });
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 创建Word文档方法
        /// </summary>
        private void AddFile()
        {
            Word.Document P_Document = G_wa.Documents.Add(       //创建新文档
                ref G_missing, ref G_missing, ref G_missing, ref G_missing);
            Word.Range P_Range = P_Document.Paragraphs[1].Range; //得到文档范围
            P_Range.Paste();                                     //将剪切板内容粘贴到文档中
            object G_str_path = string.Format(                   //计算文件保存路径
                @"{0}\{1}", G_FolderBrowserDailog.SelectedPath,
                DateTime.Now.ToString("yyyy年M月d日h时m分s秒fff毫秒") + ".doc");

            P_Document.SaveAs(//保存Word文件
                ref G_str_path,
                ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                ref G_missing, ref G_missing, ref G_missing);
            ((Word._Document)P_Document).Close(ref G_missing, ref G_missing, ref G_missing);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 建立Word文件檔方法
        /// </summary>
        private void AddFile()
        {
            Word.Document P_Document = G_wa.Documents.Add(       //建立新文件檔
                ref G_missing, ref G_missing, ref G_missing, ref G_missing);
            Word.Range P_Range = P_Document.Paragraphs[1].Range; //得到文件檔範圍
            P_Range.Paste();                                     //將剪下板內容貼上到文件檔中
            object G_str_path = string.Format(                   //計算文件儲存路徑
                @"{0}\{1}", G_FolderBrowserDailog.SelectedPath,
                DateTime.Now.ToString("yyyy年M月d日h時m分s秒fff毫秒") + ".doc");

            P_Document.SaveAs(//儲存Word文件
                ref G_str_path,
                ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                ref G_missing, ref G_missing, ref G_missing);
            ((Word._Document)P_Document).Close(ref G_missing, ref G_missing, ref G_missing);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 复制其他文件的文本框到当前word
        /// </summary>
        /// <returns></returns>
        public string CopyImageToWord(string otherFilePath, string bookmark, bool isCloseTheFile)
        {
            try
            {
                Document otherFile = OpenWord(otherFilePath);
                otherFile.Select();

                Range bookmarkPic = GetBookmarkRank(_currentWord, bookmark);

                try
                {
                    ShapeRange  shapeRange  = otherFile.Shapes.Range(1);
                    InlineShape inlineShape = shapeRange.ConvertToInlineShape();
                    inlineShape.Select();
                    _wordApp.Selection.Copy();
                }
                catch (Exception)
                {
                    Shape shape       = otherFile.Shapes[1];
                    Frame inlineShape = shape.ConvertToFrame();
                    inlineShape.Select();
                    _wordApp.Selection.Copy();
                }
                finally
                {
                    bookmarkPic.Paste();
                    if (isCloseTheFile)
                    {
                        CloseWord(otherFile, otherFilePath);
                    }
                }
            }
            catch (Exception ex)
            {
                _needWrite = false;
                Dispose();
                throw new Exception(string.Format("错误信息:{0}.{1}", ex.StackTrace.ToString(), ex.Message));
            }

            return("创建成功");
        }
Ejemplo n.º 10
0
        public void PastetoWord(System.Drawing.Image image, System.Drawing.Imaging.ImageFormat format)
        {
            try
            {
                //Byte[] imageCapture = ToByteArray(bitmap, System.Drawing.Imaging.ImageFormat.Bmp);

                System.Windows.Clipboard.SetDataObject(image, true);
                object start = 0;
                object end   = 0;
                Microsoft.Office.Interop.Word.Range rng = doc.Range(ref start, ref end);
                rng.Paste();
                if (image != null)
                {
                    ((IDisposable)image).Dispose();
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(" " + ex.Message);
            }
        }
Ejemplo n.º 11
0
 /// <summary>
 /// 复制其他文件的表格到当前word
 /// </summary>
 /// <returns></returns>
 public string CopyTableToWord(string otherFilePath, string bookmark, int tableIndex, bool isCloseTheFile)
 {
     try
     {
         Document otherFile = OpenWord(otherFilePath);
         Range    range     = GetBookmarkRank(_currentWord, bookmark);
         otherFile.Tables[tableIndex].Range.Copy();
         range.Paste();
         range.Tables[1].AutoFitBehavior(WdAutoFitBehavior.wdAutoFitWindow);
         if (isCloseTheFile)
         {
             CloseWord(otherFile, otherFilePath);
         }
     }
     catch (Exception ex)
     {
         _needWrite = false;
         Dispose();
         throw new Exception(string.Format("错误信息:{0}.{1}", ex.StackTrace.ToString(), ex.Message));
     }
     return("创建成功");
 }
Ejemplo n.º 12
0
    public void ImprimirEtiquetasArreglos(JObject pJDocumento, int pIdImpresionTemplate, string pRutaTemplate, CConexion pConexion)
    {
        CImpresionEtiquetas         ImpresionEtiquetas = new CImpresionEtiquetas();
        Dictionary <string, object> Parametros         = new Dictionary <string, object>();

        Parametros.Add("IdImpresionTemplate", pIdImpresionTemplate);

        object oEndOfDoc = "\\endofdoc"; /* \endofdoc es un bookmark predefinido */
        Object oMissing  = System.Reflection.Missing.Value;

        Word.Range wrdRng = documentoWord.Bookmarks.get_Item(ref oEndOfDoc).Range;
        int        ind    = 0;
        object     oPos;
        double     dPos = aplicacionWord.InchesToPoints(10);

        foreach (JProperty oPropiedadDocumento in pJDocumento.Properties())
        {
            if (oPropiedadDocumento.Value.Type.ToString() == "Object")
            {
                JObject JTablaConceptos = new JObject();
                JTablaConceptos = (JObject)pJDocumento[oPropiedadDocumento.Name];
                foreach (JProperty oPropiedadTabla in JTablaConceptos.Properties())
                {
                    if (oPropiedadTabla.Name == "Tipo")
                    {
                        if (oPropiedadTabla.Value.ToString() == "Conceptos")
                        {
                            for (int i = 1; i <= documentoWord.Tables.Count; i++)
                            {
                                Word.Table wTable = documentoWord.Tables[i];
                                Word.Cell  pCell  = wTable.Cell(1, 1);

                                if (wTable.ID != null && wTable.ID == "tblReceptor")
                                {
                                    foreach (CImpresionEtiquetas oImpresionEtiqueta in ImpresionEtiquetas.LlenaObjetosFiltros(Parametros, pConexion))
                                    {
                                        Word.Range rangePlantilla = wTable.Range;

                                        int rc = wTable.Rows.Count;
                                        // Selecciona y copia el renglon Layout.
                                        rangePlantilla.Start = 1;
                                        rangePlantilla.End   = wTable.Rows[rc].Cells[wTable.Rows[rc].Cells.Count].Range.End;
                                        rangePlantilla.Find.ClearFormatting();
                                        rangePlantilla.Find.Text = oImpresionEtiqueta.Etiqueta;
                                        rangePlantilla.Find.Replacement.ClearFormatting();


                                        if (pJDocumento[oImpresionEtiqueta.Campo] != null)
                                        {
                                            rangePlantilla.Find.Replacement.Text = pJDocumento[oImpresionEtiqueta.Campo].ToString();
                                        }
                                        object replaceAll = Word.WdReplace.wdReplaceAll;
                                        rangePlantilla.Find.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                                    }
                                }

                                if (wTable.ID != null && wTable.ID == JTablaConceptos["NombreTabla"].ToString())
                                {
                                    bool pRowsNum = false;
                                    if (wTable.Rows.Count > 1)
                                    {
                                        pRowsNum = true;
                                    }

                                    int contador = 1;
                                    // Layaout
                                    foreach (JObject JInventario in JTablaConceptos["Inventarios"])
                                    {
                                        contador = contador + 1;
                                        Word.Range range = wTable.Range;

                                        // Renglon Layout
                                        int selectedRow = 1;

                                        // Selecciona y copia el renglon Layout.
                                        range.Start = wTable.Rows[selectedRow].Cells[1].Range.Start;
                                        range.End   = wTable.Rows[selectedRow].Cells[wTable.Rows[selectedRow].Cells.Count].Range.End;
                                        range.Copy();

                                        // Inserta renglon nuevo al final de la tabla.
                                        wTable.Rows.Add(ref oMissing);

                                        // Mueve el cursor a la primera celda del renglon nuevo.
                                        range.Start = wTable.Rows[wTable.Rows.Count].Cells[1].Range.Start;
                                        range.End   = range.Start;

                                        // Pega los valores del renglon layout al renglon nuevo.
                                        range.Paste();

                                        int m = wTable.Rows.Count;

                                        Word.Row  renglonPlantilla = wTable.Rows[m];
                                        Word.Cell celdaPlantilla   = renglonPlantilla.Cells[1];

                                        for (int x = 1; x <= celdaPlantilla.Tables.Count; x++)
                                        {
                                            bool sRowsNum = false;
                                            if (celdaPlantilla.Tables[x].Rows.Count > 3)
                                            {
                                                sRowsNum = true;
                                            }
                                            foreach (CImpresionEtiquetas oImpresionEtiqueta in ImpresionEtiquetas.LlenaObjetosFiltros(Parametros, pConexion))
                                            {
                                                Word.Range rangePlantilla = wTable.Range;

                                                // Selecciona y copia el renglon Layout.
                                                rangePlantilla.Start = celdaPlantilla.Tables[x].Rows[1].Cells[1].Range.Start;
                                                rangePlantilla.End   = celdaPlantilla.Tables[x].Rows[1].Cells[celdaPlantilla.Tables[x].Rows[1].Cells.Count].Range.End;
                                                rangePlantilla.Find.ClearFormatting();
                                                rangePlantilla.Find.Text = oImpresionEtiqueta.Etiqueta;
                                                rangePlantilla.Find.Replacement.ClearFormatting();

                                                if (JInventario[oImpresionEtiqueta.Campo] != null)
                                                {
                                                    rangePlantilla.Find.Replacement.Text = JInventario[oImpresionEtiqueta.Campo].ToString();
                                                }
                                                object replaceAll = Word.WdReplace.wdReplaceAll;
                                                rangePlantilla.Find.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                                            }

                                            foreach (JObject JConcepto in JInventario["Lista"])
                                            {
                                                Word.Range sRange = celdaPlantilla.Tables[x].Range;

                                                // Selecciona y copia el renglon Layout.
                                                sRange.Start = celdaPlantilla.Tables[x].Rows[3].Cells[1].Range.Start;
                                                sRange.End   = celdaPlantilla.Tables[x].Rows[3].Cells[celdaPlantilla.Tables[x].Rows[3].Cells.Count].Range.End;
                                                sRange.Copy();

                                                // Inserta renglon nuevo al final de la tabla.
                                                celdaPlantilla.Tables[x].Rows.Add(ref oMissing);

                                                // Mueve el cursor a la primera celda del renglon nuevo.
                                                sRange.Start = celdaPlantilla.Tables[x].Rows[celdaPlantilla.Tables[x].Rows.Count].Cells[1].Range.Start;
                                                sRange.End   = sRange.Start;

                                                // Pega los valores del renglon layout al renglon nuevo.
                                                sRange.Paste();

                                                foreach (CImpresionEtiquetas oImpresionEtiqueta in ImpresionEtiquetas.LlenaObjetosFiltros(Parametros, pConexion))
                                                {
                                                    Word.Range rowRange = wTable.Range;

                                                    // Selecciona y copia el renglon Layout.
                                                    sRange.Find.ClearFormatting();
                                                    sRange.Find.Text = oImpresionEtiqueta.Etiqueta;
                                                    sRange.Find.Replacement.ClearFormatting();

                                                    if (JConcepto[oImpresionEtiqueta.Campo] != null)
                                                    {
                                                        sRange.Find.Replacement.Text = JConcepto[oImpresionEtiqueta.Campo].ToString();
                                                    }
                                                    object replaceAll = Word.WdReplace.wdReplaceAll;
                                                    sRange.Find.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                                                }
                                            }
                                            if (sRowsNum)
                                            {
                                                JObject JTotal = new JObject();
                                                JTotal = (JObject)JInventario["Total"];

                                                Word.Range sRange = celdaPlantilla.Tables[x].Range;

                                                // Selecciona y copia el renglon Layout.
                                                sRange.Start = celdaPlantilla.Tables[x].Rows[4].Cells[1].Range.Start;
                                                sRange.End   = celdaPlantilla.Tables[x].Rows[4].Cells[celdaPlantilla.Tables[x].Rows[4].Cells.Count].Range.End;
                                                sRange.Copy();

                                                // Inserta renglon nuevo al final de la tabla.
                                                celdaPlantilla.Tables[x].Rows.Add(ref oMissing);

                                                // Mueve el cursor a la primera celda del renglon nuevo.
                                                sRange.Start = celdaPlantilla.Tables[x].Rows[celdaPlantilla.Tables[x].Rows.Count].Cells[1].Range.Start;
                                                sRange.End   = sRange.Start;

                                                // Pega los valores del renglon layout al renglon nuevo.
                                                sRange.Paste();

                                                foreach (CImpresionEtiquetas oImpresionEtiqueta in ImpresionEtiquetas.LlenaObjetosFiltros(Parametros, pConexion))
                                                {
                                                    Word.Range rowRange = wTable.Range;

                                                    // Selecciona y copia el renglon Layout.
                                                    sRange.Find.ClearFormatting();
                                                    sRange.Find.Text = oImpresionEtiqueta.Etiqueta;
                                                    sRange.Find.Replacement.ClearFormatting();

                                                    if (JTotal[oImpresionEtiqueta.Campo] != null)
                                                    {
                                                        sRange.Find.Replacement.Text = JTotal[oImpresionEtiqueta.Campo].ToString();
                                                    }
                                                    object replaceAll = Word.WdReplace.wdReplaceAll;
                                                    sRange.Find.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                                                }
                                                celdaPlantilla.Tables[x].Rows[4].Delete();
                                            }
                                            celdaPlantilla.Tables[x].Rows[3].Delete();
                                        }
                                    }

                                    if (pRowsNum)
                                    {
                                        Word.Range sRange = wTable.Range;

                                        // Selecciona y copia el renglon Layout.
                                        sRange.Start = wTable.Rows[2].Cells[1].Range.Start;
                                        sRange.End   = wTable.Rows[2].Cells[wTable.Rows[2].Cells.Count].Range.End;
                                        sRange.Copy();

                                        // Inserta renglon nuevo al final de la tabla.
                                        wTable.Rows.Add(ref oMissing);

                                        // Mueve el cursor a la primera celda del renglon nuevo.
                                        sRange.Start = wTable.Rows[wTable.Rows.Count].Cells[1].Range.Start;
                                        sRange.End   = sRange.Start;

                                        // Pega los valores del renglon layout al renglon nuevo.
                                        sRange.Paste();

                                        foreach (CImpresionEtiquetas oImpresionEtiqueta in ImpresionEtiquetas.LlenaObjetosFiltros(Parametros, pConexion))
                                        {
                                            Word.Range rangePlantilla = wTable.Range;

                                            // Selecciona y copia el renglon Layout.
                                            rangePlantilla.Find.ClearFormatting();
                                            rangePlantilla.Find.Text = oImpresionEtiqueta.Etiqueta;
                                            rangePlantilla.Find.Replacement.ClearFormatting();


                                            if (pJDocumento[oImpresionEtiqueta.Campo] != null)
                                            {
                                                rangePlantilla.Find.Replacement.Text = pJDocumento[oImpresionEtiqueta.Campo].ToString();
                                            }
                                            object replaceAll = Word.WdReplace.wdReplaceAll;
                                            rangePlantilla.Find.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                                        }
                                        wTable.Rows[2].Delete();
                                    }
                                    wTable.Rows[1].Delete();
                                }
                            }
                        }
                    }
                }
            }
        }
    }
Ejemplo n.º 13
0
        private void BTNprintGen_Click(object sender, EventArgs e)// кнопка печати сгенерированного билета
        {
            // var namespec = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
            //int q2 dataGridViewResult.SelectedRows[2].Cells[3].Value();


            try
            {
                var q1       = dataGridViewResult.Rows[1].Cells[0].Value.ToString();
                var namespec = dataGridViewResult.Rows[1].Cells[2].Value.ToString();
                ////var spec = dataGridViewResult.CurrentRow.Cells[16].Value.ToString();
                var numTik = dataGridViewResult.Rows[1].Cells[4].Value.ToString();
                var q2     = dataGridViewResult.Rows[2].Cells[0].Value.ToString();
                var q3     = dataGridViewResult.Rows[3].Cells[0].Value.ToString();
                // var q4 = dataGridViewResult.Rows[4].Cells[1].Value.ToString();
                // var q5 = dataGridViewResult.Rows[5].Cells[1].Value.ToString();
                var wordApp = new Word.Application();
                wordApp.Visible = false;

                try
                {
                    var worddocument = wordApp.Documents.Open(FileName);
                    var range        = worddocument.Content;
                    range.Copy();/*
                                  * Object start = Type.Missing;
                                  * Object end = Type.Missing;
                                  * Word.Range rng = worddocument.Range(ref start, ref end);
                                  * rng.Select();
                                  * string quantity = worddocument.Characters.Count.ToString();// общие количество символов в документе
                                  * MessageBox.Show("Characters:"  + worddocument.Characters.Count.ToString());
                                  *
                                  *
                                  * Object unit = Word.WdUnits.wdCharacter;
                                  * Object count = quantity;
                                  * rng.MoveStart(ref unit, ref count);
                                  */
                    //string bookmarkName = "T";
                    //object rStart = 0;
                    //object rEnd = worddocument.Content.End;
                    //Word.Document worddocument2 = wordApp.ActiveDocument;
                    //worddocument2.Bookmarks.Add("{T}", rEnd);

                    //Word.Bookmark bookm =
                    //worddocument.Range(ref rStart, ref rEnd).InsertAfter("Новая строка");



                    ReplaceWordStop("{namespec}", namespec, worddocument);
                    //ReplaceWordSHAB("{T}", _table, worddocument);
                    //ReplaceBookmarkText(worddocument, ref "T", textBox1.Text);
                    ReplaceWordStop("{numTik}", numTik, worddocument);
                    ReplaceWordStop("{q1}", q1, worddocument);
                    ReplaceWordStop("{q2}", q2, worddocument);
                    ReplaceWordStop("{q3}", q3, worddocument);
                    //ReplaceWordStop("{q4}", q4, worddocument);
                    //ReplaceWordStop("{q5}", q5, worddocument);
                    //wordApp.Selection.Bookmarks
                    Object     start = Type.Missing;
                    Object     end   = Type.Missing;
                    Word.Range rng   = worddocument.Range(ref start, ref end);
                    rng.Select();
                    string quantity = worddocument.Characters.Count.ToString();// общие количество символов в документе
                    MessageBox.Show("Characters:" + worddocument.Characters.Count.ToString());


                    Object unit  = Word.WdUnits.wdCharacter;
                    Object count = quantity;
                    rng.MoveStart(ref unit, ref count);
                    rng.Paste();
                    worddocument.SaveAs(@"D:\ticket.docx");

                    wordApp.Visible = true;
                }
                catch
                {
                    MessageBox.Show("ошибка печати");
                }
            }
            catch
            {
                MessageBox.Show("oh");
            }
        }