PDA

View Full Version : [SOLVED:] =lorem(1,1) and =rand(1,2) in macros won't update the display



kkkwj
06-12-2018, 12:08 PM
Hi, I'm trying to write a VBA macro to type "=lorem(1,5) <plus Enter>" into Word so that I don't have to type in the lorem string to generate the usual lorem text. If I type it in and press Enter, it gets replaced by the desired text. If I record a macro of my actions and run the macro, nothing happens until I type a space. Then the whole paragraph shows up. Same with the '=rand(2,5)' string.

How can I change my macro to make it update the display (or run the macro after the string has been inserted into the buffer)? Thank you

Selection.TypeText Text:="=rand(2,2)"
Selection.TypeText Text:=vbCr

macropod
06-15-2018, 04:35 PM
Try:

Sub Demo()
SendKeys "=rand{(}2,2{)}" & vbCr
End Sub

kkkwj
06-16-2018, 11:08 AM
That works! Thank you so much.

I wonder why I never thought of sending the keys; I suppose sending the final key is like the one that I type after inserting the macro. I will experiment with an insertion first and a final keystroke at the end. Thank you again for your help. Where would we all be without your expertise?

UPDATE: Yes, doing the insertion by code (as shown in my original post) followed by a SendKeys operation works great. It's sending the trailing vbCr that gets word to expand the magic rand/lorem functions.

macropod
06-17-2018, 07:40 PM
doing the insertion by code (as shown in my original post) followed by a SendKeys operation works great. It's sending the trailing vbCr that gets word to expand the magic rand/lorem functions.
I don't know why you'd bother with that circumlocution when the SendKeys code I posted does the lot in a single operation...

kkkwj
06-18-2018, 09:53 AM
I was just exploring and trying to learn more, that's all. :-) Thank you again for your help.