Create new folder in the same folder where the current file is stored (in sharepoint) - excel

I want to run the below VBA to create a new folder within the folder where the current file is stored. So far I have done this using the following simple VBA:
Sub Create_Folder()
MkDir Application.ThisWorkbook.Path & "\New Folder"
End Sub
Now, my company moved to sharepoint and when I run this VBA within a sharepoint folder I get runtime error 76.
I assume this is caused because ThisWorkbook.Path can not correctly identify the path anymore. Therefore, I checked how the path looks like:
https://company.sharepoint.com/sites/Finance1/Freigegebene Dokumente/Controlling/
In order to use this path I adjusted it in the VBA:
Sub Create_Folder()
WorkbookPath = ThisWorkbook.Path
WorkbookPath = "\\" & Replace(Split(WorkbookPath, "//")(1), "/", "\")
MkDir WorkbookPath & "\New Folder"
End Sub
However, I am still getting runtime error 76 when I try to create the new folder.
What is strange to me is that when I check the link in the windows explorer it loosk totally different to the link that I get with Application.ThisWorkbook.Path.
C:\Users\firstname.lastname\company\Controlling - Dokumente
Do you have any idea how I can solve this issue and create a new folder in the same folder where the current file is stored?

Related

'Save As' of object '_workbook' failed

I am trying to save a workbook as xlsm, this is the code I use:
ThisWorkbook.SaveAs Filename:="path\workbook_name.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
I have done some testing, and It does in fact save in both a private folder and a public or shared folder
however when I save it in a shared folder I also get this:
Run-time error '1004' Method 'Save As' of object '_workbook' failed
why do I get error message when it is saved in a public/shared folder, or onedrive?
In your string containing path there is variable "path" i suppose. Then you should write it like this:
Dim path as String: path = "C:\Users\xxx\Desktop"
ThisWorkbook.SaveAs Filename:=path & "\workbook_name.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
Or you can hardcode full path (but i recommend using variables - first example).
ThisWorkbook.SaveAs Filename:="C:\Users\xxx\Desktop\file.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
Then when you can save it manually and you have permissions then i have no idea since i normally save files to shared folders and network drives... Maybe your specific path is wrong. It is best to save using servername and folders...
path = "\\servername\folder1\folder2\workbook_name.xlsm"
Because sometimes when you refefer to mapped drive for example X:\folder2 then you must ensure that all users have it mapped in same way (someone uses X someone uses R etc.). And if path is correct then only other way i can think of it is permission issue, but if you can save it manually then i have sadly no idea... :)

Do the DSOFile functions only apply to the non-binary Excel Document types?

Using Windows 10 (Build 1903 if that's relevant?) and 64bit Office 365 (probably relevant?) I've implemented a system that allows me to version control Excel VBA code.
I'm using the Workbook_BeforeSave method to check whether the current file is saved or not, and if it is saved, where it is saved to.
This works fine and will prompt the user as to whether they want to update the code contained within. I then thought that maybe I should in fact check if the code "needs" to be updated prior to prompting the user.
First off, I found the following question/solution: Using VBA to read the metadata or file properties of files in a SharePoint doc library
which I couldn't use without DSOFile.dll that I was able to install from here:
https://www.microsoft.com/en-us/download/details.aspx?id=8422
Here follows the code I have which doesn't work:
Private Function CheckTemplateIsNewerThanCurrentFile(ByVal templatePath As String) As Boolean
Dim templateName As String
Dim fso As New FileSystemObject
templateName = ActiveWorkbook.CustomDocumentProperties("TemplateName").Value
If fso.FileExists(templatePath & "\" & LocalTemplateName) Then
Dim objDSO As New DSOFile.OleDocumentProperties
objDSO.Open templatePath & "\" & LocalTemplateName, True, dsoOptionDefault
If Not objDSO.CustomProperties("LastCommitDate") = ActiveDocument.CustomDocumentProperties("LastCommitDate").Value Then
CheckTemplateIsNewerThanCurrentFile = False
Else
CheckTemplateIsNewerThanCurrentFile = True
TemplateLastCommitDate = objDSO.CustomProperties.Item("LastCommitDate")
End If
End If
End Function
And here (highlighted) is the error I receive trying to run the method above on an .xlsb file:
(FWIW: the reason for use of the .xlsb format is because we're working with 500K+ rows of data in the process we're carrying out. Yes, I know Excel is ABSOLUTELY NOT the tool for this but we're lumbered with it now)
I know I could have already tried changing the file format to .xlsm but because this file is version controlled that is a pain to do if the method is still likely to fail.
Thanks in advance,
Alex.

Open specific Folder in excel using vb.net in VisualStudio

I have to update an add-in that not written by me and i have to add a button that opens the specific path. I have no idea about this add-in. I could just add the button...
Private Sub RBOpenFolder_OnClick(sender As Object, control As IRibbonControl, pressed As Boolean) Handles RBOpenFolder.OnClick
End Sub
....\AppData\Local\AppName I have to access this address but the path changes on each pc has this add-in.
So in my computer C:\Users\Taylan\AppData\Local\AppName
Regards
If I am understanding this correctly, you are trying to access the user's local AppData folder via your application.
To achieve this, simply use the Environment.GetFolderPath and specify the local application data folder:
Dim FolderNameAs String
FolderName = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
In your case, this would return:
C:\Users\Taylan\AppData\Local\
You will then need to append the remainder of your path to this, so add AppName:
FolderName = System.IO.Path.Combine(FolderName, "AppName")
The FolderName variable will then contain:
C:\Users\Taylan\AppData\Local\AppName

custom function in excel using vba that works in any matchine

I have created a custom function via vba in excel. If I use it in my computer, it works ok, but if I change the file to another computer (where this computer also has the created function), it does not work. I must change the path of the created function. Is there any way to not change the path everytime I copy the file into another computer?
='C:\Users\Usuario1\Documents\Complementos\BondsTIRMDuration.xlam'!TIrbonds($A2;F2;'C:\Users\Usuario1\Documents\Complementos\AsBusinessDay.xlam'!asbusinessday('C:\Users\Usuario1\Documents\Complementos\AsBusinessDay.xlam'!PrevBusinessDay(HOY())))*100
Solution 1: You could use a common paths in both computers
(for example: C:\work , C:\Work2)
Solution 2: You could put all files in the same path (C:\work), then you only need the to put the file name
='BondsTIRMDuration.xlam'!TIrbonds($A2;F2;'AsBusinessDay.xlam'!asbusinessday('AsBusinessDay.xlam'!PrevBusinessDay(HOY())))*100
Just save your add-in in the correct path on every computer.
It should be something like:
C:\Users\YOURNAME\AppData\Roaming\Microsoft\AddIns\
See Install and Use Excel Add-ins to determine the correct path.
If your add-in is installed correctly you should be able to run your user defined function without a path.
You can call special folder with application.
MsgBox Application.DefaultFilePath
This example will be: C:\Users\Usuario1\Documents
'Here are a few VBA path functions
MsgBox Application.Path
MsgBox Application.DefaultFilePath
MsgBox Application.TemplatesPath
MsgBox Application.StartupPath
MsgBox Application.UserLibraryPath
MsgBox Application.LibraryPath
You can too create wscrit object to call another paths, for example:
MsgBox CreateObject("Wscript.Shell").SpecialFolders("Desktop")
Example folders for Wscript.shell object:
AllUsersDesktop
AllUsersStartMenu
AllUsersPrograms
AllUsersStartup
Desktop
Favorites
Fonts
MyDocuments
NetHood
PrintHood
Programs
Recent
SendTo
StartMenu
Startup
Templates
And execute a macro like this,(allways have to use same directory):
Sub Macro()
AddIns.Add Filename:=Application.DefaultFilePath & "\Complement.xlam"
AddIns("Complement").Installed = True
End Sub

Trying to access other files in same folder but getting files outside folder

I'm trying to access all the other files in the same folder as the program but it wants to go to files in the entire directory. I get that's basically what I'm telling it to do but I'm still new to vba and don't know the syntax to accomplish what I want. Is there a way that I can get the current folder location from the program and have it go through all the files only in that folder?
Also I'd need a solution that doesn't use the Scripting.Object stuff since I'm running into issues with needing to downloading stuff for that and I feel uncomfortable requiring the people that will use this tool download things too especially since they'll be on secure .mil computers (as opposed to .com computers)
directory = ActiveWorkbook.Path 'Also tried CurDir()
fileName = Dir(directory & "*.xl??")
Do While fileName <> ""
...
fileName = Dir()
Loop
Try
ThisWorkbook.Path
MsgBox ThisWorkbook.Path ' Make sure its the right path...
and
Filename = Dir(directory & "\" & "*.xl??")

Resources