How to insert dummy text with a macro

O

Ogier

For test purposes I wish to insert variable amounts of dummy text into a
document.
The undocumented command “=RAND(5,7)†is ideal for this purpose. It inserts
5 paragraphs with 7 sentences taken randomly from the Word help texts.
The variant “=LOREM(5,7)†inserts some Latin text instead.
My problem is that I cannot make this work from a macro. I have tried

Private Function MakeDummyText(UseLatin As Boolean) As String
Dim Command As String
If UseLatin Then
Command = "LOREM"
Else
Command = "RAND"
End If
MakeDummyText = "=" + Command + "(" _
+ CStr(TextBoxParagraphs.Text) + "," _
+ CStr(TextBoxSentences.Text) + ")" + Chr(13)
End Function

Private Sub CommandButtonInsertDummyText_Click()
Selection.TypeText Text:=MakeDummyText(OptionButtonLatinText.Value)
End Sub

But the effect of the above subs is only to insert the text, the command is
not carried out. I have tried exchanging Chr(13) with Chr(10) or Chr(10) +
Chr(13) with no success.

So, how do I insert dummy text from a macro?

Best wishes
Holger Nielsen
Denmark
 
G

Graham Mayor

Use sendkeys to send the enter command
SendKeys "~"

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


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

Ogier

Graham Mayor said:
Use sendkeys to send the enter command
SendKeys "~"

Thank you for your respose.

However, replacing with
SendKeys MakeDummyText(OptionButtonLatinText.Value) + "~"

freezes Visual Basic and nothing new is displayed in the Word document.

Word Help on SendKeys states:
"The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses
( ) have special meanings to SendKeys."
but what these meanings are is not mentioned.

Have I misunderstood something?

Best wishes
Holger
 
D

Doug Robbins - Word MVP

Use it this way:

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


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
D

Doug Robbins - Word MVP

Hi Macropod,

Since when?

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
galleries to insert tables, headers, footers, lists, cover pages, and other
document building blocks. When you create pictures, charts, or diagrams,
they also coordinate with your current document look. You can easily change
the formatting of selected text in the document text by choosing a look for
the selected text from the Quick Styles gallery on the Home tab. You can
also format text directly by using the other controls on the Home tab. Most
controls offer a choice of using the look from the current theme or using a
format that you specify directly. To change the overall look of your
document, choose new Theme elements on the Page Layout tab.
To change the looks available in the Quick Style gallery, use the Change
Current Quick Style Set command. Both the Themes gallery and the Quick
Styles gallery provide reset commands so that you can always restore the
look of your document to the original contained in your current template. On
the Insert tab, the galleries include items that are designed to coordinate
with the overall look of your document. You can use these galleries to
insert tables, headers, footers, lists, cover pages, and other document
building blocks. When you create pictures, charts, or diagrams, they also
coordinate with your current document look. You can easily change the
formatting of selected text in the document text by choosing a look for the
selected text from the Quick Styles gallery on the Home tab. You can also
format text directly by using the other controls on the Home tab.
Most controls offer a choice of using the look from the current theme or
using a format that you specify directly. To change the overall look of your
document, choose new Theme elements on the Page Layout tab. To change the
looks available in the Quick Style gallery, use the Change Current Quick
Style Set command. Both the Themes gallery and the Quick Styles gallery
provide reset commands so that you can always restore the look of your
document to the original contained in your current template. On the Insert
tab, the galleries include items that are designed to coordinate with the
overall look of your document. You can use these galleries to insert tables,
headers, footers, lists, cover pages, and other document building blocks.
When you create pictures, charts, or diagrams, they also coordinate with
your current document look.
You can easily change the formatting of selected text in the document text
by choosing a look for the selected text from the Quick Styles gallery on
the Home tab. You can also format text directly by using the other controls
on the Home tab. Most controls offer a choice of using the look from the
current theme or using a format that you specify directly. To change the
overall look of your document, choose new Theme elements on the Page Layout
tab. To change the looks available in the Quick Style gallery, use the
Change Current Quick Style Set command. Both the Themes gallery and the
Quick Styles gallery provide reset commands so that you can always restore
the look of your document to the original contained in your current
template. On the Insert tab, the galleries include items that are designed
to coordinate with the overall look of your document.
You can use these galleries to insert tables, headers, footers, lists, cover
pages, and other document building blocks. When you create pictures, charts,
or diagrams, they also coordinate with your current document look. You can
easily change the formatting of selected text in the document text by
choosing a look for the selected text from the Quick Styles gallery on the
Home tab. You can also format text directly by using the other controls on
the Home tab. Most controls offer a choice of using the look from the
current theme or using a format that you specify directly. To change the
overall look of your document, choose new Theme elements on the Page Layout
tab. To change the looks available in the Quick Style gallery, use the
Change Current Quick Style Set command.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
macropod said:
Note: Sendkeys doesn't work with Vista.

--
Cheers
macropod
[Microsoft MVP - Word]


Doug Robbins - Word MVP said:
Use it this way:

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


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
O

Ogier

Doug Robbins - Word MVP said:
Use it this way:

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

Thank you, Doug. I have tried that using this simplified version:

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

Unfortunately this results in an infinite loop. After breaking, my document
contains lots of pages filled with
=RAND(5,7)=RAND(5,7)=RAND(5,7) etcetera.
Using the MakeDummyText function from my first post I even managed to make
Word crash.

If I place the cursor after any of the "=RAND(5,7)" strings in the document
and press the Enter key that string is replaced correctly.

I an using Word 2007 on a Windows XP system and have Word opened with a
blank document.

I would be glad for any further suggestions!

Holger
 
G

Graham Mayor

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

Similarly it will not work if you are in the vba editor when you run the
macro.
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


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

Ogier

Graham Mayor said:
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

Thank you for the hint! Now the sub prints one copy of =RAND(5,7) in the
document, but unfortunately it is still not converted.
I have tried the alternative SendKeys "{ENTER}" with the same result.

Best regards
Holger
 
M

macropod

Hi Doug,

Perhaps things have changed since Vista SP1 was released.

Sendkeys didn't work in the original Vista release and a number of people came out with Sendkeys repalcements to deal with that
issue. See also http://vb.mvps.org/articles/vsm20071113.pdf. I too experienced Sendkeys not working Office 2000 on Vista - but now
it does! A web search on 'sendkeys vista vba' will return dozens of hits with problems and workarounds.

I note the .NET Framework 3.0 restored Sendkeys functionality, but in a different way than before-quote: "The enhanced security of
Windows Vista (known as User Account Control or UAC) prevents the previous implementation from working as expected"
(http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx).

Evidently, my information was out of date.

--
Cheers
macropod
[Microsoft MVP - Word]


Doug Robbins - Word MVP said:
Hi Macropod,

Since when?

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 galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. When you create
pictures, charts, or diagrams, they also coordinate with your current document look. You can easily change the formatting of
selected text in the document text by choosing a look for the selected text from the Quick Styles gallery on the Home tab. You can
also format text directly by using the other controls on the Home tab. Most controls offer a choice of using the look from the
current theme or using a format that you specify directly. To change the overall look of your document, choose new Theme elements
on the Page Layout tab.
To change the looks available in the Quick Style gallery, use the Change Current Quick Style Set command. Both the Themes gallery
and the Quick Styles gallery provide reset commands so that you can always restore the look of your document to the original
contained in your current template. On the Insert tab, the galleries include items that are designed to coordinate with the
overall look of your document. You can use these galleries to insert tables, headers, footers, lists, cover pages, and other
document building blocks. When you create pictures, charts, or diagrams, they also coordinate with your current document look. You
can easily change the formatting of selected text in the document text by choosing a look for the selected text from the Quick
Styles gallery on the Home tab. You can also format text directly by using the other controls on the Home tab.
Most controls offer a choice of using the look from the current theme or using a format that you specify directly. To change the
overall look of your document, choose new Theme elements on the Page Layout tab. To change the looks available in the Quick Style
gallery, use the Change Current Quick Style Set command. Both the Themes gallery and the Quick Styles gallery provide reset
commands so that you can always restore the look of your document to the original contained in your current template. On the
Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these
galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. When you create pictures,
charts, or diagrams, they also coordinate with your current document look.
You can easily change the formatting of selected text in the document text by choosing a look for the selected text from the Quick
Styles gallery on the Home tab. You can also format text directly by using the other controls on the Home tab. Most controls offer
a choice of using the look from the current theme or using a format that you specify directly. To change the overall look of your
document, choose new Theme elements on the Page Layout tab. To change the looks available in the Quick Style gallery, use the
Change Current Quick Style Set command. Both the Themes gallery and the Quick Styles gallery provide reset commands so that you
can always restore the look of your document to the original contained in your current template. On the Insert tab, the galleries
include items that are designed to coordinate with the overall look of your document.
You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. When you
create pictures, charts, or diagrams, they also coordinate with your current document look. You can easily change the formatting
of selected text in the document text by choosing a look for the selected text from the Quick Styles gallery on the Home tab. You
can also format text directly by using the other controls on the Home tab. Most controls offer a choice of using the look from the
current theme or using a format that you specify directly. To change the overall look of your document, choose new Theme elements
on the Page Layout tab. To change the looks available in the Quick Style gallery, use the Change Current Quick Style Set command.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
macropod said:
Note: Sendkeys doesn't work with Vista.

--
Cheers
macropod
[Microsoft MVP - Word]


Doug Robbins - Word MVP said:
Use it this way:

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


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com


:

Use sendkeys to send the enter command
SendKeys "~"


Thank you for your respose.

However, replacing with
SendKeys MakeDummyText(OptionButtonLatinText.Value) + "~"

freezes Visual Basic and nothing new is displayed in the Word document.

Word Help on SendKeys states:
"The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses
( ) have special meanings to SendKeys."
but what these meanings are is not mentioned.

Have I misunderstood something?

Best wishes
Holger
 

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