#1
|
|||
|
|||
Identify current column on a page
In Word 2010 I have a landscape document which has three equal width columns on page 1, and 2 columns on page 2, the firsty being 1/3 of the width, the second being 2/3 of the width.
I have a variable (in terms of number of lines) amount of text in the first column of page 2, and I wish to insert three lines of text at the bottom of the first column on page 2, in a formatted box. My plan was to identify how many lines are left in column 1 by inserting blank lines (vbLf) until the column number changes. My problem is that the only references I can find to columns all seem to relate to tables, not to the current page. Does anyone know how I can access the current page's current column number (if such a thing exists). I've tried various "promising" methods, e.g. Code:
' ' Code snippet ' Dim grng As Word.Range Dim intColNo As Integer ' Set grng = ActiveDocument.Content grng.Collapse Direction:=wdCollapseEnd 'The next line fails, telling me I'm not in a Table. intColNo = grng.Columns.Count 'The next line always returns -1. intColNo = grng.Information(wdEndOfRangeColumnNumber) Can any kind soul help? |
#2
|
||||
|
||||
wdEndOfRangeColumnNumber relates to tables as you have discovered however following that line of thought you could use instead the horizontal page position to determine where the cursor or range is e.g. as in the following function. The three figures are the horizontal page positions of the three left margins.
Code:
Function WhichPageCol(oRng As Range) As Long Select Case True Case oRng.Information(wdHorizontalPositionRelativeToPage) >= 72 And _ oRng.Information(wdHorizontalPositionRelativeToPage) <= 316.5 WhichPageCol = 1 Case oRng.Information(wdHorizontalPositionRelativeToPage) >= 316.5 _ And oRng.Information(wdHorizontalPositionRelativeToPage) <= 561 WhichPageCol = 2 Case oRng.Information(wdHorizontalPositionRelativeToPage) >= 561 WhichPageCol = 3 End Select End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Redefines ""Think outside the box"
Thanks gmayor - that helped a lot!
What is slightly embarrassing is that in order to switch from a three column page 1 to a two column page 2, and in order to keep column 1 the sam width on each page I had already determined the column width. A brilliant example of ACTUAL lateral thinking! I was thinking of identifying the vertical position of the "cursor", which is moderately easy when encountering a page break, but still eluded me when forcing a column break. Going horizontal works a treat. (Although you'd think there would be some convenient Information property like "wdCurrentColumnOnPage"; or is that falling into the trap of assuming Microsoft want to be helpful?) Thanks again... |
#4
|
|||
|
|||
Now it hangs/loops!
Looks like I spoke too soon.
When processing with frequent "Stop" commands to check all is working, my code runs perfectly. However, when running end-to-end it loops/freezes (i.e. it becomes unresponsive and I have to use Task Manager to dismis Word). I've had similar issues with Excel in the past, where some functions need the VBA to be paused to allow the system to catch up. Is this a known issue with Range.Information? (Note: My personal variable naming convention means that any variable starting with "g" is declared globally in a separate "Globals" module) In my document formatting I use the following code, which recovers the horizontal end points for each column:- Code:
Public Sub SetColsInWholeDoc(intCols As Integer) '####################################### '# Set the required number of columns. # '# N.B. The numcolumns parameter seems # '# to be the EXTRA columns (above # '# the basic 1), so we subtract # '# 1 from the User's specified # '# column count. # '####################################### '* '** Check it's a valid request. '* If intCols < 2 Then Exit Sub '* '** Now set the required column count. '* With Selection.Sections(1) With .PageSetup.TextColumns .SetCount NumColumns:=intCols - 1 .Add Spacing:=InchesToPoints(0.25), _ EvenlySpaced:=True End With glngColWidth = .PageSetup.TextColumns(1).width gsngColWInches = PointsToInches(glngColWidth) ' MsgBox glngColWidth & " pts, " & gsngColWInches & " inches" End With '* '** Set up horizontal end point of '** of each page column. '* glngCol1HEnd = glngColWidth Select Case intCols Case 1 glngCol2HEnd = 0 glngCol3HEnd = 0 Case 2 glngCol2HEnd = glngColWidth * 2 glngCol3HEnd = 0 Case 3 glngCol2HEnd = glngColWidth * 2 glngCol3HEnd = glngColWidth * 3 End Select End Sub 'SetColsInWholeDocc Code:
Public Function funGetCurPageCol(rng As Range) As Long Dim lngH As Long lngH = rng.Information(wdHorizontalPositionRelativeToPage) Select Case True Case lngH <= glngCol1HEnd funGetCurPageCol = 1 Case lngH > glngCol1HEnd And _ lngH <= glngCol2HEnd funGetCurPageCol = 2 Case lngH > glngCol2HEnd funGetCurPageCol = 3 End Select End Function 'funGetCurPageCol |
#5
|
||||
|
||||
It appears you are going off down rabbit holes instead of addressing your actual requirement
Quote:
Code:
Sub AddACallout() Dim aRange As Range, aShape As Shape, sCallout As String Dim bLeft As Boolean, i As Integer, sPath As String, iWidth As Integer iWidth = Selection.Sections(1).PageSetup.TextColumns(1).Width 'Must be in Page Layout view for macro to show effect If ActiveWindow.View.SplitSpecial = wdPaneNone Then ActiveWindow.ActivePane.View.Type = wdPrintView Else ActiveWindow.View.Type = wdPrintView End If If Selection.Paragraphs.Count > 1 Then Set aRange = Selection.Range aRange.Start = aRange.Paragraphs.First.Range.Start aRange.End = aRange.Paragraphs.Last.Range.End Else Set aRange = Selection.Paragraphs(1).Range End If sCallout = aRange.Text aRange.Text = "" Set aShape = ActiveDocument.Shapes.AddShape(Type:=msoShapeRectangle, Left:=0, Top:=2, Width:=iWidth, Height:=80, Anchor:=aRange) 'msoShapeRoundedRectangle With aShape .TextFrame.TextRange = Left(sCallout, Len(sCallout) - 1) .TextFrame.TextRange.Style = "Normal" .TextFrame.MarginTop = 0 .TextFrame.MarginLeft = 4 .TextFrame.MarginRight = 4 .TextFrame.MarginBottom = 4 .TextFrame.AutoSize = True .WrapFormat.Type = wdWrapSquare .WrapFormat.Side = wdWrapBoth .RelativeVerticalPosition = wdRelativeVerticalPositionBottomMarginArea .Top = -.Height .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin .Left = wdShapeLeft .Line.Visible = msoFalse .Fill.ForeColor = RGB(150, 230, 230) End With End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
Tags |
columns, count, information |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Return nameof Left column in current view | trevorc | Excel Programming | 7 | 09-19-2018 08:38 PM |
Table adding cells to previous page with room still on current page. | gedet | Word | 1 | 01-03-2018 10:35 AM |
Identify last blank cell in column | mbesspiata | Excel | 0 | 02-27-2015 11:29 AM |
How to save the current page in a new file with all the page settings (header, footer | Jamal NUMAN | Word | 6 | 03-15-2012 03:27 PM |
Is there a way to automatically highlight the column and the row that of the current | Jamal NUMAN | Excel | 8 | 02-14-2012 02:58 PM |