Click here to Skip to main content
16,016,501 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm using VB.net 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
Updated 21-Apr-11 7:02am
v2

1 solution

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.

C#
using System.Runtime.InteropServices;
using Word = Microsoft.Office.Interop.Word;

...
C#
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();

    // ----------Create new document----------
    wrdApp.Documents.Add();
    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.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!
 
Share this answer
 
Comments
Nish Nishant 21-Apr-11 17:41pm    
Good effort. My vote of 5!
R. Hoffmann 21-Apr-11 18:06pm    
Thanks!
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: http://www.codeproject.com/KB/cs/mswordcount.aspx. It calls on Word to provide document statistics, specifying that it wants the number of pages.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900