locked
adding a tab stop to make partial text left aligned and another part right aligned RRS feed

  • Question

  • There is a need for me to put one line paragraph right at the bottom of an unfinished table, "See footnote(s) at end of table." left aligned and "--continued" right aligned.  Check out a sample pdf at http://www.agcensus.usda.gov/Publications/2012/Full_Report/Volume_1,_Chapter_1_US/st99_1_068_068.pdf

    I have been using tab stop for that kind of situation but I am having problem with "--continued".  Because tab stop utilize actual inch or point to position that word, but I have no reliable way to calculate the actual with of the word "--continued".  Anyway to get around that?

    Regards,

    Kelvin

    Monday, April 11, 2016 12:43 PM

Answers

  • As I said, there is no need to calculate the string width of "--continued." The problem with your code is that you have omitted the margin widths and are instead taking into account the paragraph indents, which are irrelevant for the tab-stop positioning. You should be doing something like the following VBA code:

    Sub Demo()
    Dim sngStop As Single
    With ActiveDocument.Sections(1)
      With .PageSetup
        sngStop = .PageWidth - .LeftMargin - .RightMargin
      End With
      With .Range.Paragraphs(1)
        .TabStops.Add Position:=sngStop, Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces
        .Range.Text = "See footnote(s) at end of table." & vbTab & "--continued"
      End With
    End With
    End Sub

    where Sections(1) and Paragraphs(1) designate which Section and Paragraph you want to work with.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Kelvin Jin Friday, April 15, 2016 1:22 AM
    Wednesday, April 13, 2016 9:15 PM

All replies

  • Hi, Kelvin Jin

    This is the forum to discuss questions and feedback for Visual Studio Tools for Office (VSTO), I'll move your question to the Technet forum for Word IT Pro Discussions

    https://social.technet.microsoft.com/Forums/office/en-US/home?forum=word

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. 

    Thanks for your understanding.
    Tuesday, April 12, 2016 6:04 AM
  • While it is possible to calculate the width of a string, it's not clear to me why you need that. Surely all you need is a right-aligned tab-stop wherever you want the "--continued" string to end. With that in place, all you'd need between the "See footnote(s) at end of table." string and the "--continued" string is a single tab character to get the positioning right. Another option would be to use a two-cell table, with the "See footnote(s) at end of table." in the left cell and the "--continued" string in the right cell. With the table width set to 100% and the paragraph alignment for the right-hand cell set to 'right', the "--continued" string in the right cell would adjust position to match changing page dimensions.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, April 12, 2016 7:16 AM
  • Sorry, I should have stated that I need a clear and easy way to get the actual width of "--continued" in order for me to set a correct tab stop value in my C# program.

    My current problem is I can hard code the tab stop on that line, but when font face and font size changed, the text will not be aligned correctly.

    float currentPosition = dLocalRange.PageSetup.PageWidth - dParagraph.getRange().Paragraphs.RightIndent - Continued.Length(here I need actual width);

    object dAlignment = Microsoft.Office.Interop.Word.WdAlignmentTabAlignment.wdRight;
    object dLeader = Microsoft.Office.Interop.Word.WdTabLeader.wdTabLeaderSpaces;

    dLocalRange.Text = "See footnote(s) at end of table.\t" + Continued;

    dLocalRange.ParagraphFormat.TabStops.Add(currentPosition1, ref dAlignment, ref dLeader);

    I have been trying to go through MSDN utilize the fake text box but the value that I got is always wrong.  This is a truly VSTO question.  The answer that I received is for word processor only.  Please kindly move my question back to VSTO.  Thanks,

    Regards,

    Kelvin

    Wednesday, April 13, 2016 12:59 PM
  • As I said, there is no need to calculate the string width of "--continued." The problem with your code is that you have omitted the margin widths and are instead taking into account the paragraph indents, which are irrelevant for the tab-stop positioning. You should be doing something like the following VBA code:

    Sub Demo()
    Dim sngStop As Single
    With ActiveDocument.Sections(1)
      With .PageSetup
        sngStop = .PageWidth - .LeftMargin - .RightMargin
      End With
      With .Range.Paragraphs(1)
        .TabStops.Add Position:=sngStop, Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces
        .Range.Text = "See footnote(s) at end of table." & vbTab & "--continued"
      End With
    End With
    End Sub

    where Sections(1) and Paragraphs(1) designate which Section and Paragraph you want to work with.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Kelvin Jin Friday, April 15, 2016 1:22 AM
    Wednesday, April 13, 2016 9:15 PM
  • Thank you so much for the wonderful answer.  It solved my problem.

    There is one question which is not related to this, but has been in my mind for a while.  Is VSTO still the future of the office automation? or something else is on the way?  I have been searching for books, but the most of the return were those written for office 2007.  It seems VSTO is dying.  Learning tree said something about VSTO will not be the mainstream and they would not bother to offer any class on that subject.  Any information regarding that?

    Regards,

    Kelvin

    Friday, April 15, 2016 1:33 AM
  • Is VSTO still the future of the office automation? or something else is on the way?

    I have no knowledge of any of that. You'd have to ask someone else.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, April 15, 2016 1:38 AM
  • Thanks for the reply.

    Kelvin

    Friday, April 15, 2016 2:50 PM