M
Mike Petrie
Having resolved some earlier problems with using column select mode I've hit
another wierd problem. I can now get columns to move correctly, but when I
try to restore the surrounding markers (bookmarks) Word can't seem to count
unless I single step the procedure! Using single step works just fine, but
not running it. Obviously a timing issue, but adding some sleeps inbetween
the paste and the remark of the pasted block didn't help. Any ideas? The
code is below:
Sub MoveColumn()
Dim rngStart As Range
Dim rngEnd As Range
Dim rngCurPos As Range
Dim iStartLine As Long
Dim iEndLine As Long
Dim iStartCol As Long
Dim iEndCol As Long
' Record current cursor position
Set rngCurPos = Selection.Range
ActiveDocument.Bookmarks.Add "CurPos", Selection.Range
' Select text block delimited by <KB> and <KK>
Set rngStart = ActiveDocument.Bookmarks("Start").Range
Set rngEnd = ActiveDocument.Bookmarks("End").Range
' Select the block by going to the beginning, then moving down
' and then right, until the end is reached
' Get first line and column number of the block
iStartLine = rngStart.Information(wdFirstCharacterLineNumber)
iStartCol = rngStart.Information(wdFirstCharacterColumnNumber)
' Now get the last line and column numbers
iEndLine = rngEnd.Information(wdFirstCharacterLineNumber)
iEndCol = rngEnd.Information(wdFirstCharacterColumnNumber)
' Go to beginning of block
rngStart.Select
Selection.Collapse (wdCollapseStart)
' Turn ON Column Select Mode
Selection.ColumnSelectMode = True
' Select the block
Selection.MoveDown unit:=wdLine, Count:=(iEndLine - iStartLine)
Selection.MoveRight unit:=wdCharacter, Count:=(iEndCol - iStartCol)
' Block is marked, so can now carry out Move - Need to use Clipboard
Selection.Copy
Selection.Delete
' Insert the copied block at the cursor position
ActiveDocument.Bookmarks("CurPos").Range.Paste
' We're left at the start marker so go back to the cursor position
ActiveDocument.Bookmarks("CurPos").Select
' Move the start marker to the cursor
ActiveDocument.Bookmarks.Add "Start", Selection.Range
' move the cursor to the moved block's end
Selection.MoveDown unit:=wdLine, Count:=(iEndLine - iStartLine)
Selection.MoveRight unit:=wdCharacter, Count:=(iEndCol - iStartCol)
' Set the new end marker position
ActiveDocument.Bookmarks.Add "End", Selection.Range
' Tidy up
Selection.ColumnSelectMode = False
' put cursor back where it started
ActiveDocument.Bookmarks("CurPos").Select
' Don't need the saved position marker any more, so remove it
ActiveDocument.Bookmarks("CurPos").Delete
End Sub
Thanks
Mike
another wierd problem. I can now get columns to move correctly, but when I
try to restore the surrounding markers (bookmarks) Word can't seem to count
unless I single step the procedure! Using single step works just fine, but
not running it. Obviously a timing issue, but adding some sleeps inbetween
the paste and the remark of the pasted block didn't help. Any ideas? The
code is below:
Sub MoveColumn()
Dim rngStart As Range
Dim rngEnd As Range
Dim rngCurPos As Range
Dim iStartLine As Long
Dim iEndLine As Long
Dim iStartCol As Long
Dim iEndCol As Long
' Record current cursor position
Set rngCurPos = Selection.Range
ActiveDocument.Bookmarks.Add "CurPos", Selection.Range
' Select text block delimited by <KB> and <KK>
Set rngStart = ActiveDocument.Bookmarks("Start").Range
Set rngEnd = ActiveDocument.Bookmarks("End").Range
' Select the block by going to the beginning, then moving down
' and then right, until the end is reached
' Get first line and column number of the block
iStartLine = rngStart.Information(wdFirstCharacterLineNumber)
iStartCol = rngStart.Information(wdFirstCharacterColumnNumber)
' Now get the last line and column numbers
iEndLine = rngEnd.Information(wdFirstCharacterLineNumber)
iEndCol = rngEnd.Information(wdFirstCharacterColumnNumber)
' Go to beginning of block
rngStart.Select
Selection.Collapse (wdCollapseStart)
' Turn ON Column Select Mode
Selection.ColumnSelectMode = True
' Select the block
Selection.MoveDown unit:=wdLine, Count:=(iEndLine - iStartLine)
Selection.MoveRight unit:=wdCharacter, Count:=(iEndCol - iStartCol)
' Block is marked, so can now carry out Move - Need to use Clipboard
Selection.Copy
Selection.Delete
' Insert the copied block at the cursor position
ActiveDocument.Bookmarks("CurPos").Range.Paste
' We're left at the start marker so go back to the cursor position
ActiveDocument.Bookmarks("CurPos").Select
' Move the start marker to the cursor
ActiveDocument.Bookmarks.Add "Start", Selection.Range
' move the cursor to the moved block's end
Selection.MoveDown unit:=wdLine, Count:=(iEndLine - iStartLine)
Selection.MoveRight unit:=wdCharacter, Count:=(iEndCol - iStartCol)
' Set the new end marker position
ActiveDocument.Bookmarks.Add "End", Selection.Range
' Tidy up
Selection.ColumnSelectMode = False
' put cursor back where it started
ActiveDocument.Bookmarks("CurPos").Select
' Don't need the saved position marker any more, so remove it
ActiveDocument.Bookmarks("CurPos").Delete
End Sub
Thanks
Mike