Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-28-2019, 03:46 AM
abc3132 abc3132 is offline Hyphen until the end of the row Windows 7 64bit Hyphen until the end of the row Office 2007
Novice
Hyphen until the end of the row
 
Join Date: Feb 2015
Posts: 24
abc3132 is on a distinguished road
Default Hyphen until the end of the row

Hi all.
It is to create a contract word file and to ensure that nothing is wrote until the end of the row; it is fill all the blank spaces automatically until the end with hyphens.


Thanks, Adrian
Reply With Quote
  #2  
Old 10-28-2019, 04:56 AM
gmayor's Avatar
gmayor gmayor is offline Hyphen until the end of the row Windows 10 Hyphen until the end of the row Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Automatically would be a stretch, but you can use a macro to create a tab at the right margin and add a tab with a dashed leader to fill the space. Put the cursor in the 'line' in question and run the macro
Code:
Sub TabToEndOfLine()

Dim lngWidth As Long, lngLM As Long, lngRM As Long
Dim oRng As Range
    
    With Selection.Sections(1).PageSetup
        lngLM = .LeftMargin
        lngRM = .RightMargin
        lngWidth = .PageWidth
    End With

    Set oRng = Selection.Paragraphs(1).Range
    oRng.End = oRng.End - 1
    With oRng.ParagraphFormat.TabStops
        .ClearAll
        .Add Position:=lngWidth - lngLM - lngRM, _
             Alignment:=wdAlignTabRight, _
             Leader:=2
    End With
    oRng.Collapse 0
    oRng.Text = Chr(32) & vbTab
    oRng.End = oRng.Paragraphs(1).Range.End + 1
    oRng.Collapse 0

    oRng.Select
lbl_Exit:
    Set oRng = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 10-28-2019, 05:23 AM
abc3132 abc3132 is offline Hyphen until the end of the row Windows 7 64bit Hyphen until the end of the row Office 2007
Novice
Hyphen until the end of the row
 
Join Date: Feb 2015
Posts: 24
abc3132 is on a distinguished road
Default Thanks Gmayor

Quote:
Originally Posted by gmayor View Post
Automatically would be a stretch, but you can use a macro to create a tab at the right margin and add a tab with a dashed leader to fill the space. Put the cursor in the 'line' in question and run the macro
Code:
Sub TabToEndOfLine()

Dim lngWidth As Long, lngLM As Long, lngRM As Long
Dim oRng As Range
    
    With Selection.Sections(1).PageSetup
        lngLM = .LeftMargin
        lngRM = .RightMargin
        lngWidth = .PageWidth
    End With

    Set oRng = Selection.Paragraphs(1).Range
    oRng.End = oRng.End - 1
    With oRng.ParagraphFormat.TabStops
        .ClearAll
        .Add Position:=lngWidth - lngLM - lngRM, _
             Alignment:=wdAlignTabRight, _
             Leader:=2
    End With
    oRng.Collapse 0
    oRng.Text = Chr(32) & vbTab
    oRng.End = oRng.Paragraphs(1).Range.End + 1
    oRng.Collapse 0

    oRng.Select
lbl_Exit:
    Set oRng = Nothing
    Exit Sub
End Sub
Thanks Gmayor. Brilliant! it works very well
Reply With Quote
  #4  
Old 10-28-2019, 11:41 AM
abc3132 abc3132 is offline Hyphen until the end of the row Windows 7 64bit Hyphen until the end of the row Office 2007
Novice
Hyphen until the end of the row
 
Join Date: Feb 2015
Posts: 24
abc3132 is on a distinguished road
Default

Just a quick question more. Your solution works very well as I have said but when I´m trying to run thorough several paragraphs the macro pick just one up. How I can make it run trough all of them?
Thanks
Reply With Quote
  #5  
Old 10-28-2019, 09:38 PM
gmayor's Avatar
gmayor gmayor is offline Hyphen until the end of the row Windows 10 Hyphen until the end of the row Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

It's a bit unpredictable without access to the document, but the following will loop through the paragraphs.

Code:
Sub TabToEndOfLine()

