locked
Formatting a line (starting at a particular column) RRS feed

  • Question

  • Ok, I'm sure there has to be a way to do this otherwise there wasn't a lot of thought put into this functionality.  (quick background) I'm used to writing apps for documents in PDF's and the control that goes with that.  Is there a way with VB.net and word to specify the column I want my text to start at on a line.  For example I want "PO Box 1" to start at Col 1 I would then want "Client Number 1234" to start on the same line but at col 156.  Really what I'm trying to do is avoid drifting on the page of which is quite simple with a PDF as you just specify I want this element to start at pos x,y and and at pos x,y and there you go.  I understand word is a bit different, but I would think you should still be able to function within the confines of what is there, i.e. specifying the col to start at for the current line you are on.  Thanks in advance again!
    Tuesday, April 19, 2011 12:32 PM

Answers

  • Hi temlehdrol

    Word simply was not designed to work in this manner. It's a word processor, not a page layout tool, so some things simply aren't available in Word (and some things Word can do aren't available in layout software).

    Since the days proportional fonts were introduced, it makes no sense trying to work with columns in the sense you're using the term. Each letter uses a different amount of horizontal space, which makes the text nicer to look at and easier to read. But it doesn't let you put things "in columns".

    Word does have the concept of the Advance field (http://office.microsoft.com/en-us/help/field-codes-advance-field-HP010260100.aspx?CTT=5&origin=HA010100426). This is not recommended; I'd never use it unless there were absolutely no other way to achieve what I want. But it does come closest to what you envision.

    Given what you say you need, my inclination would be to set TAB stops where I need text to be "indented" relative to other text. And I'd use a TAB character (ANSI 9; "\t" in C#) to "push" the text to the tab stop. The article I link to above doesn't mention tabs, but you can get to them via the Paragraph box it does mention.

    another approach to achieve what you describe, which is useful if there are multiple lines that need to align "next to each other" is to insert a table and set the table columns for the positions you want to use.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by temlehdrol Thursday, April 21, 2011 5:00 PM
    Thursday, April 21, 2011 4:11 PM
  • <<Oh so if lets say tab stop 6 is where I need to be, even if the initial string would bring to to say 5 3/4 I'd still be good. >>

    Yes :-) But it would probably be a good idea for you to test this a bit as an end-user, just to get a feel for it. That makes programming Word a bit more understandable than if you're working from a purely theoretical point-of-view. The object model was originally designed to mimic the user's actions. Since the introduction of VBA it's been abstracted a bit more, but it's still very user-action-oriented.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by temlehdrol Thursday, April 21, 2011 7:13 PM
    Thursday, April 21, 2011 7:08 PM

All replies

  • ok here is some code as this may help some...
    ElseIf c = "b" Then
     boldState = Not boldState
     wrdSelection.Font.Bold = boldState 'wdToggle
     strline1 = strLine.Substring(2, strLine.LastIndexOf("%") - 2)
     strLine2 = strLine.Substring(strLine.LastIndexOf("%") + 2)
     wrdSelection.InsertAfter(strline1)
     boldState = Not boldState
     wrdSelection.Font.Bold = boldState
     wrdSelection.InsertAfter(strLine2 + Chr(11))
     bolSkipLine = True
    
    what I would like is for strLine2 to start at Col 156 on the current line.
    Tuesday, April 19, 2011 1:46 PM
  • Hi temlehdorl,

     

    Thanks for posting in the MSDN Forum.

     

    Would you clarify where you application will run? Is it running on Word or running on PDF?

     

    If you application is running on Word, I would recommend you handle your issue via Paragraph Interface. You can set the begin position via CharacterUnitFirstLineIndent property.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, April 21, 2011 6:39 AM
  • I'm sorry I thought I was clear, this is a vb.net 2010 application that is creating formatted letters in word from a text file.

    I'll try to explain what I mean as perhaps I wasn't as clear as I should have been.  Lets say I'm trying to write the following line to the word doc:
    PO BOX 12345                                                         Client Number: 56789

    P in PO Box starts at Col 1, however I want C in Client to always start at col 156 and not be dependent on the number of spaces in the string between 12345 and Client to determine the starting position of Client like it is now.

    Does this make more sense and clarify what I was talking about? 

    Thursday, April 21, 2011 12:43 PM
  • Hi temlehdrol

    Word simply was not designed to work in this manner. It's a word processor, not a page layout tool, so some things simply aren't available in Word (and some things Word can do aren't available in layout software).

    Since the days proportional fonts were introduced, it makes no sense trying to work with columns in the sense you're using the term. Each letter uses a different amount of horizontal space, which makes the text nicer to look at and easier to read. But it doesn't let you put things "in columns".

    Word does have the concept of the Advance field (http://office.microsoft.com/en-us/help/field-codes-advance-field-HP010260100.aspx?CTT=5&origin=HA010100426). This is not recommended; I'd never use it unless there were absolutely no other way to achieve what I want. But it does come closest to what you envision.

    Given what you say you need, my inclination would be to set TAB stops where I need text to be "indented" relative to other text. And I'd use a TAB character (ANSI 9; "\t" in C#) to "push" the text to the tab stop. The article I link to above doesn't mention tabs, but you can get to them via the Paragraph box it does mention.

    another approach to achieve what you describe, which is useful if there are multiple lines that need to align "next to each other" is to insert a table and set the table columns for the positions you want to use.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by temlehdrol Thursday, April 21, 2011 5:00 PM
    Thursday, April 21, 2011 4:11 PM
  • I was really hoping I wasn't right, but you have just let me know that I was. 

    I'm pretty sure what I want to do isn't going to work with tab stops as depending on the length of the first string of characters that would change the number of tabs to get to where I need to be, so one instance might be 4 where the next letter might need 5.  I guess I'll have to deal with drifting text for now until I can find a better way of doing this.  Perhaps do something with string lengths and what not to determine the number of spaces to line up the text.

    Thursday, April 21, 2011 5:00 PM
  • I'm pretty sure what I want to do isn't going to work with tab stops as depending on the length of the first string of characters that would change the number of tabs to get to where I need to be, so one instance might be 4 where the next letter might need 5.  I guess I'll have to deal with drifting text for now until I can find a better way of doing this.  Perhaps do something with string lengths and what not to determine the number of spaces to line up the text.


    Ah, but you haven't worked with Tab stops in Word, then :-)

    When you set a TAB stop in Word, all default stops up to that point are removed. So the number of tab stops are knownable from "column" to "column". All you need to do is make sure the first block of text can't be so long that it runs into that tab stop. If that could be the case then what you need is a table, as that will force the blocks of text to break to new lines before they reach the next column.


    Cindy Meister, VSTO/Word MVP
    Thursday, April 21, 2011 5:03 PM
  • Oh so if lets say tab stop 6 is where I need to be, even if the initial string would bring to to say 5 3/4 I'd still be good. 

    And yes I have had very limited exp using word for anything other than physically typing a letter.  However I have to update some software from VB6 to 2010 and it uses word to create the documents and they don't want to use PDF's so I'm stuck in this situtation.

    Thursday, April 21, 2011 5:12 PM
  • <<Oh so if lets say tab stop 6 is where I need to be, even if the initial string would bring to to say 5 3/4 I'd still be good. >>

    Yes :-) But it would probably be a good idea for you to test this a bit as an end-user, just to get a feel for it. That makes programming Word a bit more understandable than if you're working from a purely theoretical point-of-view. The object model was originally designed to mimic the user's actions. Since the introduction of VBA it's been abstracted a bit more, but it's still very user-action-oriented.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by temlehdrol Thursday, April 21, 2011 7:13 PM
    Thursday, April 21, 2011 7:08 PM
  • Thanks, now to figure out how to anchor drawn lines (but that's another issue/post) :-) 

    Thank you again, you've been most helpful as usual in these matters.

    Thursday, April 21, 2011 7:14 PM