Word .SaveAs2 pops error message (in some situations)

Posted on 2014-04-10
Medium Priority
Last Modified: 2014-04-21

please bear with me and read through the scenario.

I have a .docm file that lives in a SharePoint document library. When it is opened, a macro fires that prompts the user for a new file name and folder and then uses .SaveAs2 to save a copy of the file in the specified location as a .docx without the macro.

I cannot use content types with a proper .dotm, since there are over 30 different file templates in the library. I haveto use the .docm in a SharePoint library and then make sure that the user saves a copy of the file to their personal drive.

So I use some code in the open event. I let the user specify a folder and a file name. Then .SaveAs2 takes these parameters and saves the current file in the new path with the new name as a normal .docx file without macros.  I 'll spare you the details about how strFolder and strDoc are gathered. Rest assured that they exist. I have a debug.print with the full file name and it is correct.

With o
    .SaveAs2 strFolder & strDoc & ".docx", wdFormatDocumentDefault
End With

Open in new window

The problem is that this code brings up a message saying that the file cannot be found.
error message
Well, duh, I am trying to save the file in this location. Of course it does not exist. That's the point (let me assure you that the folder DOES exist).

Now the fun bit. After the message box is closed, Word happily saves the file to the specified location.

Another fun bit: The message only pops up if the original file is opened in Read mode from SharePoint AND if the new file path is on a network drive.

The message does NOT pop up if
- the file is opened in Edit mode (click the SharePoint file, select Edit in the next dialog), or if
- the file is saved to a local drive (C:\) or if
- the file is opened from the File > Recent backstage dialog.

In the production system, the users will not have a choice of Edit or Read only. They will default to Read only. Also the users will not be able to save to a local C:\ drive, since the business system puts their profile and "My Documents" on a network drive (H:\).

I have tried

- saving the file with a different approach: using msoFileDialogSaveAs  -- same message
- suppressing the message with Application.DisplayAlert = False. - Does not work. Message still shows
- suppressing the message with Application.ScreenUpdating  = False. Does not work. The message still shows.

Why does Read or Edit mode matter when the file is saved to a new location?

Why does the new location of the file (network path or local path) matter when it is saved?

And anyway, why does the message come up when afterwards the file saves correctly?

I don't need answers to these three why questions, I just want the message to go away.

Please try to repro the scenario before posting. Any suggestions?

cheers, teylyn
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
  • 8
  • 8
LVL 15

Accepted Solution

colly92002 earned 2000 total points
ID: 39995240
It might be the H: drive mapping on your client OS.  Which OS is it?

Have you tried saving it to a network URI path rather than a the mapped drive?  I.E. a \\server\folder  link to the same location, but without the mapping?

Alternative could you try saving it to your %TEMP% folder, then copying that to the correct location on H:  after the save?
LVL 50
ID: 39997846
thank you for your thoughts. The OS is Windows 7 Enterprise.  

Using a URI makes no difference. The message still pops up.

Everybody uses virtual machines. They are configured so that the user does not use and cannot see the C: drive.

I don't want to save in one place and then have to copy to another. Also, I'm not sure if that would work. The .docm has the code and it is saved with a different name to a new path. How can the VBA then copy the file to another location? Do you have some sample code that would do this?

LVL 15

Expert Comment

ID: 39997874
Can you save it to any of your other mapped drives successfully, or does it always give the message no matter where you try to save it?
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

LVL 50
ID: 39997987
Nope. Any network path triggers the message (if the file is opend in Read mode).
LVL 15

Expert Comment

ID: 39999318
Sounds like it might be a bug.  Are there any other events you can attach to rather than the open event?  Perhaps you could set a flag in the open event and do it later?

Sharepoint/Office integration causes me no end of problems.   Sharepoint use javascript to launch Office using Active X and this is probably what is at fault.
What version are you using?  Have you tried other versions of Office?

