PDA

View Full Version : Solved: How to copy whole text into a string?



jumpjack
09-16-2005, 03:44 AM
This command does NOT copy the whole selection into the variable!

a$ = selection.text

Only the first paragraph is stored into the variable: why? How can I store the whole document? Or, how can I parse all paragraphs?

TonyJollans
09-16-2005, 04:24 AM
Hi jumpjack,

Welcome to VBAX!

That statement works for me. What are you actually trying to do?

sheeeng
09-16-2005, 04:27 AM
Welcome to VBAX, jumpjack!

Maybe you can provide more detail in your statement?

TIA.

jumpjack
09-16-2005, 05:02 AM
I want to write a simple macro which splits text in lines, with NUM characters per line, as I need to fill a table with the text, putting one line per cell.

It works fine until I reach a CR inthe text, then it stops. Looking at the debug window, I see that actually when I try to store the text into a variable to process it, only the text before the first CR is stored: all the remaining of the text is NOT into the variable!

Also, if I select the whole text, Selection.end will not be at the end of the text, but just at the first CR!!!
I'm using word 2000 with win2000.

TonyJollans
09-16-2005, 07:43 AM
I'm sorry but I can't reproduce what you say, at all. Can you post your whole code?

MWE
09-16-2005, 08:02 AM
I want to write a simple macro which splits text in lines, with NUM characters per line, as I need to fill a table with the text, putting one line per cell.

It works fine until I reach a CR inthe text, then it stops. Looking at the debug window, I see that actually when I try to store the text into a variable to process it, only the text before the first CR is stored: all the remaining of the text is NOT into the variable!

Also, if I select the whole text, Selection.end will not be at the end of the text, but just at the first CR!!!
I'm using word 2000 with win2000.
I created 3 lines of text in Word with <CR> at the end of each line:

Now is the time for all good men to come to the aid of their party

The quick brown fox jumped over the lazy dog.

She sells sea shells by the sea shore

I then wrote and ran a little macro:

Sub GobbleText()
Dim strBuffer As String

strBuffer = Selection.Text

MsgBox strBuffer

End Sub
seems to work fine.

I know that this does not solve your whole need (break things down into lines, etc), but it appears to do what you initially asked about. I am running Office2K and WinXP.

fumei
09-16-2005, 08:05 AM
if I select the whole text, Selection.end will not be at the end of the text, but just at the first CR!!!

As Tony mentioned, we need your code. Because what you wrote above is impossible. If you actually really Select the whole text, then the whole text is Selected. If that is the case, then your variable is processing some other instruction.

Either that or your instructions for getting the text into the variable is incorrect. It sounds like - as least by your comments - that you are putting the whole text into a variable, then breaking out parts of the variable, to put in the cell.

Please post your code, and state exactly what you want to happen.

jumpjack
09-16-2005, 09:32 AM
Thanks guys, I cant' testi it now: I have Office 95 installed at home :) , I'll have to wait until tomorrow when back to office... ('95 doesn't even accept your macro).

I'll inform you.

jumpjack
09-16-2005, 09:35 AM
I remeber I declared my variable this way:

dim testo$

followed by

testo$ = selection.text

Maybe the "$" is causing the problem?

dim testo$ as string is not accepted. I'll try with dim testo as string

TonyJollans
09-16-2005, 11:48 AM
It may be that what you say does happen in 95 and, if so, I don't know off the top of my head how to work round it. I don't even have a copy of Word 95 any more!

The $ is not the problem and I think, in fact, you have to use it in WordBasic - the "As String" syntax came in with VBA in 97, so just use Dim a$.

jumpjack
09-16-2005, 12:06 PM
It may be that what you say does happen in 95 and, if so, I don't know off the top of my head how to work round it. I don't even have a copy of Word 95 any more!

The $ is not the problem and I think, in fact, you have to use it in WordBasic - the "As String" syntax came in with VBA in 97, so just use Dim a$.
You misunderstood my post: I CAN'T use the macro at all in Word 95; I was talking about what I remember I wrote in Word2000 in my office PC, which I can't access right now and till monday.

