Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Word 2010 Save As Dialog under VBA control gives file name not valid error

Posted on 2013-12-12
4
Medium Priority
?
2,032 Views
Last Modified: 2013-12-12
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:

'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

Open in new window


Works as expected.  Here is the result:
save asIf 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.  
error messageOnly 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?
0
Comment
Question by:Bryce Bassett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 17

Expert Comment

by:Cem Türk
ID: 39714269
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.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39714295
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
0
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 2000 total points
ID: 39715293
I think the problem is where you are gathering the first line of text to create the name. You don't show this code, but I suspect it includes the carriage return character - look at your error message: the .docx part is on a new line. Try adding a line like
suggesteditemtitle = left(suggesteditemtitle, len(suggeteditemtitle) - 1)

Open in new window

before you save, or modify the code that selects the first line to strip off the carriage return (or paragraph marker)
0
 

Author Closing Comment

by:Bryce Bassett
ID: 39715808
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!
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
With its various features, Office 365 can not only help you with your day-to-day business tasks, it can also do wonders for your marketing campaign.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question