I might be worth changing the web app to use permissive mode which does not send so many headers and lets the client deal with it (http://sharepointsoldiers.wordpress.com/2011/11/03/permissive-mode-in-browser-file-handling/).
LVL 50
ID: 40000391
We're on SharePoint 2010 with Office 2010. No other versions in the corporate environment. The web applications is already set to permissive browser file handling.

Ah well, maybe it _is_ a bug. I'll leave this open a while longer. Maybe someone else can come up with something to suppress the message.  That's really all I'm after.
LVL 15

Expert Comment

ID: 40000438
Office 2010 opened in view mode from Sharepoint 2010 will give the user a notification saying that the file is "read only" and an option to "check out for editing".   I would guess that immediately saving the file to a new location might be thrown out by this somehow.

Try to see if it happens out side of a sharepoint environment, i.e. just host the .docm on one of your shares.  If that works then you could just have a link to the share from Sharepoint rather than hosting the document.

If you really want in in sharepoint (and the above works, suggesting the problem is with Sharepoint opening the document using Active X), then you could override document handling in the Sharepoint site.  You can do this using javascript attached via a masterpage.
LVL 50
ID: 40001230
Whoa! Let me digest this.

>> Try to see if it happens out side of a sharepoint environment,

Won't do. The file is hosted by SharePoint because the file is a specific Content Type. The columns of the Content Type are used as content controls in the document to set metadata like "Start date" or "Other party" . The user fills in the content control in-line without realizing that they are setting metadata values.

Eventually, the file that is saved by the user will be uploaded into a SharePoint Document Library that uses the same columns and then kicks off workflows and all kinds of stuff based on the values in those properties.

I've tried Hosting the files somewhere else, but that does not work for our scenario. All the 30 or so different template documents are stored in a SharePoint library. They are tied to a content type which is also used in a different SharePoint doc lib that will house the final edited files.

You suggest that the problem might lie with SharePoint opening the document using ActiveX. I'm more than happy to pursue that line of thought. You suggest that document handling could be overridden with Javascript. Now, I am fine with writing simple js scripts and some more elaborate jQuery. I understand how it all hangs together (I think), but I'm kind of lost to even come up with an idea of what kind of js would be required to override SharePoint document handling.

If you have any pointers towards working code for this situation, I'd be very interested.

cheers, teylyn
LVL 15

Expert Comment

ID: 40001259
OK first I would simply save the document to the file system and then open it from there (i.e. save it to the file system, close it, then double click on it from an explorer) to see if your method attached to the "open" event fires and works correctly without the popup message.

If you still get the message then its a problem with Word 2010 and your best bet is either to raise it with Microsoft as an issue or try to re-design you concept to do this in a different way (perhaps a Sharepoint delegate control on your masterpage could do it rather than doing it from Office directly?)

If it works then we know its the Sharepoint/Office integration and I would be more than happy to show you how to override the file handling in SP.
LVL 50
ID: 40005082
I can confirm that the message does NOT pop up if the file lives on the file system. It also does not pop up if the file lives in SharePoint but is opened from the "Recent" file list. The issue seems to lie with the document being opened in "Read" mode in combination with saving it to a network drive.

The rationale behind the design concept is as follows: The library contains templates for dozens of different business scenarios. The user opens any one of these templates and must then save it in a different location. They will edit their copy and eventually upload it to another SharePoint library, where an approval process will be kicked off. The approval process requires certain metadata. The template library and the final document library have the same content type. Columns of the content type are in the Word document as content controls, so by editing the document and filling in the content controls, the user defines the document metadata that is essential for the workflow.

I don't want the user to manually save the file in another location, because the default folder that comes up is the templates library that they don't have write permissions for. I'd rather they see their home drive instead.
LVL 15

Expert Comment

ID: 40005387
I can confirm that the message does NOT pop up if the file lives on the file system.
I think that confirms that it is the Sharepoint JS that opens the file in read only mode that is the culprit, as suspected.

Having though about it the absolute easiest thing to do is to just put a link to the file somewhere in your masterpage - i.e. edit the masterpage in SPD, and somewhere (perhaps in the titlearea - that's the bit I like to use to put delegate controls into) you can put a HTML anchor (<A href="" \> etc) in it called "Create new document to my share" or something, and point that to the .docm hosted in your Sharepoint envrinment.  This will simply open the file without ANY processing (it's the list webpart that puts all the clever active X processing in place).

This is pretty simplistic but you could create a button, or you could even create a full delegate control to do it if you want to go that far.

If you really want to be able to do this from a list webpart then I can show you how to override the sharepoint "open for view" but that sounds like too much work for scenario you are describing as you have to overide code in CORE.JS (and you might have to use jquery to intercept clicks too) so its much more complicated.
LVL 15

Expert Comment

ID: 40006150
For an even simpler solution (or perhaps a protoype), you could even create the link in a Content Editor Web Part if you are using web part pages.   If you do this on a page with a standard list web part you will lose the breadcrumb trail,  so beware of that.
LVL 50
ID: 40008082

I've used a link to the document in a CEWP before. The problem with that is that the document opens in protected mode and the code runs before the document is opened, so VBA errors out when I create a reference to the document object, saying that no object exists. If I debug the code, and end it, it gets to the end of the code AND THEN loads the document content on the screen.

The protected mode is applied because the file is a macro-enabled file. This mode gets applied DESPITE the fact that all our SharePoint sites are in the intranet zone and should be trusted. Googling this I found that the only way to run the macro from the linked file would be to turn allow running all macros, so it does not get opened in protected mode. This is of course out of the question. Our network and server folks will laugh me out of the door if I suggest that they change the GPO settings to allow any and all macros to run without checks or user intervention.

It's not as if I haven't tried a few things already. That's why this is so frustrating.

I'm happy to use jQuery to override any native JS if that means the document can be opened in a way that the macro runs and the message does not appear. But if the JS override will achieve the same effect that can be created with a simple anchor link to the file, then that won't help either.

The only way I found the message does not appear is if the file is opened in EDIT mode and the users won't have edit permissions on the library. So even if native SharePoint JS is replaced with some other JS, that still does not get the user into Edit mode, will it?

Can we not jut make the message go away from inside Word's VBA? That's all I want really. Everything runs, everything saves. Everything is fine. Just the message gets in the way.
LVL 15

Expert Comment

ID: 40010384
Have you tried setting up a list with no versioning enabled and check out disabled and hosting the file in that list?  This should in effect open it "for edit".
LVL 50
ID: 40013763
Thank you for trying, but there are a few things that need to be in place and that includes versioning.

Incidentally, a new test library with no versioning and no checkout required still lets me choose between read only and edit. If I select read only, I get the error message.
No user with read only access to the library will ever be able to open the document in Edit mode.

Can you reproduce the problem at all? If not, I don't think there is much value in asking me to try things, unless you have some evidence that they work.
LVL 50
ID: 40014029
Eventually, this one did the trick.  Even though the user does not see their C drive, their temp folder actually resides there and not on the H drive. So I could do this:

' user has specified file name (strDoc) and final destination folder
' since we get an annoying message when trying to save to a network drive while
' in read only mode, we first create a new, empty file in the user's temp folder,
' then copy that empty file to the specified folder
' set the temp folder and full path
tempFolder = Environ("Temp")
tempPath = tempFolder & "\" & strDoc & ".docx"
' create a new document
Documents.Add DocumentType:=wdNewBlankDocument
Set oo = ActiveDocument
ChangeFileOpenDirectory tempFolder
' save to temp folder and close
With oo
    .SaveAs2 tempPath, wdFormatDocumentDefault
End With
' copy from temp folder to previously defined destination
FileCopy tempPath, fullPath
' delete the temp file
KillFile = tempPath
' finally, save the contract over the empty file
With o
    .SaveAs2 fullPath, wdFormatDocumentDefault
End With

Open in new window


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

764 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