Saving file Maro issue

S

Steve_n_KC

I am using the code listed below (Which works like a top thankyou! Got it
from here) to save a file with a new name based on User input into 2 Input
boxes.

Is there a line or 2 of code someone could help me with to exit the sub if
the user either doesn't enter a response into one of the Input Boxes or
selects Cancel?
I tried using a reapplied exit sub line from one of my borrowed Excel macros
but couldn't get it to work.
As it stands now, the macro executes anyway and I end up with a bad file
path or name.
Sub SAVENEW()

ActiveDocument.Shapes("Text Box 2").Select
Selection.Delete

Dim strFilename1 As String
Dim strFilename2 As String
Dim strFilename As String

strFilename1 = InputBox("Department Name MUST BE ....", "Enter Dept. Name")

strFilename2 = InputBox("OPL Title", "Enter Title")
strFilename = "L:\KCS LIBRARY\OPL\" & strFilename1 & "\" & strFilename2 &
".OPL.doc"
ActiveDocument.SaveAs strFilename
ActiveWindow.Caption = ActiveDocument.FullName
End Sub
 
J

Jay Freedman

If you want to simply exit the macro, you can do this:

strFilename1 = InputBox("...")
If Len(Trim(strFilename1)) = 0 Then Exit Sub

strFilename2 = InputBox("...")
If Len(Trim(strFilename2)) = 0 Then Exit Sub

It would be more user-friendly to let the user have another chance to
enter something (for example, if they slipped and hit Enter by
mistake) instead of forcing them to run the macro again -- which,
depending on what the macro does, might mess up the document or throw
a run-time error. There are a couple of better ways, one involving a
While...Loop structure, and another using a UserForm instead of an
InputBox.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
S

Steve_n_KC

Thank You very much! You are exactly right with the issue being caused by
exiting the macro midway. I had to change around the order of operation to
fix that. I have had absolutely no luck in working with UserForms (many
hours of frustration) so I end up cutting and pasting code from an Excel file
where I keep it all. Primitive but functional.

Thanks again for your addition to my "Library"!
 

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