Disclaimer: I'm using Office 2010 (I don't have 2000), with VS 2010 & .NET 4.0 (C#). My Word interop is rusty, so please don't interpret my techniques as best practices. Nevertheless, I hope this may be useful.
It would seem that the line that is created by wrdDoc.Shapes.AddLine is created on the page that contains the anchor. The anchor is simply a Range object representing a section of the document. The shape is anchored to the start of the first paragraph within this section, as per the MSDN page
here[
^]:
Anchor
Optional Object. A Range object that represents the text to which the label is bound. If Anchor is specified, the anchor is positioned at the beginning of the first paragraph in the anchoring range. If this argument is omitted, the anchoring range is selected automatically and the label is positioned relative to the top and left edges of the page.
Below is some quick and dirty code I hacked together, that creates a Word document consisting of two pages, each containing a little text and a line.
using System.Runtime.InteropServices;
using Word = Microsoft.Office.Interop.Word;
...
Word.ApplicationClass wrdApp = null;
const string TEXT =
"This is some random text.\n" +
"This is more random text.\n" +
"This is the final bit of random text.";
try
{
wrdApp = new Word.ApplicationClass();
wrdApp.Documents.Add();
Word.Document wrdDoc = wrdApp.ActiveDocument;
wrdDoc.Range(Start: 0, End: 0).Text = TEXT;
wrdDoc.Shapes.AddLine(50, 50, 100, 100, wrdDoc.Range(Start: 0, End: TEXT.Length - 1));
wrdDoc.Range(TEXT.Length+1, TEXT.Length+1).Select();
wrdApp.Selection.InsertBreak(Word.WdBreakType.wdPageBreak);
Word.Range startOfPage2 = wrdDoc.Range(Start: wrdApp.Selection.Start);
startOfPage2.Text = TEXT;
wrdDoc.Shapes.AddLine(150, 50, 200, 100, startOfPage2);
wrdDoc.SaveAs2(FileName: "Test.docx");
wrdDoc.Close();
wrdDoc = null;
}
finally
{
if (wrdApp != null)
{
wrdApp.Quit();
Marshal.FinalReleaseComObject(wrdApp);
}
}
So based on this, I'd recommend creating a Range object representing the page you want to add a line to, and then calling
wrdDoc.Shapes.AddLine
as you've been doing, but passing in the range object as your anchor, and that will hopefully insert the line on the correct page. The document's current Selection has nothing to do with the line placement as far as I could tell.
Hope this helps!