Word VBA .SaveAs2 pops message (in some situations) - vba

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.
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?

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

Here's a shot in the dark based on some googling and similar experience:
http://blogs.technet.com/b/wordonenotesupport/archive/2009/02/07/word-2007-file-save-errors-an-error-is-displayed-when-attempting-to-save-a-word-document.aspx
The relevent bits:
"<path and file name>" cannot be found. Check your spelling, or try a different path.
<path and file name> is currently in use. Try again later.
Word cannot complete the save to due to a file permissions error.
You might see the aforementioned errors if Word 2007 is having a
conflict with antivirus software. Most of these conflicts have been
addressed by the antivirus software manufacturers, but you must
download and install updates or patches for the antivirus software
(not just new virus definitions) to resolve the issue. See the
software manufacturer's web site for possible updates and for
knowledge base articles concerning configuration issues.
...and then from McAfee describing a related issue:
https://kc.mcafee.com/corporate/index?page=content&id=KB75449
Most forum discussions I found implicate server-side realtime anti-virus scanning - something about the interplay of behavior between the saving mechanism of Office 2010 not playing nice with live, access-based file scanning.
Even if this doesn't solve your issue, hope it helps!

Related

Why can the Word file my script created and saved not be opened

I developed a VBA script in Access that:
reads client specific information from my database
creates a Word file from a Word template containing this information
saves the file in .docx format in a local folder.
Everything works fine on my PC, but an exact copy on my laptop not.
So I have a PC and a laptop with an identical copy of the frontend of the database and the Word template. Both frontends are attached to a backend on a network drive.
The script works fine on the laptop too, however the thus saved file can't be opened (not on my laptop an neither a copy of it on my PC). The content can be read in the explorer preview pane though.
On trying to open the document either:
the Word opening screen pops up only shortly and nothing happens
after that, or
Word opens without a document loaded.
My conclusion is, that the saving proces on the laptop corrupts the file.
I can not figure out why?
Both PC and laptop run windows 10 and Office 365).
Here is the code that saves the document:
Public Function BestandOpslaan(ByVal Bestandsnaam)
Dim fDialog As Office.FileDialog
Set fDialog = Application.FileDialog(msoFileDialogSaveAs)
With fDialog
.Title = "Sla het bestand op"
.ButtonName = "Opslaan"
.InitialFileName = Bestandsnaam
If .Show <> 0 Then
BestandOpslaan = .SelectedItems(1)
End If
End With
Set fDialog = Nothing
End Function
Has anyone a clue?
It may be that when you transported the Word template file over to the other computer, Word is blocking Access from opening it properly in the first place because of potential internet safety concerns.
On the laptop where this is not working, try opening the Word template file, and if there is a bar across the top warning you that the file is from the internet and that it is currently opened in a restricted mode, click the button to enable all features so that the template will operate normally.
I found the solution:
On the laptop the default-save-as format was set to ".docm"
So the script saved the file with the extention ".docx" as a document with macro's enabled "docm".
Changing this default setting in word (under file | options | save | save files in this format) to ".docx" solved the problem.

Print button didn't save the PDF file at a shared path, and no error logged

I have an Excel macro that uses SAP for printing data in pdf format.
Steps:
it access the SAP transaction which provides a table with the necessary information
it press Print button (from the Menu Bar of SAP)
then Print window appears (from here it is selected Microsoft Print To PDF option and then it is pressed OK button)
Save As window appears (complete the path and filename: \S\BC....\outputName)
then Save button it is pressed
then no error happens
But if I access the path folder "\S\BC....", nothing happen, there is no file saved. BUT, if we look in "Date modified" property of the folder, it is modified in accordance with the last saving time.
Also, I mention that if I'm trying to write the pdf file on local folders, not network shared folders (example: a desktop folder: C:\Users\mariah\Test), macro works.
Also, I mention that user can Read&Write at the shared path \S\BC....\
Please help me to find the cause of this issue.
A VBA macro only does what you are allowed to do manually. Can you manually save the file in the shared path without the macro?
Is the common path really \S\BC...\ or \ \S\BC...\ ?
Regards,
ScriptMan
Solution proposed by the OP in this comment:
IT WORKS if I save the file on "C:\Users\UserName" and then cut it to shared path "S\BC...". So I've implemented code that saves the file in a path and then copy it to another path and then I deleted it from the first path (such that user never knows that I copy the file in other destination than desired destination).

Obtain the "absolute path to the workbook" from an .xls file

