Click here to Skip to main content
13,148,427 members (60,725 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I'm using 2010 and word 2000. What I'm trrying to do is get the lines that I'm drawing to anchor to the current page that is being "worked on". As of right now I just have this portion of code being called with the first page and it's getting pushed to the last page with no text on it.
wrdDoc.Shapes.AddLine(intBeginX, intBegY, intEndX, intEndY).Select()

there is a anchor object according to the intillisense after the endY but it's not very intuitive. I tried the MSDN Library and that is even more useless, I've tried searching with google and no such luck so far.
All I want to do is get those stupid lines to be anchored on the current page when it is called. on a side note the text file that I'm working with to produce the letter does not have the lines in them, the lines are in a seperate file that is being called containing the coordinates.
This is the reason I like working with PDF's you can just tell it where you want it and not have to worry about drifting.
Thanks in advance!!!
Posted 21-Apr-11 5:09am
Updated 21-Apr-11 7:02am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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[^]:
    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.";
    wrdApp = new Word.ApplicationClass();
    // ----------Create new document----------
    Word.Document wrdDoc = wrdApp.ActiveDocument;
    // ----------Populate document----------
    // Page 1
    wrdDoc.Range(Start: 0, End: 0).Text = TEXT; // Insert text at start of document
    wrdDoc.Shapes.AddLine(50, 50, 100, 100, wrdDoc.Range(Start: 0, End: TEXT.Length - 1)); // Insert line anchored to first paragraph within specified range
    // Page break
    wrdDoc.Range(TEXT.Length+1, TEXT.Length+1).Select(); // Put selection at end of inserted text.
    wrdApp.Selection.InsertBreak(Word.WdBreakType.wdPageBreak); // Start a new page at current selection (i.e. after inserted text)
    // Page 2
    Word.Range startOfPage2 = wrdDoc.Range(Start: wrdApp.Selection.Start);
    startOfPage2.Text = TEXT; // Insert text at beginning of 2nd page
    wrdDoc.Shapes.AddLine(150, 50, 200, 100, startOfPage2); // Add shape on 2nd page, anchored to top of page
    // ----------Save document----------
    wrdDoc.SaveAs2(FileName: "Test.docx");
    wrdDoc = null;
    if (wrdApp != null)

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!
Nishant Sivakumar 21-Apr-11 17:41pm
Good effort. My vote of 5!
R. Hoffmann 21-Apr-11 18:06pm
MacRaider4 22-Apr-11 11:19am
Finally have some time to work on this, let me see how it is with 2000... looks good though
MacRaider4 22-Apr-11 11:35am
Ok the problem I'm seeing is I'm not going to know how many pages there are, so I would have to have a lot of variables set up and possibly many of which will be unused. have I mentioned I really hate word yet? I'll see what I can come up with, with what you have here though.
On a side note, with this method it did stay with the first page (only have it priting the lines once right now, figured I'd get the first page working before I work on the rest)
R. Hoffmann 22-Apr-11 12:20pm
To get the number of pages in a document, try the code in this article: It calls on Word to provide document statistics, specifying that it wants the number of pages.
MacRaider4 22-Apr-11 12:27pm
The word Doc I'm creating is from a txt file, one file has the text and another has the coordinates of the lines.
MacRaider4 22-Apr-11 13:38pm
It works... had to do some tweeking and I don't remember what it was off hand from what I had before, but it's now working. Thanks for the help I greatly appreciate it!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170924.2 | Last Updated 21 Apr 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100