-1

Hi I'm looking for any idea to make something like that:

Some Text---------------------------------------------------

This "------" I have to make on button click to the end of line.

My idea was to make it on tabstop with dashed leader, but it make problem because when im trying to use ^t inside text after this it makes dashes also. It can't be done just by typing dashes because second button would be to clear that dashing from whole document.

This is code that I make but like I said it is not good because that ^t at any other place of document is terrible

public void DashingSingleLane()
    {
        Word.Range rng = Globals.ThisAddIn.Application.Selection.Range;
        if (rng.Start == rng.End)
        {

            float pageWidth = classDocument.PageSetup.PageWidth - classDocument.PageSetup.LeftMargin - classDocument.PageSetup.RightMargin;


            foreach (Word.Paragraph singleParagraph in rng.Paragraphs)
            {
                if ((singleParagraph.Range.Text != "\r") && (singleParagraph.Range.Text.IndexOf("\t") == -1))
                {
                    singleParagraph.TabStops.Add(pageWidth, Word.WdTabAlignment.wdAlignTabRight, Word.WdTabLeader.wdTabLeaderDashes);
                    Globals.ThisAddIn.Application.Selection.EndKey(Microsoft.Office.Interop.Word.WdUnits.wdLine);
                    Globals.ThisAddIn.Application.Selection.TypeText("\t");

                }

            }
        }
    }

I found that to make it good I need also to put Tabstop with left align exacly at the end of sentence. Something like that

singleParagraph.TabStops.AddPosition_in_Point, Word.WdTabAlignment.wdAlignTabLeft, Word.WdTabLeader.wdTabLeaderDashes);

But I don't know how to get that position in point

I'm thinking about that problem from 2 days and I don't have any good idea. I hope somebody would have some good think.

Sascha
  • 1,210
  • 1
  • 17
  • 33
  • Before I can answer I need to understand how you're using the TAB character (\t) generally, in the document. AND I need to understand how you're handling formatting in the document. Would I be correct if I said you're basically "typing" the content from left-to-right and top-to-bottom using Selection.TypeText and that you're NOT using the Style object? – Cindy Meister Jan 05 '16 at 15:23
  • Probably yes I'm not sure if I'm really correctly understand what you mean but probably yes. – P.Bochenek Jan 07 '16 at 15:35
  • If you don't really understand the question then, yes, "probably yes" Most efficient and reliable in Word is to work with STYLES to apply formatting (including tab stops). You apply the style appropriate to the situation. Keep this in mind as we continue researching... – Cindy Meister Jan 07 '16 at 16:36
  • Next, I need to understand more about how this document is structured. You mention needing to "tab" otherwise, in the text: "but it make problem because when im trying to use ^t inside text after this it makes dashes also". We need to understand when and where what kind of Tab is required. Could you possibly add a diagram to your original question that illustrates blocks of text and Tabs with a short explanation? ADDITIONAL question: Could this structure be achieved using a TABLE? – Cindy Meister Jan 07 '16 at 16:40
  • What is that STYLES ? About structure it is normal text justified paragraph and user by click need to get tabstop in place of selection to the end of lane if nothing is on right side if it is it would be to the distance of text width on right side . It can't be done by table it have to be just text – P.Bochenek Jan 08 '16 at 17:18
  • http://scr.hu/95ow/vh23h http://scr.hu/95ow/f9ryo http://scr.hu/95ow/8vqif On screens I showed it on second screen is how it actual work and I need to know how to get this from thirdscreen but programmatically – P.Bochenek Jan 08 '16 at 17:19
  • The explanation helps. I don't see how to change screens in the link but I don't think I need that, now. IF I understand correctly you need to: evaluate whether there's following text yes/no. If no, tab stop at the right margin. If yes, calculate distance open to right margin and set tab stop at position that uses this distance (push following text to the right). Next line (paragraph) with no tab stop to begin with. QUESTION: Might the user want TWO sets of dashes in a line (paragraph)? If that can happen, you've got a problem... – Cindy Meister Jan 08 '16 at 18:20
  • It is 3x link to screen. For real it is all about put 1 tabstop on right side like in screen2 and it is done already. Problem is how to get point value of this what is on screen3 so I need to know how to get value of text width in points from left side to selection.start. – P.Bochenek Jan 11 '16 at 17:03
  • "It is 3x link to screen." I'm sorry, this means nothing to me. I see one picture when I click your link and no way to change what I see. – Cindy Meister Jan 11 '16 at 17:58

1 Answers1

0

In order to determine the horizontal position of the current selection (or a Range) you can use the Information property. This takes a member of the WdInformation enumeration that tells Word what value to return.

You can return the information as a value from the left side of the page wdHorizontalPositionRelativeToPage, or from the "text boundary", which would be the margin in "plain text" (as opposed to a table or some other construct) on a page with a single column - wdHorizontalPositionRelativeToTextBoundary.

So, for example:

//Position is returned in POINTS, which is what TabStop uses
float selPosition = Selection.get_Information(Word.WdInformation.wdHorizontalPositionRelativeToTextBoundary);
singleParagraph.TabStops.Add(selPosition, Word.WdTabAlignment.wdAlignTabRight, Word.WdTabLeader.wdTabLeaderDashes);
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43