TonyJollans
09-16-2005, 12:39 PM
Ah, OK. Sorry.

In VBA on 2000 you should be able to use either:Dim testo$orDim testo As Stringbut NOTDim testo$ As String.

jumpjack
09-16-2005, 01:11 PM
Ah, OK. Sorry.

In VBA on 2000 you should be able to use either:Dim testo$orDim testo As Stringbut NOTDim testo$ As String.
Thanks a lot.
Could you kindly try if your macro still works if assign selection.text contents to a variable declared with this instruction?dim a$

TonyJollans
09-16-2005, 03:12 PM
Hi jumpjack,

Yes it works.

In Word 95, can you do this?


Dim a$
a$ = Selection$()

MOS MASTER
09-16-2005, 04:11 PM
Hi and welcome to VBAX! :hi:

As stated before it seams unlikely the Dimensioning of the variable is the problem here and as stated there are many ways to Dim a variable!

Perhaps you can post a sample document with your code and tell us what is happing when you do this?

And more important what you want it do do instead?

HTH, :whistle:

jumpjack
09-17-2005, 01:12 AM
Hi and welcome to VBAX! :hi:

As stated before it seams unlikely the Dimensioning of the variable is the problem here and as stated there are many ways to Dim a variable!

Perhaps you can post a sample document with your code and tell us what is happing when you do this?

And more important what you want it do do instead?

HTH, :whistle:
My code is on my office's PC and I can't access it till monday. I'm trying to solve the problem "theroetically"...

The macro I wrote successfully split a text into lines long MAXLEN characters, but I want it to split the WHOLE text of the document; instead, it actually works only on the first paragraph, ignoring all the remaining text!

I try to re-invent the macro right now, but I can't guarantee it's exactly the same I wrote on my office PC:


dim testo$
dim count
dim count2
dim temp$
const MAXLEN = 66

testo$=selection.text
count2 = 0
temp$ = ""
for count = 1 to len(testo$)
temp$ = temp$ & mid$(testo$,count,1)
count2= count2+1
if count2 >= MAXLEN
count2=0
[something which adds the text to the document]
[cursor down one position in the table]
debug.print temp$
temp$ = ""
end if
next


This does not work at all on my Home PC (with Office 95 installed): selection.text is not accepted. Looks like my italian version of Word has also the VBA commands translated into Italian!!! :banghead: (I looked at a macro I recorded...)


The macro uses the text of a document to fill a table in another document: I select the text in document A, I switch to document B, I put the text cursor in the first line of a table and I run the macro, which fills the table with one text-line per table-line.

jumpjack
09-17-2005, 01:33 AM
Hi jumpjack,

Yes it works.

In Word 95, can you do this?


Dim a$
a$ = Selection$()
No, it does not work.

But, after some archelogical searching, I eventually found the help for VBA... which is NOT VBA, actually; in 95 it was named WordBasic! That's why I spent 3 hours to find it... :banghead:

Looks like WordBasic commands... have been translated to Italian!!! So I must use:
a$ = selezione$()

But, how can I check the value of a$? I can't see the Variable Window (did it exist???), I can't see debug window(???)... but PRINT command raises no errors! Where the hell is it printing? :dunno

jumpjack
09-17-2005, 01:59 AM
Ok, this macro works fine on Word 95. You will need 2 documents: one containing the source text, the second containing an empty table and named "Documento2":


Sub MAIN
Dim testo$
Dim count
Dim count2
Dim temp$
Dim MAXLEN
Dim inizio
Dim fine

MAXLEN = 25

testo$ = Selezione$()
Attiva "Documento2"
count = 1
count2 = 0
temp$ = ""
While count <= Len(testo$)
temp$ = temp$ + Mid$(testo$, count, 1)
count = count + 1
count2 = count2 + 1
If count2 >= MAXLEN Then
count2 = 0
Inserisci temp$ + Chr$(13) + Chr$(10)
RigaGi?
temp$ = ""
REM SetSelRange inizio, fine
End If
Wend
End Sub


