Run Time error on Code

A

Angyl

I'm using a bit of code to save a document as and automatically populating
the file name with form field data.

Seems to work fine...the Save As Dialog appears, the proper field names are
there for the save as, but then if you click "Save" I get a run error

"Run-time error '-2147(bunch of other numbers)'
Command failed

And that's it. Any idea what I'm supposed to do to fix this? here's the
code I'm using:

response = MsgBox("Form Complete! Do you want to save this survey now?",
vbQuestion + vbYesNo)
If response = vbYes Then
Dim dlg As FileDialog
Set dlg = Application.FileDialog(msoFileDialogSaveAs)
With dlg
.InitialFileName = "c:\Documents and Settings\My Documents\Survey -
" + ActiveDocument.FormFields("textLast").Result + ", " +
ActiveDocument.FormFields("textFirst").Result + ".doc"
If .Show Then .Execute
End With
End If
 
R

Russ

I might try inserting this after building the string.
Msgbox "<<" & .InitialFileName & ">>"
To see if what is between the chevrons is a legitimate filename without
rogue characters not allowed in a dos path.

Also I try to use a ampersand &, not plus +, to concatenate strings so I
don't accidentally do math instead.
 
F

fumei via OfficeKB.com

I can not duplicate this. i get no errors running:

Dim response

response = MsgBox("Form Complete! Do you want to save this survey now?", _
vbQuestion + vbYesNo)
If response = vbYes Then
Dim dlg As FileDialog
Set dlg = Application.FileDialog(msoFileDialogSaveAs)
With dlg
.InitialFileName = _
"c:\Test\Survey -" _
& ActiveDocument.FormFields("textLast").Result _
& ", " & ActiveDocument.FormFields("textFirst").Result & ".
doc"
If .Show Then .Execute
End With
End If
 
D

David Sisson

The help file on '.InitialFileName ' also states:

Setting this property to a string longer than 256 characters will
cause a run-time error.

Along with Russ' suggestion, you might try

Msgbox( len (.InitialFileName))

....just in case something screwy is going on.
 

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