Problem controlling location when using AddLine

J

Joe Ross

I am programmatically building a Word doc with C#. Everything is working
quite well; I've been pleasantly surprised so far.

I am now attempting to add a horitzontal line to the document at the
location of the cursor. I need to do this several times throughout the
document. I am currently using the following code:

object temp = wrdSelection.Range;
wrdDoc.Shapes.AddLine (0, 0, 500, 0, ref temp);

The 500 is abritrary...I'm having bigger problems.

Each time I call this, a line does get added to the document.
Unfortunately, it continuously gets pushed down to the bottom of the
document. If I call this code 5 times, at the end of the process I will end
up with 5 horizontal lines stacked on top of each other (they look like a
single line).

Any insight is appreciated.

Thanks
-joe
 
T

Tony Strazzeri

Hi Joe.

Your question got me playing.
Do you change the zero values in your code?
The determine where the line will be placed.

Now the playing. The following code directly in word VBA it should
help you to fix the problem.

I think this is neat. I haven't played with drawing lines since my
Apple2e days.

Dim wrddoc As Document
Set wrddoc = ActiveDocument
Set temp = Selection.Range
Dim BeginX, BeginY, endX, endY
For BeginY = 40 To 400 Step 100
For BeginX = 60 To 600 Step 100
wrddoc.Shapes.AddLine BeginX, BeginY, 500, 200, temp
Next
Next


Cheers
TonyS.
 
J

Joe Ross

Tony-

Thanks for the code snippet. I ran it and understand what's going on.

The problem I face now is that I want to put the line at the current
cursor/selection position. I've read about using Selection.Information to
retrieve x&y coordinate points. However, the interfaces I'm using in C# do
not appear to expose the Information property.

Any ideas? Or is there a better/different way to determine current
location?

Thanks
-joe
 
J

Jonathan West

Hi Joe,

Actually, I would suggest an entirely different approach to the problem.
Instead of inserting a line in the drawing layer, which won't be seen by the
user if the document is viewed in Normal view, I would recommend instead
that you add a border to the current paragraph. You can be sure that will
stay will the selected paragraph if the document is subsequently edited.
 
J

Joe Ross

Unfortunately, I am attempting to recreate a document which uses the lines.
You are correct that they do not appear in Normal view. I don't know if
that's intentional for our end users, so I need to keep it consistent.

Thanks for the suggestion though,
-joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top