When I use the Excel "Document Inspector" on a particular .xls file to check for "hidden properties or personal information" it says:
The following document information was found:
* Absolute path to the workbook
How can I obtain the absolute path of the workbook from the file? If it needs to be done programmatically, I could use Java (e.g. Apache POI) or VBA.
I know where the file is currently saved, but what I want to extract is the absolute path to the workbook which is saved in the file I have. This is so I can know where it was saved by the author.
Here's what has happened to the file:
Someone authored it, saving it at some absolute filepath unknown to me
They uploaded it to a website
I downloaded it from the website
Excel indicates that the document contains the absolute path from step 1. I'm after this path, not the place I saved it at step 3 since I know that.
I can reproduced that warning message by simply creating an empty Excel file, added a formula, saved it as BIFF8 (.xls). The Document Inspector will then warn about the absolute path. ... but in my case, there was no filename inside the file.
A simple way to verify this, is to open the file in a hex-editor and search for a well-known save location (i.e. the location where a dummy/test file was stored) - this is either stored as ASCII or as 16-bit string, i.e. every odd byte is a character.
If you want to use the POI developer tools, you can use the following:
To list all Excel records:
java -cp poi-3.16-beta1.jar org.apache.poi.hssf.dev.BiffViewer file.xls
To list the document and summary properties:
java -cp poi-3.16-beta1.jar org.apache.poi.hpsf.extractor.HPSFPropertiesExtractor file.xls
To list any embedded objects beside the usual suspects SummaryInformation, DocumentSummaryInformation and Workbook:
java -cp poi-3.16-beta1.jar org.apache.poi.poifs.dev.POIFSLister file.xls
So after running the tools and recording the output, you can then remove the properties via the Excel Document Inspector and execute the tools again. The output can be diffed and you might find the culprit.
Assuming it is an .xlsx file rather than an older-style .xls file, you can
Rename the workbook as a .zip file
Look at the xl\workbook.xml "file" within the .zip file
and you will find the absolute path when last saved from Excel.
This is why it is not a good idea to share work-related spreadsheets with other people, unless you first clear out this sort of information.
I'm not sure how to find it in the binary format files.

Getting contents of Outlook email attachment from networked computer

I had needed to get the attachments from an email draft so that I could check if they were an Excel file, and if so read through the file to copy/paste a range of certain text into the body of the email.
Thanks to an answer from my previous question I've figured out how to get the email attachments.
I'm working on how to get when an attachment is added to that specific email draft, but the more pressing issue is that once I've added something, how do I open it in Excel?
Dim NewMail As MailItem, oInspector As Inspector
Set oInspector = Application.ActiveInspector
Dim eAttachment As Object
'~~> Get the current open item
Set NewMail = oInspector.CurrentItem
Set eAttachment = CreateObject("Excel.Application")
With eAttachment
' Change file name to suit
.Workbooks.Open FileName:=NewMail.Attachments.Item(1).FileName
End With
This tells me that the file doesn't exist. So I look at the pathName on the attachment and find that it is set to nothing. There is no text there.
I'm thinking this has something to do with the Excel file being attached is on a computer networked to the one I am using.
I've searched, but "get contents of outlook email attachment on networked computer" didn't net me the results I wanted.
How would I gain access to the workbooks of the attached Excel file? Please note my example only uses the first attachment because for testing I am only attaching the one Excel file. When I can get the Excel file to open I will check the attachments to ensure they are excel files before I open them.
Edit: I just copied the file over to my local hard drive and tried to open the file, same issue. Am I going to have to open the file temporarily to open it? Is that what Outlook does when you edit an email attachment?
In case someone has the same issue I did - Here's what I've come to the conclusion of:
the PathName object, perhaps it is just the version of outlook that I am using, but it stores absolutely nothing. I tested it out on both of the computers in my office and my one at home, with the same result: it is just not there. Each Attachment object will have a SaveAsFile method that you can use to save the file to the temporary folder and access it from there via the usual Excel applications. This seems to be the only way, unfortunately, to read through the contents of a file attachment, even when it is simply a draft copy you are writing.
Also, what got me was the fact that I was trying, in the Excel file, to find the last cell in use, and was using the .End(xlUp) method. Remember, if you are using constants defined in the program you use, it is not defined in another. E.G. I was opening this Excel file from Outlook, technically, so trying to use xlUp gave me errors. Simply open up Excel, Word, or what have you to check the value of such constants and set them in your program.

Make my applescript process specified files in the current directory?

I need to generate PDFs from word files regularly, and I'm getting tired of doing it by hand.
Manually, all I do is open a file, and click "Save as PDF". So, one would think applescript would be a nice easy way to do this. [If you have another approach than applescript, I'm open to it.]
I'm almost there, the following script works, except the full path is hard coded.
tell application "Microsoft Word"
open file "Macintosh HD:Users:me:repos:training:class:Activities:ActivityGuide.docx"
set doc to document "ActivityGuide.docx"
save as doc file name "Macintosh HD:Users:me:repos:training:class:Activities:ActivityGuide.pdf" file format format PDF
end tell
I need it to work for other people, on other machines, so the username and other parts of the path may change. If I could just do this from the current directory for the script, I'd be set.
I found this: Applescript to launch file from current folder?
tell application "Finder"
open file "somefile.txt" of folder of (file (path to me))
end tell
Which works for opening an app from the current directory in Word, via Finder, but I think I need to open it with an application of "Microsoft Word", if I'm going to use "Save As". But this method of opening doesn't work if I change the application for "Finder" to "Microsoft Word".
Any suggestions welcome.
[edit: clarity]
Try this
set x to path to me
tell application "Finder" to set tFile to (file "ActivityGuide.docx" of (container of x)) as alias
set newFile to (text 1 thru -5 of (tFile as string)) & "pdf"
tell application "Microsoft Word"
open tFile
tell document 1
save as file name newFile file format format PDF
close saving no
end tell
end tell
Another thing that might be helpful to you is under System Preferences:Keyboard:Shortcuts: App Shortcuts:All Applications
Make a shortcut that is named "Save as PDF..." assign the shortcut to command+P since most applications that is the shortcut for print, then just press it twice and it will prompt you to save it as a PDF (I got that tip from David Sparks) and depending on the application it will default to where the file was opened up from. You can also set that with the application Default Folder X.
Hopefully that is a bit more of a global solution for other problems but it looks like you already have the answer to this isolated problem.