Loops are making me loopy

J

Janice

Hi - I've tried for two days to make this work, can you
help?

I need to "assemble" a document based on paragraphs of
text I have stored in documents. I'm using the insert
file feature to do this. The problem I'm having is I need
it to keep prompting for a doc name (which is a number)
until I type "9999" which will signal the end of the
loop. Here's my code, I've deleted my loopy mess. Thanks!

Sub InsertParas()
'
Dim pName As String
pName = InputBox("Enter filename")
If Len(pName) > 0 Then

Selection.InsertFile FileName:="c:/willform/0000" &
pName

Selection.EndKey unit:=wdStory

' (ok, now ask me again for another pname)

End If

' (ok, end the whole thing if I type "9999")
If pName = ("99999") Then

Exit Sub

End If

End Sub
 
J

Jezebel

Better is like this:

Sub InsertParas()

Dim pName As String

Do
pName = InputBox("Enter filename")
If len(pName) = 0 Then
Exit Do
End if

Selection.InsertFile FileName:="c:/willform/0000" & pName
Selection.EndKey unit:=wdStory
Loop

End Sub
 
C

Chad DeMeyer

Also please note that using Jezebel's code, the loop is not exited by typing
"9999" but by clicking Cancel, or by clicking OK when nothing is typed.

Regards,
Chad
 

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