Dim lngWidth As Long, lngLM As Long, lngRM As Long
Dim oRng As Range
Dim oPara As Paragraph

    For Each oPara In ActiveDocument.Paragraphs
        If oPara.Range.Information(wdWithInTable) = False Then
            Set oRng = oPara.Range

            With oRng.Sections(1).PageSetup
                lngLM = .LeftMargin
                lngRM = .RightMargin
                lngWidth = .PageWidth
            End With

            oRng.End = oRng.End - 1
            With oRng.ParagraphFormat.TabStops
                .ClearAll
                .Add Position:=lngWidth - lngLM - lngRM, _
                     Alignment:=wdAlignTabRight, _
                     Leader:=2
            End With
            oRng.Collapse 0
            oRng.Text = Chr(32) & vbTab
        End If
        DoEvents
    Next oPara
lbl_Exit:
    Set oRng = Nothing
    Set oPara = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #6  
Old 10-29-2019, 07:38 AM
abc3132 abc3132 is offline Hyphen until the end of the row Windows 7 64bit Hyphen until the end of the row Office 2007
Novice
Hyphen until the end of the row
 
Join Date: Feb 2015
Posts: 24
abc3132 is on a distinguished road
Default

Thanks Graham. Just a comment I´ve noticed that the Macro does no work inside a table cell always in a Word document.
Thanks
Reply With Quote
  #7  
Old 10-29-2019, 03:12 PM
Guessed's Avatar
Guessed Guessed is offline Hyphen until the end of the row Windows 10 Hyphen until the end of the row Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Graham put in the code to not do anything in a table cell. Presumably this was because a table cell would imply a column width of something other than the page usable width.

Do you need it to work in a table as well?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 10-29-2019, 03:16 PM
abc3132 abc3132 is offline Hyphen until the end of the row Windows 7 64bit Hyphen until the end of the row Office 2007
Novice
Hyphen until the end of the row
 
Join Date: Feb 2015
Posts: 24
abc3132 is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
Graham put in the code to not do anything in a table cell. Presumably this was because a table cell would imply a column width of something other than the page usable width.

Do you need it to work in a table as well?
Thanks for your tiem Andrew. The idea is to use it in a table that is in a word document.
Cheers
Reply With Quote
  #9  
Old 10-29-2019, 07:40 PM
Guessed's Avatar
Guessed Guessed is offline Hyphen until the end of the row Windows 10 Hyphen until the end of the row Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

OK, this code adds the line to every paragraph including table cells (except for the last para in the last cell of a table)
Code:
Sub TabToEndOfLine()
  Dim lngWidth As Long, lngLM As Long, lngRM As Long
  Dim oRng As Range, lngColWidth As Long
  Dim oPara As Paragraph
  On Error Resume Next
  For Each oPara In ActiveDocument.Paragraphs
    Set oRng = oPara.Range
    If oPara.Range.Information(wdWithInTable) = False Then
      lngColWidth = oRng.Sections(1).PageSetup.TextColumns.Width
    Else ' in a table
      lngColWidth = oRng.Cells(1).Width
    End If
    With oRng.ParagraphFormat.TabStops
      .ClearAll
      .Add Position:=lngColWidth, Alignment:=wdAlignTabRight, Leader:=2
    End With
    oRng.End = oRng.End - 1
    oRng.InsertAfter Chr(32) & vbTab
    DoEvents
  Next oPara
