Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 40 of 40

Thread: Solved: How to copy whole text into a string?

  1. #21
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by jumpjack
    But I'll wait to close this issue till I'll know if actually selection.text works or not in word2000.
    I'll wait to see if Tony's code does the trick for you. Looks like it to me.

    Selection.Text of course works for sure in 97 to 2003 like Tony I don't have 95.

    You could also try: Selection.Range.Text (But it should make no difference)

    HTH,
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  2. #22
    VBAX Regular
    Joined
    Sep 2005
    Posts
    35
    Location
    Ok, this is the actual macro on Word2000:

    [vba]
    Sub RiempiMinuta()
    ' Scrive una riga in ogni casella della tabella-minuta.

    Dim count
    Dim count_line
    Dim testo As Variant


    Dim temp$
    Dim store$
    Dim lines
    Const MAXLENGTH = 6


    Selection.MoveDown Unit:=wdLine, count:=1, Extend:=wdExtend
    Selection.Paste
    MsgBox ("wait")
    End
    Debug.Print "vero:", Selection.Range.Text
    testo = Selection.Range.Text
    Debug.Print "val:", Selection.End, Selection.Start
    Debug.Print "len=", Selection.End - Selection.Start

    Debug.Print "mio:", testo


    For count = 1 To Len(Selection.Text)
    Debug.Print Mid$(Selection.Text, count, 1); " - "; Asc(Mid$(Selection.Text, count, 1))
    Next


    testo = Replace$(Selection.Text, Chr$(13), "<br>") ' Memorizza selezione e la azzera.
    testo = Replace$(testo, Chr$(7), "<br>")
    Selection.Cut


    count = 1
    count_line = 1
    lines = 0
    While count < Len(testo)
    temp$ = temp$ + Mid$(testo, count, 1)
    count = count + 1
    count_line = count_line + 1
    If count_line > MAXLENGTH Or count = Len(testo) Then ' divide in righe.
    If InStr(temp$, " ") <> 0 Then
    While Mid$(temp$, Len(temp$), 1) <> " "
    count_line = count_line - 1
    count = count - 1
    temp$ = Left$(temp$, Len(temp$) - 1)
    Wend
    End If
    store = Selection.Text
    Selection.Text = temp$
    Selection.Font.name = "Courier New"
    Selection.Font.Size = 10
    Selection.Font.Bold = False
    Debug.Print Selection.Text & Str$(lines) & " - " & Str$(count) & " - " & Str$(count_line) & " ; " & Str$(Len(testo))
    'Selection.Paste
    Selection.MoveDown Unit:=wdLine, count:=1
    Selection.Text = store$

    count_line = 1
    lines = lines + 1
    temp$ = ""

    End If
    Wend
    End Sub
    [/vba]
    I'm now testing it.
    I'm now thinking that maybe the problem is the way I "transfer" the text from a document to another: the PASTE action appears to lose most of the text!

    I use it as I don't know how to copy clipboard contents into a string variable: is there a method to do it?

  3. #23
    VBAX Regular
    Joined
    Sep 2005
    Posts
    35
    Location
    How can I programmatically select the whole contents of the a table cell where te cursor currently is in?

  4. #24
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi jumpjack,

    It is possible to get the contents of the clipboard into a variable but not entirely straightforward.

    There are several ways to refer to an individual cell. It's a bit hard to tell what's best in your situation but:[vba]Selection.Cells(1).Range.text[/vba] should do it. This will include the end of cell marker so you might want to drop the last two characters from it:[vba]Left(Selection.Cells(1).Range.text,Len(Selection.Cells(1).Range.text)-2)[/vba]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  5. #25
    VBAX Regular
    Joined
    Sep 2005
    Posts
    35
    Location
    Ok, after some fixing, here should be the ultimate, working version:
    [vba]
    Sub RiempiMinuta()
    ' Scrive una riga in ogni casella della tabella-minuta.

    Dim count
    Dim count_line
    Dim testo As Variant


    Dim temp$
    Dim store$
    Dim lines
    Const MAXLENGTH = 66


    ' Selection.MoveDown Unit:=wdLine, count:=1, Extend:=wdExtend
    Selection.Paste
    testo = Selection.Cells(1).Range.Text
    Selection.Cells(1).Range.Text = ""
    temp$ = ""

    count = 1
    count_line = 1
    lines = 0
    While count < Len(testo)
    temp$ = temp$ + Mid$(testo, count, 1)
    Debug.Print count, Mid$(testo, count, 1), Asc(Mid$(testo, count, 1))
    count = count + 1
    count_line = count_line + 1
    ' ******** Split in lines:
    If count_line > MAXLENGTH Or _
    count = Len(testo) Or _
    Mid$(testo, count, 1) = Chr$(13) Or _
    Mid$(testo, count, 1) = Chr$(15) Or _
    Mid$(testo, count, 1) = Chr$(11) Then
    If Mid$(testo, count, 1) <> " " And _
    InStr(temp$, " ") <> 0 _
    And Mid$(testo, count, 1) <> Chr$(13) And _
    Mid$(testo, count, 1) <> Chr$(15) And _
    Mid$(testo, count, 1) <> Chr$(11) Then ' Avoid splitting words: split line only on spaces!
    While Mid$(temp$, Len(temp$), 1) <> " " ' "Rewind" temp$.
    count_line = count_line - 1
    count = count - 1
    temp$ = Left$(temp$, Len(temp$) - 1)
    Wend
    End If
    If Mid$(testo, count, 1) = Chr$(13) Or _
    Mid$(testo, count, 1) = Chr$(15) Or _
    Mid$(testo, count, 1) = Chr$(11) Then
    testo = Left$(testo, count) + Right$(testo, Len(testo) - count)
    End If
    'store = Selection.Text
    temp$ = Replace$(temp$, Chr$(13), "")
    temp$ = Replace$(temp$, Chr$(11), "")
    temp$ = Replace$(temp$, Chr$(15), "")
    Selection.Text = temp$
    'Selection.Font.name = "Courier New"
    'Selection.Font.Size = 10
    'Selection.Font.Bold = False
    Selection.MoveDown Unit:=wdLine, count:=1
    Selection.Text = LTrim$(temp$)
    count_line = 1
    lines = lines + 1
    temp$ = ""
    End If
    Wend
    End Sub

    [/vba]

    Thanks for your help.


    Being able to copy&pasteALREADY FORMATTED text would be fine... Any clue?

  6. #26
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Actually Tony, I believe that when you are extract the .Range.Text from a Word table cell you should only be trimming off 1 character.

    It should be Len(Selection.Cells(1).Range.text)-1) , not 2. While true the ASCII characters for the "end-of-cell" marker...ahem, the paragraph mark,.... Word counts it as 1, not 2. The second ASCII character ("bell") is not considered in the character count.

  7. #27
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    You're probably right, Gerry. I regularly forget - If I'm writing code it's easy to check. I suppose it's not that hard to check when I'm posting, I just didn't - my bad, as they say.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  8. #28
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Hey, no sweat.

  9. #29
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Jack,

    Is this one solved? Looks like it. (Thanx for sharing your sollution)

    Don't forget to mark it solved.
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  10. #30
    VBAX Regular
    Joined
    Sep 2005
    Posts
    35
    Location
    Yes, this issue is solved.

  11. #31
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I would still like to know precisely what is trying to be achieved here. Generally, using Selection is not the best way to process instructions. Using Range is better, although I am not sure of the object model. However, it IS Word 2000, correct?

  12. #32
    VBAX Regular
    Joined
    Sep 2005
    Posts
    35
    Location
    Quote Originally Posted by fumei
    I would still like to know precisely what is trying to be achieved here. Generally, using Selection is not the best way to process instructions. Using Range is better, although I am not sure of the object model. However, it IS Word 2000, correct?
    Yes, word 2000.
    I needed a macro to fill in all the rows of a table with an existing text, using one single line per cell. This is what my macro does.

    It would be better if I could fill the table with FORMATTED text, but I don't know where to start from... Any tip?

  13. #33
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    This is why I am asking for you to state EXACTLY what you are doing. Of course you can have formatted text.

    WHERE is the text coming from?
    Does it use Styles?
    Is the original text formatted correctly?
    Are you saying that when you put the text in the cells, it is NOT formatted? Or not formatted correctly.

    Please describe.

  14. #34
    VBAX Regular
    Joined
    Sep 2005
    Posts
    35
    Location
    Quote Originally Posted by fumei
    This is why I am asking for you to state EXACTLY what you are doing. Of course you can have formatted text.

    WHERE is the text coming from?
    Does it use Styles?
    Is the original text formatted correctly?
    Are you saying that when you put the text in the cells, it is NOT formatted? Or not formatted correctly.

    Please describe.
    Text comes from another document currently open (I think I already said in this thread).
    I have two documents: one contains text, one contains the emoty table. I want the text from the first document to be splitted in fixed-length lines and to fill the table, one line per cell (table is 2 columns wide, n lines long).

    Original text is already formatted the right way. It would be interesting (but not required) that formatting is maintained during the copy.

    Currently I accomplished the task of splitting&filling; I don't know how to keep the text formatting.

  15. #35
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Original text is already formatted the right way. It would be interesting (but not required) that formatting is maintained during the copy.

    Currently I accomplished the task of splitting&filling; I don't know how to keep the text formatting.
    It may help if you can answer my questions fully. I will try again.

    Does the original text use Styles? Because if it does, then keeping the format is easy.

  16. #36
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Sorry, OK, maybe not easy, but using styles would certainly solve any format issue.

    Have a style in the document with the table that matches the format of the source document. Apply it to the text going in the table.

    Done.

  17. #37
    VBAX Regular
    Joined
    Sep 2005
    Posts
    35
    Location
    No styles, just CTRL+B or CTRL+I used on some parts of the text...

  18. #38
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by jumpjack
    No styles, just CTRL+B or CTRL+I used on some parts of the text...
    Wel you can add bold or Italic formating directly to the Font Property of the Selection or Range object like:[vba]
    Selection.Font.Bold = True
    Selection.Font.Italic = True
    [/vba]

    HTH,
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  19. #39
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    It would be much easier to use Styles, but Joost is correct, if you want to ADD the format to the text you are inserting into tghe table.....then just do that. However, your question was how to KEEP the format. Keeping format is the whole purpose behind Styles.

  20. #40
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by fumei
    It would be much easier to use Styles, but Joost is correct, if you want to ADD the format to the text you are inserting into tghe table.....then just do that. However, your question was how to KEEP the format. Keeping format is the whole purpose behind Styles.
    I agree buddy. The questione was about retaining the formatting of the text. (As usual I've lost track of the original question)

    Yes Styles of course are a good way of doing this but If you already have a document with formatting in it. And you want to get some formatted text and put it Formatted back in your table cell then I think it's just as easy to use the FormattedText property of the range object.

    That way you can set to the formatted range and pass to the cells range the formatted text. No need to apply the style afterwards.

    But I think our OP has enough stuff to test on by now.

    Later..
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

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