問題描述
複製 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 bit、bit、Burhan Saify)