lbl_Exit:
  Set oRng = Nothing
  Set oPara = Nothing
  Exit Sub
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #10  
Old 10-29-2019, 11:22 PM
gmayor's Avatar
gmayor gmayor is offline Hyphen until the end of the row Windows 10 Hyphen until the end of the row Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The original code was lifted from another process that puts the lines at the start and end of the paragraph for emphasising headings. As Andrew suggests the column width will do the job without the need to calculate the tab position, and can be adapted for use in tables. I would however suggest adding a trap for empty paragraphs, and of course the positioning variables are superfluous thus
Code:
Sub TabToEndOfLine()
Dim oRng As Range, lngColWidth As Long
Dim oPara As Paragraph
    On Error Resume Next
    For Each oPara In ActiveDocument.Paragraphs
        Set oRng = oPara.Range
        If oPara.Range.Information(wdWithInTable) = False Then
            lngColWidth = oRng.Sections(1).PageSetup.TextColumns.Width
        Else    ' in a table
            lngColWidth = oRng.Cells(1).Width
        End If
        If Len(oRng) > 1 Then
            With oRng.ParagraphFormat.TabStops
                .ClearAll
                .Add Position:=lngColWidth, Alignment:=wdAlignTabRight, Leader:=2
            End With
            oRng.End = oRng.End - 1
            oRng.InsertAfter Chr(32) & vbTab
            DoEvents
        End If
    Next oPara
lbl_Exit:
    Set oRng = Nothing
    Set oPara = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #11  
Old 10-30-2019, 01:05 AM
abc3132 abc3132 is offline Hyphen until the end of the row Windows 7 64bit Hyphen until the end of the row Office 2007
Novice
Hyphen until the end of the row
 
Join Date: Feb 2015
Posts: 24
abc3132 is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
OK, this code adds the line to every paragraph including table cells (except for the last para in the last cell of a table)
Code:
Sub TabToEndOfLine()
  Dim lngWidth As Long, lngLM As Long, lngRM As Long
  Dim oRng As Range, lngColWidth As Long
  Dim oPara As Paragraph
  On Error Resume Next
  For Each oPara In ActiveDocument.Paragraphs
    Set oRng = oPara.Range
    If oPara.Range.Information(wdWithInTable) = False Then
      lngColWidth = oRng.Sections(1).PageSetup.TextColumns.Width
    Else ' in a table
      lngColWidth = oRng.Cells(1).Width
    End If
    With oRng.ParagraphFormat.TabStops
      .ClearAll
      .Add Position:=lngColWidth, Alignment:=wdAlignTabRight, Leader:=2
    End With
    oRng.End = oRng.End - 1
    oRng.InsertAfter Chr(32) & vbTab
    DoEvents
  Next oPara
lbl_Exit:
  Set oRng = Nothing
  Set oPara = Nothing
  Exit Sub
End Sub
WOW. It works perfectly well. Thanks both for your time...and abusing of your time what you should I change in the code above to do the way around?. I mean there is a document the end of the page where all the phrases´hyphens should be erased
Reply With Quote
  #12  
Old 10-30-2019, 02:24 PM
Guessed's Avatar
Guessed Guessed is offline Hyphen until the end of the row Windows 10 Hyphen until the end of the row Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

You can record a macro to remove the tabs easily enough. Or you can do it with a quick find and replace.
Ctrl-A to select all
Ctrl-H to open the replace dialog
Find ^t
Nothing in Replace field
Click on Replace All
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #13  
Old 10-31-2019, 01:28 AM
abc3132 abc3132 is offline Hyphen until the end of the row Windows 7 64bit Hyphen until the end of the row Office 2007
Novice
Hyphen until the end of the row
 
Join Date: Feb 2015
Posts: 24
abc3132 is on a distinguished road
Default

Many thanks Andrew!!
Reply With Quote
  #14  
Old 10-31-2019, 04:36 AM
gmayor's Avatar
gmayor gmayor is offline Hyphen until the end of the row Windows 10 Hyphen until the end of the row Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

This thread has inspired me to make some additions to my add-in Lined Headings to provide this type of function to paragraphs, including selected paragraphs. You may find it useful.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Tags
hyphen



Similar Threads
Thread Thread Starter Forum Replies Last Post
Hyphen goes to next line DBlomgren Publisher 0 07-14-2016 06:46 PM
Bibliography Hyphen for repeat author grantgibson45 Word 3 02-25-2013 04:32 AM
Switching text on either side of hyphen nignog Word 1 01-23-2012 01:13 PM
Hyphen until the end of the row Auto change hyphen to em character not working gstech Word 1 10-08-2010 09:57 AM
Hyphen vs. en-dash tocuin Word 0 09-18-2009 10:01 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:58 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft