Results 1 to 5 of 5
  1. #1
    JustCallMeAl
    Guest

    Move Range Backwards by Cell in Table

    I have a two-column, twenty-row table. I want to start somewhere in the table (user defined), insert text (user defined), and then move backwards through the table with a range object, inserting the same text in each cell a user-defined number of cells. (Creating labels.)

    The Selection object has a nice method MoveLeft. However, the Range object does not. It does, however, have a Move object. So, I tried it:

    <pre>Set rngInsertHere = ActiveDocument.Tables(1).Cell(UserDefinedRow, UserDefinedColumn).Range

    rngInsertHere.Text = "User Defined"

    rngInsertHere.Move wdCell, -1

    rngInsertHere.Text = "User Defined"</pre>


    The range did not move. The "User Defined" was inserted in the originating cell, the one I had set rngInsertHere equal to. In other words, Cell(UserDefinedRow, UserDefinedCell) had "User Defined" inserted into it twice.

    Ideas? Don't particularly want to use Selection (though it does work), and don't particularly want to use bookmarks. I'm wanting to learn more about manipulating the Range object.

    Thanks for your consideration.

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Move Range Backwards by Cell in Table

    I'll give you a hint and see how far you get with it:

    You need to use the Start and End properties of the range object.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  3. #3
    JustCallMeAl
    Guest

    Re: Move Range Backwards by Cell in Table

    Try one step more.

    In the interim, though I still want to learn more about Ranges, I have found that I can step through the Cells collection, instead of setting the range time and again.

    However, I would still like the knowledge.

  4. #4
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Move Range Backwards by Cell in Table

    You can make it look pretty:

    Just change the cell reference for start and end

    Dim aRng As Range
    Set aRng = ActiveDocument.Range(Start:=ActiveDocument.Tables( 1).Cell(1, 2).Range.Start, _
    End:=ActiveDocument.Tables(1).Cell(1, 2).Range.End)
    aRng.Select
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  5. #5
    JustCallMeAl
    Guest

    Re: Move Range -- Working Code, but different

    Thanks, Kevin, for your assistance. As I wrote above, by the time I saw your response, I had found that I could use the Cells collection to accomplish what I wanted much easier than trying to position the range around. In this case, the collection method may have been the better solution as I would have to have several for/next loops to pick up every combination of cell column/row.

    Anyway, here is the solution that I came up with. Before I post it, though, let me give a little more background.

    We print on label sheets more than once (I know, I know, it is highly unadvisable <img src=/S/smile.gif border=0 alt=smile width=15 height=15>). In our case, we don't do it "back-to-back." Many times, the second time may be days after; so, Avery's caution about the glue leaking out and gumming up the laser printers is negated by the long time period between printings.

    Now I have no idea if the next is a myth or not, but supposedly, at some time in the past, our company decided that it was better to use the labels from the bottom up if you are only going to use a partial sheet, that way it is "stronger" to go through the printer multiple times. Whether that is fact or fiction, I don't know. But that gives the programming parameter of needing to go backwards (bottom to top, right to left) on a sheet of labels.

    I will now post the procedure and make some explanations:

    <pre>Public Sub InsertLabelsIntoDocument( _
    vfrmTransferFrom As UserForm, _
    vwdTransferTo As Word.Document, _
    strLabelContent As String, _
    intStartingPosition As Integer, _
    intTotalNumberOfLabels As Integer)

    Dim intCounter As Integer
    Dim intFNLowerBound As Integer
    Dim intFNUpperBound As Integer
    Dim intInsertCounter As Integer
    Dim intLabelsPage2Plus As Integer
    Dim rngTableCell As Range

    Set rngTableCell = vwdTransferTo.Tables(1).Range

    intFNUpperBound = 21 - intStartingPosition

    If intFNUpperBound - intTotalNumberOfLabels <= 0 Then
    intFNLowerBound = 1
    Else
    intFNLowerBound = intFNUpperBound - intTotalNumberOfLabels + 1
    End If

    For intCounter = intFNUpperBound To intFNLowerBound Step -1
    rngTableCell.Cells(intCounter).Range.Text = strLabelContent
    intInsertCounter = intInsertCounter + 1
    Next intCounter

    ' Build the rest of the table, page 2

    intLabelsPage2Plus = intTotalNumberOfLabels - intInsertCounter

    rngTableCell.Cells(20).Select

    For intCounter = 1 To intLabelsPage2Plus
    Selection.MoveRight unit:=wdCell
    Selection.Range.Text = strLabelContent
    Next intCounter

    End Sub</pre>


    This is based on a starting table on the page of 2 columns and 10 rows, 20 cells. In the above, you will see the number 21 used in calculations. That is the number needed to make the positioning work.

    strLabelContent = what the user enters in the userform which is collated into a "chunk" to drop into the label.

    intStartingPosition = the user, in the userform, is given 20 optionboxes positioned as we want them to choose the starting label. In other words, since we are going backwards, label number 20 is in the upper left, and label number 1 is in the lower right.

    intTotalNumberOfLabels = Self explaining.

    Page one is built from bottom to top. Then, page two of the labels, if needed, is built from top to bottom, as one normally does labels. If that page is only partially used, then the next time, it can be put in the printer "upside down" and all is well.

    There are two variables containing the initials "FN". At this time, I cannot remember what it means!!! And it was only a day ago.

    Any "tightening" of the code will be appreciated.

    Thanks, all.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •