VBA OneDrive Access - vba

I have an excel file tree.xlsm which I have stored on my personal OneDrive subfolder Tree.
One of the requirements is for the VBA to display a photograph.
The folder has been shared with certain friends and so the path needs to be a URL so they can access it as well.
The code below is used to display the photograph.
sPath = Application.ActiveWorkbook.Path
Image1.Picture = LoadPicture(sPath & "\1001.jpg")
The VBA code crashes with Run-time error
75: Path/File access error.
If sPath & "1001.jpg" is copied into windows explorer the photograph is opened.
I think the error is caused by a OneDrive access rights issue.

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.

how to add file open password to excel file by VBA

i have a VISUAL BASIC code that creates a copy of the EXCEL file in a different location.... i need a code to password protect the copied file in different location from opening by other users.
The Workbook.SaveAs method includes the ability to add a password to the saved workbook. See the Office Dev Center reference page.
You the SaveAs method on the workbook and use the Password option
dim wb as workbook
rest of code
wb.SaveAs Filename:="your_filename", Password:="xxxxxx"

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

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.

Relative file references

In have built a couple of mda library files which I am then referencing from my main Access application (i.e. using Tools -> References from within the IDE).
Is there a way that these references can be made relative rather than absolute. The reason I am asking is so that it would make it easy to set-up on the user's computer if all three files (main application and two mda files) could simply be placed in any directory and work without having to change the references...
Thanks
Why not just place the three MDEs in the same folder on the target system? Access should find the MDE references just fine.
Or are you using the add-in logic with the USysRegInfo table? You don't really need to do that with your own add-ins. Just with developer type add-ins such as Rick Fisher's Find and Replace.
If this isn't working for you then tell us what error messages or symptoms.
To get the file path for the access application
CurrentProject.Path & "\"
Then just add the other files into the same directory and get them by name. i.e.
Dim filepath As String
filepath = CurrentProject.Path & "\name_of_file.mda"
You can add VBA references through VBA itself.
Dim sFilename As String
sFilename = CurrentProject.Path & "\" & whatever.mda
Application.References.AddFromFile sFilename
Just put that in your AutoExec and that reference ought to be available for everything. Of course, you'll have to check if the reference already exists before adding it, otherwise you get an error. But that's just a matter of looping through Application.References.