Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Word VBA .SaveAs2 pops message (in some situations)

  • SharePoint 2010
  • Word 2010
  • Windows 7 Enterprise

I have a .docm file that lives in a SharePoint document library. When it is opened, a macro fires and 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 SP content types with a proper .dotm as the template, since there are over 30 different file templates in the library. I have to 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

The problem is that this code brings up a message saying that the file cannot be found. enter image description here

Well, duh, I am trying to save the file in this location. Of course it does not exist. That's the point. (Note that the folder does exist.)

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

Also, 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 (I know but I was desperate) or wdAlertsNone. - Does not work. Message still shows
  • suppressing the message with Application.ScreenUpdating = False. Does not work. The message still shows.
  • suppressing the message with error handling On Error Resume Next or On Error Goto MyHandler but the message pops up without the error handlers being fired. The message has the blue "i" icon, so maybe it is not interpreted as an error, but as a piece of information.

How do I make the message go away?

Also, although this is not essential, it would be nice to know:

  • 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 why does the message come up when afterwards the file saves correctly?
like image 806
teylyn Avatar asked Apr 11 '14 07:04

teylyn


1 Answers

After several futile attempts to change the way SharePoint serves the document in read only mode, I used the following approach to create a new Word doc, save it to the user's temp folder, copy the doc from the temp folder to the folder previously specified by the user. Now the document exists and using SaveAs does not trigger the error message.

Before the code below runs, the user has defined a file name (strDoc) and a folder for the document to be saved to.

' 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
ChangeFileOpenDirectory tempFolder
' save to temp folder and close
With ActiveDocument
    .SaveAs2 tempPath, wdFormatDocumentDefault
    .Close
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
like image 97
teylyn Avatar answered Nov 15 '22 07:11

teylyn