Inserting dummy text from a macro revisited

O

Ogier

Hi out there!

In a thead started om May 30 I asked how to insert dummy text using the
=rand(.,.) statement.


Dough Robbins replied:

"Running a macro with the following code:

Selection.TypeText "=RAND(5,7)"
SendKeys "~"

on a machine with Vista Business and Word 2007 results in the following at
the location of the selection:

On the Insert tab, the galleries include items that are designed to
coordinate with the overall look of your document. You can use these
[etcetera]".

(This is the kind of result I want!)


Graham Mayor responded:

"The userform is the active window when you run this macro.
Sendkeys works in the active window.
You would need to dismiss the userform first eg

Private Sub CommandButtonInsertDummyText_Click()
Me.Hide
Selection.TypeText "=RAND(5,7)"
SendKeys "~"
End Sub"


I cannot make any of these suggestions work (using Word 2007 on a XP Prof
machine and calling the sub from a user form).

Using Dough's version I get, as I noted earlier, an infinite loop printing
"=RAND(5,7)".

Using Graham's version results in the text "RAND(5,7)" being written in the
document and a carriage return being inserted in the VB source code at the
position of the cursor. Switching the first two lines has the same effect.

I tried to add "Me.Show" after the line with SendKeys (in order to have a
symmetric situation) but this resulted in a "Out of stack space" sitiation.


I presume that "Me" refers to the user form, but the hiding seems not to work.

How come Dough Robbins' version can manage without the "Me"-statement?

Any furthers suggestions to make this work?


Best regards

Holger Nielsen
 
G

Graham Mayor

I have just retested and

Private Sub CommandButtonInsertDummyText_Click()
Me.Hide
Selection.TypeText "=RAND(5,7)"
SendKeys "~"
End Sub

works fine. Are you using English regional settings in Windows? If not, what
is your list separator character?
You may need to change to something like

Private Sub CommandButtonInsertDummyText_Click()
Me.Hide
Selection.TypeText "=RAND(5;7)"
SendKeys "~"
End Sub

Me.Hide should hide the userform containing the button and return focus to
the document..

Unload Me
(no period)
Unloads the form altogether and will work as an alternative.


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
O

Ogier

Graham Mayor said:
I have just retested and

Private Sub CommandButtonInsertDummyText_Click()
Me.Hide
Selection.TypeText "=RAND(5,7)"
SendKeys "~"
End Sub

works fine. Are you using English regional settings in Windows? If not, what
is your list separator character?
Comma work fine a Word document, semicolon as you suggested does not.
Me.Hide should hide the userform containing the button and return focus to
the document..

Unload Me
(no period)
Unloads the form altogether and will work as an alternative.

Graham Mayor - Word MVP


I just don't get it!
I am using Word 2007 and Windows XP.
Using a fresh user form, two command buttons and the following code:

Private Sub CommandButton1_Click()
Me.Hide
Selection.TypeText "=RAND(5,7)"
SendKeys "~"
End Sub

Private Sub CommandButton2_Click()
Unload Me
Selection.TypeText "=rand(5,7)"
SendKeys "~"
End Sub

Pressing Button1 has the following result:
1) The text "=RAND(5,7)" is inserted in the Word document (pressing the
enter key manually after this produces the desired random text).
2) If the cursor in the VB source code is positioned after "Command" in
"CommandButton1_Click", the code line changes to
Private Sub Command()
Button1_Click()
and the following procedure is added:

Private Sub UserForm_Click()

End Sub

So, apparentlty, a car return is indeed inserted, but not in the Word text,
but in the VB source code.

Pressing Button2 inserts "=rand(5,7)" in the Word document and adds a
UserForm_Click sub as above, but does not insert a car return in the VB
source code.

I have tried to replace "~" with Chr(126) and "{ENTER}", but the results are
as above.

It is as if the Me.Hide only has a scope of one line. On the other hand,
adding another Me.Hide after the Selection statement does not change anything.

Any further clues?

Thank you for your patience.

Holger Nielsen
 
G

Graham Mayor

It sounds as though you are running the macro to call the userform from the
vba editor. It will only work if you call it from the document. Sendkeys
works in the *current* window.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
O

Ogier

Graham Mayor said:
It sounds as though you are running the macro to call the userform from the
vba editor. It will only work if you call it from the document. Sendkeys
works in the *current* window.

Indeed, in the two months I have been programming macros, I have always
called userforms from the editor. I looked for a way to call the userform
from the document and found this example in Cindy's book:

Option Explicit

Sub Demo_DummyText()
Dim frm As frmDummyText
'Show user form
Set frm = New frmDummyText
frm.Show
Set frm = Nothing
End Sub

Now the macro inserts text as I wish it, and I have learned something new.
Thank you very much!

Holger
 
G

Graham Mayor

The problem is not that you cannot run the macro from the vba editor, but
that the sendkeys command from the user form will operate as a direct
command in the displayed window - the vba editor - and not the document.
the macro command
frmDummyText.Show
would have worked as well, but either macro must be called while the
document has focus and not the vba window.
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top