I wonder if I'll success in implementing this trivial program in "such and advanced program" like word 2000.... :dunno :motz2:

TonyJollans
09-17-2005, 06:14 AM
Hi jumpjack,

In 2000 you can, of course, use VBA. But you can still use all the old WordBasic stuff - you just need to tell VBA that it is WordBasic.

My Italian's not so good (OK, it's non-existent) so I'm not entirely sure about this: Selezione = Selection, Attiva = Activate, Inserisci = Insert, RigaGi? = LineDown

This works for me (in English)


Sub MAIN()
Dim testo$
Dim count
Dim count2
Dim temp$
Dim MAXLEN
Dim inizio
Dim fine

MAXLEN = 25

testo$ = WordBasic.Selection$()
WordBasic.Activate "Documento2"
count = 1
count2 = 0
temp$ = ""
While count <= Len(testo$)
temp$ = temp$ + Mid$(testo$, count, 1)
count = count + 1
count2 = count2 + 1
If count2 >= MAXLEN Then
count2 = 0
WordBasic.Insert temp$ + Chr$(13) + Chr$(10)
WordBasic.LineDown
temp$ = ""
Rem SetSelRange inizio, fine
End If
Wend
End Sub

jumpjack
09-17-2005, 06:25 AM
Hi jumpjack,

In 2000 you can, of course, use VBA. But you can still use all the old WordBasic stuff - you just need to tell VBA that it is WordBasic.

My Italian's not so good (OK, it's non-existent) so I'm not entirely sure about this: Selezione = Selection, Attiva = Activate, Inserisci = Insert, RigaGi? = LineDown

This works for me (in English)
Thank you very much: if Word2000 will persist in NOT working with my old macro, I'll just implement this new one.
But I'll wait to close this issue till I'll know if actually selection.text works or not in word2000.

MOS MASTER
09-17-2005, 06:06 PM
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.:yes

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, :whistle:

jumpjack
09-19-2005, 12:48 AM
Ok, this is the actual macro on Word2000:


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

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?

jumpjack
09-19-2005, 12:51 AM
How can I programmatically select the whole contents of the a table cell where te cursor currently is in?

TonyJollans
09-19-2005, 01:17 AM
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:Selection.Cells(1).Range.text should do it. This will include the end of cell marker so you might want to drop the last two characters from it:Left(Selection.Cells(1).Range.text,Len(Selection.Cells(1).Range.text)-2)

jumpjack
09-19-2005, 03:04 AM
Ok, after some fixing, here should be the ultimate, working version:

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



Thanks for your help.


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

fumei
09-19-2005, 06:16 AM
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.

TonyJollans
09-19-2005, 06:45 AM
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.

fumei
09-19-2005, 06:54 AM
Hey, no sweat.

MOS MASTER
09-20-2005, 03:31 PM
Hi Jack, :hi:

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

Don't forget to mark it solved. :*)

jumpjack
09-21-2005, 12:12 AM
Yes, this issue is solved.

fumei
09-21-2005, 08:53 AM
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?

jumpjack
09-21-2005, 12:02 PM
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?

fumei
09-21-2005, 08:08 PM
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.

jumpjack
09-22-2005, 12:04 AM
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.

fumei
09-22-2005, 05:00 AM
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.

fumei
09-22-2005, 05:02 AM
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.

jumpjack
09-22-2005, 05:54 AM
No styles, just CTRL+B or CTRL+I used on some parts of the text...

MOS MASTER
09-22-2005, 02:50 PM
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:
Selection.Font.Bold = True
Selection.Font.Italic = True


HTH, :whistle:

fumei
09-23-2005, 08:23 AM
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.

MOS MASTER
09-23-2005, 03:59 PM
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

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..:whistle: