Bryce Bassett
asked on
Word 2010 Save As Dialog under VBA control gives file name not valid error
I have a VBA macro in Word 2010 that lets the user carve out pieces of a document and save them to a Content Library. The user highlights a section of text, for example a product description, then runs the macro, which creates a temporary document, pastes in the selected text, then opens a Save As dialog. The macro grabs the first line of the selected text and suggests that as the filename to use in the Save As. Here's my code for the SaveAs part:
Works as expected. Here is the result:
If I just click Save (accepting the suggested filename), it tells me that the filename is not valid. Doesn't matter what the name is or how long the name is, I always get this error. If I shorten the filename and try again, same error.
Only if I completely delete the suggested name and type something new will it accept the name and perform the save.
Does anybody know what's going on and how to fix this?
'Get the present default filepath for documents
defpath = Options.DefaultFilePath(wdDocumentsPath)
'Change it to desired folder
Options.DefaultFilePath(wdDocumentsPath) = contentlibraryfolder
Application.ChangeFileOpenDirectory (contentlibraryfolder)
With Application.Dialogs(wdDialogFileSaveAs)
.Name = suggesteditemtitle
.Show
End With
'Change the default filepath for documents back to the original default
Options.DefaultFilePath(wdDocumentsPath) = defpath
Works as expected. Here is the result:
If I just click Save (accepting the suggested filename), it tells me that the filename is not valid. Doesn't matter what the name is or how long the name is, I always get this error. If I shorten the filename and try again, same error.
Only if I completely delete the suggested name and type something new will it accept the name and perform the save.
Does anybody know what's going on and how to fix this?
I think it can be related with your suggesteditemtitle , try some hard coded stuff instead of that variable, maybe we can narrow the problem by that.
Your code works OK for me. I use Word 2007, but I can't believe that there are any design changes in that area.
Try the general Word problems fix from Microsoft here:
http://support.microsoft.com/default.aspx/kb/921541
Try the general Word problems fix from Microsoft here:
http://support.microsoft.com/default.aspx/kb/921541
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Brilliant insight! I had noticed that carriage return in the error message but didn't put two and two together. I took your solution and added in this check:
If Right(suggesteditemtitle, 1) = vbCr Then
suggesteditemtitle = Left(suggesteditemtitle, Len(suggesteditemtitle) - 1)
End If
Thanks so much!
If Right(suggesteditemtitle, 1) = vbCr Then
suggesteditemtitle = Left(suggesteditemtitle, Len(suggesteditemtitle) - 1)
End If
Thanks so much!