複製 Word 腳註 (Copy Word Footnotes)


問題描述

複製 Word 腳註 (Copy Word Footnotes)

I've two word documents. Both documents are equals except for footnotes (just one doc contains notes). I need to copy that notes form one document to another one on the same position. I'm using Microsoft.Office.Interop.Word COM reference.

Any idea?

‑‑‑‑‑

參考解法

方法 1:

Well, I found a solution by myself:

foreach (Microsoft.Office.Interop.Word.Footnote x in doc.Footnotes)
{
    //MessageBox.Show(x.Range.Text);

    object selStart = x.Range.Start;
    object selEnd = x.Range.End;
    object missing = Type.Missing;
    object footnote = x.Range.Text;
    Range range = doc2.Range(ref selStart, ref selEnd);
    doc2.Footnotes.Add(range, ref missing, x.Range.Text);
}

方法 2:

 public static void FindFootnoteInDocument(Word.Document doc)
        {
     foreach (Microsoft.Office.Interop.Word.Footnote paragraph in doc.Footnotes)
                {
                    paragraph.Range.Select();
                    Microsoft.Office.Interop.Word.Style style1 = paragraph.Application.Selection.get_Style() as Microsoft.Office.Interop.Word.Style;
                    string styleName1;
                    if (style1 != null)
                        styleName1 = style1.NameLocal;
                    else
                        styleName1 = "Footnote Text";
                    int range = paragraph.Range.Start;
                    Word.Range ty = APIWrapper.APIUtility.GetWordRange(doc.Name, paragraph.Range.Start, paragraph.Range.End);
                    object unit = Word.WdUnits.wdWord;
                    object count = 1;
                    object extend = Word.WdMovementType.wdMove;
                    int t = paragraph.Application.Selection.Range.Move(ref unit, ref count);
                    string text = paragraph.Range.Text;
                    if (styleName1 == "Footnote Text")
                    {
                        Word.Selection currentSelection = doc.Application.Selection;
                        currentSelection.Find.ClearFormatting();
                        object style = "Footnote Text";
                        currentSelection.Find.set_Style(ref style);
                        var _with1 = currentSelection.Find;
                        _with1.Text = "";
                        _with1.Replacement.Text = "";
                        _with1.Forward = true;
                        _with1.Wrap = Word.WdFindWrap.wdFindContinue;
                        _with1.Format = true;
                        _with1.MatchCase = false;
                        _with1.MatchWholeWord = false;
                        _with1.MatchWildcards = false;
                        _with1.MatchSoundsLike = false;
                        _with1.MatchAllWordForms = false;
                        currentSelection.Cut();
                        object Count1 = 1;
                        object Unit1 = WdUnits.wdLine;
                        object Extend1 = WdMovementType.wdExtend;

                        doc.Application.Selection.MoveUp(ref Unit, ref Count, ref missing);
                        currentSelection.Find.Execute();//Execute()
                        while (currentSelection.Find.Found)
                        {
                           currentSelection.Range.Select();
                         string textvalue = currentSelection.Range.Text;
                         r2.Text = textvalue;
            MessageBox.Show("Sent lines :" + currentSelection.Text.ToString());
                          r2.set_Style(ref style);
                         break;  
                        }
                    }
                }
                break;
            }
        }

(by bitbitBurhan Saify)

參考文件

  1. Copy Word Footnotes (CC BY‑SA 3.0/4.0)

#ms-office #ms-word #C#






相關問題

什麼是 MS Office 對比算法? (What is the MS Office contrast algorithm?)

在 Office 2007 應用程序中使用 VBA? (Use VBA in Office 2007 Applications?)

複製 Word 腳註 (Copy Word Footnotes)

使用 WebApi 打開辦公文檔並使用 MS Office 保存回站點 (Open office documents with WebApi and save back to the site using MS Office)

用於確定 Windows 和 Office 版本的快速命令或批處理腳本 (quick command or batch script to determine Windows and Office version)

通過 Office 加載項將內容添加到 Outlook 電子郵件正文 (Adding content to an Outlook email body via an Office Add-In)

Word 2016 自動化生成“錯誤:80080005 服務器執行失敗” (Word 2016 automation generates "error: 80080005 Server execution failed")

Office 2016 查找激活日期 (Office 2016 find activation date)

Word 2007 文件啟動新窗口而不是顯示內聯 (Word 2007 files launching new window instead of displaying inline)

將 A1 公式和數組轉換為 L1C1 公式,反之亦然 (convert A1 formula and Array into L1C1 formula and vice-versa)

.NET C# Office Shared Add In WCF Service 引用異常 (.NET C# Office Shared Add In WCF Service reference exception)

從顯示“錯誤!超鏈接引用無效”的 MS Word 字段中恢復 URL (Recover URL from MS Word fields showing "Error! Hyperlink reference not valid")







留言討論