![]() |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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 |
|
#3
|
|||
|
|||
|
Quote:
|
|
#4
|
|||
|
|||
|
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 |
|
#5
|
||||
|
||||
|
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 |
|
#6
|
|||
|
|||
|
Thanks Graham. Just a comment I´ve noticed that the Macro does no work inside a table cell always in a Word document.
Thanks |
|
#7
|
||||
|
||||
|
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 |
|
#8
|
|||
|
|||
|
Quote:
Cheers |
|
#9
|
||||
|
||||
|
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 |
|
#10
|
||||
|
||||
|
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 |
|
#11
|
|||
|
|||
|
Quote:
|
|
#12
|
||||
|
||||
|
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 |
|
#13
|
|||
|
|||
|
Many thanks Andrew!!
|
|
#14
|
||||
|
||||
|
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 |
|
| Tags |
| hyphen |
| Thread Tools | |
| Display Modes | |
|
|
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 |
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 |