Exemple #1
0
        public void SetTextInCell(int Column, int Row, string strText, int indTable = -1, string strDefault = "", bool bAdd = false)
        {
            if (strText == "")
            {
                strText = strDefault;
                if (strText == "")
                {
                    return;
                }
            }

            int top  = 1;
            int left = 1;

            //MSWord.Columns pCols = null;
            //MSWord.Rows pRows = null;
            MSWord.Cell      pCell = null;
            MSWord.Selection pSel  = null;

            GetTableReference(indTable);

            //pCols = m_pTable.Columns;
            //if (pCols == null) return;
            //pRows = m_pTable.Rows;
            //if (pRows == null) return;

            if (Column >= 0)
            {
                left = Column;
            }
            if (Row >= 0)
            {
                top = Row;
            }

            // проверяем правильность установки границ диапазона
            // получаем верхнюю левую ячейку диапазона
            pCell = m_pTable.Cell(top, left);
            if (pCell == null)
            {
                return;
            }

            if (!bAdd)
            {
                pCell.Range.Text = strText;
            }
            else
            {
                pCell.Select();
                pSel = m_pApp.Selection;
                pSel.EndKey();

                pSel.Text = strText;
                pSel.Collapse(0);
            }
        }
Exemple #2
0
        public void SetText(string strText, MSWord.WdParagraphAlignment hAlignment, FontParams font = null)
        {
            if (m_pSelection == null)
            {
                if (m_pDoc == null)
                {
                    m_pDoc = m_pApp.ActiveDocument;
                }
                if (m_pApp == null)
                {
                    return;
                }

                m_pDoc.Select();
                m_pSelection = m_pApp.Selection;
                if (m_pSelection == null)
                {
                    return;
                }
            }

            object varInfo = m_pSelection.get_Information(MSWord.WdInformation.wdWithInTable);

            if (Convert.ToBoolean(varInfo) == true)
            {
                m_pDoc.Select();
                m_pSelection = m_pApp.Selection;
                if (m_pSelection == null)
                {
                    return;
                }
                m_pSelection.Collapse(0);
            }

            MSWord.ParagraphFormat pf = m_pSelection.ParagraphFormat;
            pf.Alignment      = hAlignment;
            m_pSelection.Text = strText;
            if (font != null)
            {
                PutFont(font, m_pSelection);
            }

            m_pSelection.Collapse(0);
        }
Exemple #3
0
        //</Snippet26>


        //---------------------------------------------------------------------
        //<Snippet56>
        private void SelectionInsertText()
        {
            //<Snippet57>
            Word.Selection currentSelection = Application.Selection;
            //</Snippet57>

            // Store the user's current Overtype selection
            bool userOvertype = Application.Options.Overtype;

            // Make sure Overtype is turned off.
            //<Snippet58>
            if (Application.Options.Overtype)
            {
                Application.Options.Overtype = false;
            }
            //</Snippet58>

            //<Snippet59>
            // Test to see if selection is an insertion point.
            if (currentSelection.Type == Word.WdSelectionType.wdSelectionIP)
            {
                currentSelection.TypeText("Inserting at insertion point. ");
                currentSelection.TypeParagraph();
            }
            //</Snippet59>
            //<Snippet60>
            else
            if (currentSelection.Type == Word.WdSelectionType.wdSelectionNormal)
            {
                // Move to start of selection.
                if (Application.Options.ReplaceSelection)
                {
                    object direction = Word.WdCollapseDirection.wdCollapseStart;
                    currentSelection.Collapse(ref direction);
                }
                currentSelection.TypeText("Inserting before a text block. ");
                currentSelection.TypeParagraph();
            }
            //</Snippet60>
            //<Snippet61>
            else
            {
                // Do nothing.
            }
            //</Snippet61>

            // Restore the user's Overtype selection
            Application.Options.Overtype = userOvertype;
        }
Exemple #4
0
        private void Button1_Click(object sender, EventArgs e)
        {
            //Insert text in a range
            Word.Range rng = this.Application.ActiveDocument.Range(0, 0);
            rng.Text = "New Text";


            //Select the Range object, which has expanded from one character to the length of the inserted text.
            rng.Select();


            // Replace text in a Range
            Word.Range rng = this.Application.ActiveDocument.Range(0, 12);
            rng.Text = "New Text";


            // To insert text using the TypeText method
            //1. Declare a selection object variable
            Word.Selection currentSelection = Application.Selection;

            // Turn off the overtype if it is turn on
            if (Application.Options.Overtype)
            {
                Application.Options.Overtype = false;
            }
            // Test to see if selection is an insertion point.
            if (currentSelection.Type == Word.WdSelectionType.wdSelectionIP)
            {
                currentSelection.TypeText("Inserting at insertion point. ");
                currentSelection.TypeParagraph();
            }
            else
            if (currentSelection.Type == Word.WdSelectionType.wdSelectionNormal)
            {
                // Move to start of selection.
                if (Application.Options.ReplaceSelection)
                {
                    object direction = Word.WdCollapseDirection.wdCollapseStart;
                    currentSelection.Collapse(ref direction);
                }
                currentSelection.TypeText("Inserting before a text block. ");
                currentSelection.TypeParagraph();
            }
            else
            {
                // Do nothing.
            }
        }
Exemple #5
0
        public void sayWord(string word)
        {
            //Get current document
            if (Application.Documents.Count >= 1)
            {
                Word.Document doc = Application.ActiveDocument;
                //Get current user selection, if none, the selection is the cursor point
                Word.Selection currentSelection = Application.Selection;
                //Get current user overtype policy. Usually would be false, but could be true.
                //We need it to be false. We store it so that we can restore it at the end.
                bool userOvertype = Application.Options.Overtype;
                if (userOvertype)
                {
                    Application.Options.Overtype = false;
                }
                try
                {
                    //Now we test if the cursor is at insertion mode (no selection):
                    if (currentSelection.Type == Word.WdSelectionType.wdSelectionIP)
                    {
                        //If it is, then insert the text.
                        currentSelection.TypeText(word);
                    }
                    else if (currentSelection.Type == Word.WdSelectionType.wdSelectionNormal)
                    {
                        //The user selected a piece of text.
                        //Now we need to know if the policy on write over selected text is to replace it.
                        if (Application.Options.ReplaceSelection)
                        {
                            //If it is, "collapse" (delete) the selection (and set the cursor at the beggining)
                            object direction = Word.WdCollapseDirection.wdCollapseStart;
                            currentSelection.Collapse(ref direction);
                        }
                        //Whatever happened with the selection, insert the text
                        currentSelection.TypeText(word);
                    }
                    else
                    {
                    }        //Do nothing
                } catch (Exception e)
                {
                }

                //Restore overtype
                Application.Options.Overtype = userOvertype;
            }
        }
Exemple #6
0
        public void InsertPicture(string strFileName, MSWord.WdParagraphAlignment hAlign, string bookmark)
        {
            if (bookmark == "")
            {
                if (m_pDoc == null)
                {
                    m_pDoc = m_pApp.ActiveDocument;
                }
                if (m_pDoc == null)
                {
                    return;
                }

                m_pDoc.Select();
                m_pSelection = m_pApp.Selection;
                if (m_pSelection == null)
                {
                    return;
                }

                m_pSelection.Collapse(0);
            }
            else
            {
                m_pDoc.Select();
                m_pSelection = m_pApp.Selection;
                if (m_pSelection == null)
                {
                    return;
                }

                m_pSelection.GoTo(-1, Type.Missing, Type.Missing, bookmark);
            }

            MSWord.ParagraphFormat pParFormat = m_pSelection.ParagraphFormat;
            if (pParFormat == null)
            {
                return;
            }
            pParFormat.Alignment = hAlign;
            MSWord.InlineShapes shapes = m_pSelection.InlineShapes;
            if (shapes == null)
            {
                return;
            }
            shapes.AddPicture(strFileName, false, true);
        }
Exemple #7
0
        public void InsertRowsInTable(int indTable, int nCount)
        {
            if (nCount <= 0)
            {
                return;
            }

            if (m_pApp != null)
            {
                if (m_pDoc == null)
                {
                    m_pDoc = m_pApp.ActiveDocument;
                }
                if (m_pDoc == null)
                {
                    return;
                }

                MSWord.Tables tables = m_pDoc.Tables;
                if (tables.Count == 0)
                {
                    return;
                }

                m_pTable = tables[indTable];
                if (m_pTable == null)
                {
                    return;
                }

                m_pTable.Select();

                MSWord.Selection pSel = m_pApp.Selection;
                pSel.InsertRowsBelow(nCount);

                m_pDoc.Select();
                m_pSelection = m_pApp.Selection;
                if (m_pSelection == null)
                {
                    return;
                }
                m_pSelection.Collapse(0);
            }
        }
Exemple #8
0
        public void InsertText(string text)
        {
            Word.Selection currentSelection = Application.Selection;

            // Store the user's current Overtype selection
            bool userOvertype = Application.Options.Overtype;

            // Make sure Overtype is turned off.
            if (Application.Options.Overtype)
            {
                Application.Options.Overtype = false;
            }

            // Test to see if selection is an insertion point.
            if (currentSelection.Type == Word.WdSelectionType.wdSelectionIP)
            {
                currentSelection.TypeText(text);
                currentSelection.TypeText(" ");
            }
            else
            if (currentSelection.Type == Word.WdSelectionType.wdSelectionNormal)
            {
                // Move to start of selection.
                if (Application.Options.ReplaceSelection)
                {
                    object direction = Word.WdCollapseDirection.wdCollapseStart;
                    currentSelection.Collapse(ref direction);
                }
                currentSelection.TypeText(text);
                currentSelection.TypeText(" ");
            }
            else
            {
                // Do nothing.
            }

            // Restore the user's Overtype selection
            Application.Options.Overtype = userOvertype;
        }
Exemple #9
0
        /// <summary>
        /// Gets a list of all marked ranges in the specified range. The current selection is updated to the end of the range to scan.
        /// </summary>
        /// <param name="rangeToScan">The Range to scan.</param>
        /// <param name="ShadingColor">The color for redaction marks in the document.</param>
        /// <param name="mergeAdjacent">True to merge adjacent ranges with identical formatting, False otherwise.</param>
        /// <returns>A List of RangeDataEx objects containing each marked subrange.</returns>
        internal static List <RangeDataEx> GetAllMarkedRanges(Word.Range rangeToScan, Word.WdColor ShadingColor, bool mergeAdjacent)
        {
            object Missing             = Type.Missing;
            object CollapseStart       = Word.WdCollapseDirection.wdCollapseStart;
            object CharacterFormatting = Word.WdUnits.wdCharacterFormatting;

            int LastPosition;
            int OriginalPosition;
            List <RangeDataEx> Ranges             = new List <RangeDataEx>();
            List <RangeDataEx> RangesToDelete     = new List <RangeDataEx>();
            List <RangeDataEx> ConcatenatedRanges = new List <RangeDataEx>();

            //we don't redact comments - if that's where we are, return
            if (rangeToScan.StoryType == Word.WdStoryType.wdCommentsStory)
            {
                return(ConcatenatedRanges);
            }

            //move the selection to the beginning of the requested range
            rangeToScan.Select();

            Word.Selection CurrentSelection = rangeToScan.Application.Selection;
            CurrentSelection.Collapse(ref CollapseStart);
            OriginalPosition = CurrentSelection.Start;

            //scan for distinct ranges of formatting
            do
            {
                //update LastPosition
                LastPosition = CurrentSelection.Start;

                //move to the next position
                CurrentSelection.Move(ref CharacterFormatting, ref Missing);

                //BUG 3913: if we detect that .Move has moved us out of the scan range (which appears to happen because of a Word bug)
                // break out and don't save the current range
                if (CurrentSelection.Start < OriginalPosition)
                {
                    CurrentSelection.End = rangeToScan.End;
                    break;
                }

                //store that range
                if (CurrentSelection.Start != LastPosition && rangeToScan.End != LastPosition)
                {
                    if (mergeAdjacent)
                    {
                        Ranges.Add(new RangeDataEx(LastPosition, CurrentSelection.Start < rangeToScan.End ? CurrentSelection.Start : rangeToScan.End, new RangeDataEx())); //since we're going to merge, don't fetch the extra properties
                    }
                    else
                    {
                        Word.Font CurrentFont = CurrentSelection.Font;
                        Ranges.Add(new RangeDataEx(LastPosition, CurrentSelection.Start < rangeToScan.End ? CurrentSelection.Start : rangeToScan.End, CurrentFont.Name, CurrentFont.Size, CurrentFont.Bold, CurrentFont.Italic, CurrentSelection.OMaths.Count > 0));
                    }
                }
            }while (CurrentSelection.End <= rangeToScan.End && CurrentSelection.End > LastPosition);

            if (CurrentSelection.End != rangeToScan.End)
            {
                if (mergeAdjacent)
                {
                    Ranges.Add(new RangeDataEx(CurrentSelection.End, rangeToScan.End, new RangeDataEx())); //since we're going to merge, don't fetch the extra properties
                }
                else
                {
                    Word.Font CurrentFont = CurrentSelection.Font;
                    Ranges.Add(new RangeDataEx(CurrentSelection.End, rangeToScan.End, CurrentFont.Name, CurrentFont.Size, CurrentFont.Bold, CurrentFont.Italic, CurrentSelection.OMaths.Count > 0));
                }
            }

            //go through those ranges and check if they are marked
            foreach (RangeDataEx UniqueRange in Ranges)
            {
                rangeToScan.Start = UniqueRange.Start;
                rangeToScan.End   = UniqueRange.End;

                //remove the range from the list
                if (!IsMarkedRange(rangeToScan, ShadingColor))
                {
                    RangesToDelete.Add(UniqueRange);
                }
            }

            //clean out the list
            foreach (RangeDataEx RangeToDelete in RangesToDelete)
            {
                Ranges.Remove(RangeToDelete);
            }
            RangesToDelete.Clear();

            //concatenate ranges that are next to each other
            int?Start = null;
            int?End   = null;

            for (int i = 0; i < Ranges.Count; i++)
            {
                //set start and end points
                if (Start == null)
                {
                    Start = Ranges[i].Start;
                }
                if (End == null)
                {
                    End = Ranges[i].End;
                }

                if ((i + 1) < Ranges.Count && (mergeAdjacent || (Ranges[i].InMath && Ranges[i + 1].InMath) || Ranges[i].IdenticalTo(Ranges[i + 1])) && End == Ranges[i + 1].Start)
                {
                    End = null;
                }
                else
                {
                    ConcatenatedRanges.Add(new RangeDataEx((int)Start, (int)End, Ranges[i]));
                    Start = End = null;
                }
            }

            //return the marked ranges
            return(ConcatenatedRanges);
        }
Exemple #10
0
        public void ConvertToTable(string strText, int row_cnt = -1, int col_cnt = -1, bool bSetBorders = true, string bookmark = "")
        {
            string strReturn  = "" + (char)10;
            bool   bUseReturn = false;

            if (strText.IndexOf(strReturn) >= 0)
            {
                bUseReturn = true;
            }

            strText = strText.Replace(strReturn, "#enter#");

            if (bookmark == "")
            {
                if (m_pDoc == null)
                {
                    m_pDoc = m_pApp.ActiveDocument;
                }
                if (m_pDoc == null)
                {
                    return;
                }

                m_pDoc.Select();
                m_pSelection = m_pApp.Selection;
                if (m_pSelection == null)
                {
                    return;
                }

                m_pSelection.Collapse(0);

                object varInfo = m_pSelection.get_Information(MSWord.WdInformation.wdWithInTable);
                if (Convert.ToBoolean(varInfo) == true)
                {
                    m_pDoc.Select();
                    m_pSelection = m_pApp.Selection;
                    if (m_pSelection == null)
                    {
                        return;
                    }
                    m_pSelection.Collapse(0);
                }
            }
            else
            {
                m_pSelection.GoTo(-1, Type.Missing, Type.Missing, bookmark);
            };

            m_pSelection.Font.Bold = 0;
            m_pSelection.Text      = strText;

            m_pSelection.Paragraphs.SpaceAfter      = 0;
            m_pSelection.Paragraphs.SpaceBefore     = 0;
            m_pSelection.Paragraphs.LineSpacingRule = MSWord.WdLineSpacing.wdLineSpaceSingle;


            if (row_cnt < 0 && col_cnt < 0)
            {
                m_pTable = m_pSelection.ConvertToTable(1, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                       Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                       Type.Missing, 0, 0);
            }
            else
            {
                m_pTable = m_pSelection.ConvertToTable(1, row_cnt, col_cnt, 0, 0,
                                                       Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                       Type.Missing, 0, 0);
            }

            // выравниваем талицу (не содержимое!!!)
            if (m_pTable == null)
            {
                return;
            }

            MSWord.Rows pRows = m_pSelection.Rows;
            pRows.AllowBreakAcrossPages = 0;

            m_pSelection.Collapse(0);

            MSWord.Borders pBorders = m_pTable.Borders;
            if (pBorders == null)
            {
                return;
            }
            if (bSetBorders)
            {
                pBorders.Enable = 1;
            }
            else
            {
                pBorders.Enable = 0;
            }

            if (bUseReturn)
            {
                m_pTable.Select();
                MSWord.Selection sel = m_pApp.Selection;

                MSWord.Find find = sel.Find;
                find.ClearFormatting();
                MSWord.Replacement replace = find.Replacement;
                replace.ClearFormatting();
                find.Text              = "#enter#";
                replace.Text           = "^p";
                find.MatchCase         = false;
                find.MatchWholeWord    = false;
                find.MatchWildcards    = false;
                find.MatchSoundsLike   = false;
                find.MatchAllWordForms = false;
                find.Wrap              = MSWord.WdFindWrap.wdFindContinue;
                MSWord.WdReplace repl = MSWord.WdReplace.wdReplaceAll;
                find.Execute(find.Text, find.MatchCase, find.MatchWholeWord, find.MatchWildcards,
                             find.MatchSoundsLike, find.MatchAllWordForms, Type.Missing, find.Wrap, Type.Missing, replace.Text, repl);
            }
        }
        internal void splitTableByEachRowTitleed字源圖片()
        {//將字源圖片原來的總表,分割成一個字源一個表格。且都有標題。即每表格有二列
            //此表格下面再插入一個新的表格,以便置入字源論述及字源圖片和靜態筆順20210428
            if (wdApp.Documents.Count > 0)
            {
                d = wdApp.ActiveDocument;
                if (d.Name != docName)
                {
                    foreach (winWord.Document item in wdApp.Documents)
                    {
                        if (item.Name == docName)
                        {
                            item.Activate(); d = item; break;
                        }
                    }
                }

                if (d.Name != docName)
                {
                    DocOps doc = new DocOps();
                    d = doc.openDoc(DocOps.getDocFullname());
                    if (d == null)
                    {
                        return;
                    }
                }
            }
            else
            {
                DocOps doc = new DocOps();
                d = doc.openDoc(DocOps.getDocFullname());
                if (d == null)
                {
                    return;
                }
            }
            //放在指定位置以開始
            d.Tables[1].Cell(3, 1).Range.Characters[1].Select();
            winWord.Selection Selection = d.ActiveWindow.Selection;
            Selection.Collapse(winWord.WdCollapseDirection.wdCollapseStart);
            int r, s; winWord.Cell cel; winWord.Range rng;

            winWord.InlineShape inlsp; winWord.Table tb;
            //List<WinWord.InlineShape> inlsps = new List<WinWord.InlineShape>();
            winWord.Row rw; winWord.Range rngInlSp;
            const float picCellWidth = 122.7F, picCellHeight = 120.2F;

            r = 1;
            string wTitle = d.Tables[r].Cell(2, 1).Range.Characters[1].Text; //記下標頭字

            winWord.Range wTitleRng = Selection.Range;
            rng = Selection.Range;
            wdApp.ScreenUpdating = false;
            d.Tables[1].Rows.Add(); d.Tables[1].Rows.Add();//最後會留下一個表格再予刪除
            int picsCount;

            //開始逐字分割為一表格:
            while (Selection.Information[winWord.WdInformation.
                                         wdWithInTable])
            {
                Selection.SplitTable();

                /* 表格置中都無效
                 * Selection.ParagraphFormat.Alignment = WinWord.WdParagraphAlignment.wdAlignParagraphCenter;
                 */
                rw = Selection.Document.Tables[1].Rows[1];
                rw.Range.Copy();//準備標題列
                Selection.Document.Tables[Selection.Document.
                                          Tables.Count].Range.Characters[1].Select();
                Selection.Collapse(winWord.WdCollapseDirection.
                                   wdCollapseStart);
                Selection.Paste();//貼上標題列
                wTitle = d.Tables[r].Cell(2, 1).Range.Characters[1].Text;
                if (d.Tables[r].Range.Characters[1].Previous() == null)
                {
                    d.Tables[r].Range.Characters[1].InsertBefore("\r");
                }
                wTitleRng = d.Tables[r].Range.Characters[1].Previous();
                wTitleRng.InsertParagraphBefore();
                wTitleRng      = wTitleRng.Paragraphs[1].Range;
                wTitleRng.Text = wTitle;//設定每個字頭的標題完成
                //wTitleRng.set_Style("標題 2");//此二式均可
                wTitleRng.set_Style(d.Styles["標題 2"]);
                Selection.Document.Tables[Selection.Document.Tables.Count]
                .Range.Characters[1].Select();
                Selection.Collapse(winWord.WdCollapseDirection.
                                   wdCollapseStart);
                Selection.MoveLeft();//分割完表格,就定位
                Selection.InsertParagraphAfter();
                Selection.Collapse(winWord.WdCollapseDirection.
                                   wdCollapseEnd);

                //插入表格,準備將圖片置入
                tb = Selection.Tables.Add(Selection.Range, 2, 2);
                tb.Range.Cells.VerticalAlignment   = winWord.WdCellVerticalAlignment.wdCellAlignVerticalCenter; //置中對齊
                tb.Range.ParagraphFormat.Alignment = winWord.WdParagraphAlignment.wdAlignParagraphLeft;         //向左對齊
                tb.Cell(1, 1).VerticalAlignment    = winWord.WdCellVerticalAlignment.wdCellAlignVerticalTop;
                tb.Columns[1].Width = 359.15F;
                tb.Columns[2].Width = picCellWidth;                                  //圖片儲存格的寬
                tb.Rows[2].Cells.Merge();                                            //第二列合併儲存格
                tb.Borders.InsideLineStyle =                                         //內框樣式
                                             winWord.WdLineStyle.wdLineStyleSingle;
                tb.Borders.OutsideLineStyle =                                        //外框樣式
                                              winWord.WdLineStyle.wdLineStyleSingle; //winWord.WdLineStyle.wdLineStyleDouble;
                tb.Rows[1].Height = picCellHeight;                                   //圖片儲存格的高
                tb.Rows[2].Height = 56;                                              //靜態筆順圖所放的儲存格,準備插入靜態筆順圖形
                if (DirFiles.PicsStaticStrokeOrderFolder != "")
                {
                    FindFileThruLINQ       fftL = new FindFileThruLINQ(DirFiles.PicsStaticStrokeOrderFolder);
                    IEnumerable <FileInfo> ieFi = fftL.findFiles(wTitle);
                    foreach (FileInfo item in ieFi)
                    {
                        PicsOps.InlineShapesAddPic(tb.Cell(2, 1)
                                                   .Range.Characters[1], item.FullName);
                    }
                }
                //表格置中
                //此無效:tb.Range.ParagraphFormat.Alignment = WinWord.WdParagraphAlignment.wdAlignParagraphCenter;
                //這才有效://http://www.wordbanter.com/showthread.php?t=110960
                tb.Rows.Alignment = winWord.WdRowAlignment.
                                    wdAlignRowCenter;
                tb.PreferredWidthType = winWord.WdPreferredWidthType.
                                        wdPreferredWidthPoints; //https://stackoverflow.com/questions/54159142/set-table-column-widths-in-word-macro-vba
                tb.PreferredWidth = (float)549.6378;            //固定表格寬度 Selection.Document.Tables[r].PreferredWidth;

                if (Selection.Document.Tables[r].Rows.Count == 1)
                {
                    cel = Selection.Document.Tables[r].Cell(1, 8);
                }
                else
                {
                    cel = Selection.Document.Tables[r].Cell(2, 8);
                }
                if (cel.Range.InlineShapes.Count > 0)
                {
                    ;
                }
                else
                {
                    if (Selection.Document.Tables[r].Rows.Count > 1)
                    {
                        cel = Selection.Document.Tables[r].Cell(2, 8);
                    }
                }

                s = Selection.Start;
                rng.SetRange(s, s);//記下圖片要貼上的位置
                picsCount = cel.Range.InlineShapes.Count;
                if (picsCount > 0)
                {
                    inlsp = cel.Range.InlineShapes[1];
                    inlsp.Select();
                    Selection.Cut();//剪下圖片,準備移動位置
                    #region 圖片若不貼新表格中,則如下:

                    /*                    s1 = Selection.Start;
                     * if (s1 > s)//兩種inlineshape圖形作用不同,故須分別處置
                     * {
                     *  while (rng.Information[winWord.WdInformation
                     *      .wdWithInTable])
                     *  {
                     *      s1--;
                     *      rng.SetRange(s1, s1);
                     *  }
                     * }
                     * else if (s1 < s)
                     * {
                     *  while (rng.Information[winWord.WdInformation.wdWithInTable])
                     *  {
                     *      s1++;
                     *      rng.SetRange(s1, s1);
                     *  }
                     *
                     * }
                     *
                     * rng.Select();
                     * Selection.Paste();//圖片貼到定位
                     *
                     * //foreach (WinWord.InlineShape insp in
                     * //              Selection.Previous().InlineShapes)
                     * //{
                     * //    inlsps.Add(insp);
                     * //    insp.Height += 181;
                     * //    insp.Width += 181;
                     * //}
                     *
                     * //docx檔中的圖形有二種,貼上後插入點位置也不同,故皆須分別處置
                     * if (Selection.Previous().InlineShapes.Count > 0)
                     * {
                     *  inlsp = Selection.Previous().InlineShapes[1];
                     *  inlsp.LockAspectRatio = MsoTriState.msoTrue;
                     *  inlsp.Height = 200;
                     * }//調整圖片大小
                     * else
                     * {//這種選取後周邊呈虛線形的圖形,與一般的圖形剪下貼上後插入點的落點會不同(會在貼上的圖形前),且用Selection.Next()也取不到它,須先將其選取 20210418
                     *  Selection.MoveRight(winWord.WdUnits.wdCharacter, 1, winWord.WdMovementType.wdExtend);
                     *  inlsp = Selection.InlineShapes[1];
                     *  inlsp.Height += 181;// = Selection.InlineShapes[1].Height + 181;
                     *  inlsp.Width += 181;//= Selection.InlineShapes[1].Height + 181;
                     *  Selection.Collapse(winWord.WdCollapseDirection.wdCollapseEnd);
                     * }
                     * //圖片置中
                     * //Selection.ParagraphFormat.Alignment = WinWord.WdParagraphAlignment.wdAlignParagraphCenter;
                     *
                     * inlsp.Select(); Selection.Cut();//剪下圖片貼入新插入的表格中
                     */
                    #endregion
                    tb.Cell(1, 2).Range.Characters[1].Select();
                    Selection.Paste();//貼上圖片,配合儲存格調整圖片大小
                    if (Selection.Previous().InlineShapes.Count > 0)
                    {
                        rngInlSp = Selection.Previous();
                    }
                    else //(Selection.Next().InlineShapes.Count > 0)
                    {
                        rngInlSp = Selection.Next();
                    }
                    rngInlSp.InlineShapes[1].LockAspectRatio = MsoTriState.msoTrue;
                    rngInlSp.InlineShapes[1].Height          = picCellHeight;
                    if (rngInlSp.InlineShapes[1].Width > picCellWidth)
                    {
                        rngInlSp.InlineShapes[1].Width = picCellWidth;
                    }
                    //以上圖片貼到定位且處理好其大小了
                    //離開圖片
                    Selection.MoveDown(Count: 2);
                    //Selection.Collapse(WinWord.WdCollapseDirection.wdCollapseEnd);
                    //與下一分割出來的表格空2行(段)--即與下一個漢字字源表分開來(距離拉開)
                    Selection.InsertParagraphAfter(); Selection.InsertParagraphAfter();
                }
                //以上有圖時的處理,以下缺圖者亦同然:
                Selection.Document.Tables[r].Columns[8].Cells.Delete(); //原來放置圖片的那欄刪除
                r += 2;                                                 //前面Tables.Add多插一表格,計數要再加1
                if (r > d.Tables.Count)
                {
                    break;
                }
                if (Selection.Document.Tables[r].Rows.Count > 3)//結束時,尚須修改。目前可以權且加幾空白列在最後一列後
                {
                    Selection.Document.Tables[r].Rows[3].Select();
                }
                else
                {
                    break;
                }
            }
            d.Tables[d.Tables.Count].Delete();
            wdApp.ScreenUpdating = true;
            SystemSounds.Beep.Play();//Beep
            //https://blog.kkbruce.net/2019/03/csharpformusicplay.html#.YHiXtqzivsQ
        }
Exemple #12
0
        //sel.MoveLeft(ref unit, ref count, ref extend);
        private void CollaseSelection(Word.Selection sel, Word.WdCollapseDirection direction = Word.WdCollapseDirection.wdCollapseEnd)
        {
            object collapseDirection = direction;

            sel.Collapse(ref collapseDirection);
        }