I am trying to determine if a Sharepoint folder exists in Excel VBA using the URL path and if not create the folder. I can do this easily if I map the network drive:
myWorkbookBasePath = "Z:Documents\Reports\2013\"
If Dir(myWorkbookBasePath, vbDirectory) = "" Then
MkDir myWorkbookBasePath
End If
However, I can not figure out how to do it using the URL path. If I use
myWorkBookBasePath= "http://sharepoint/Documents/Reports/2013/"
I get error code 52. Can anyone tell me how to make it work with the URL path?
Give this a go
myWorkBookBasePath= "\\sharepoint\Documents\Reports\2013\"
or
myWorkBookBasePath = "http://sharepoint/Documents/Reports/2013/"
myWorkBookBasePath = Replace(Replace(myWorkBookBasePath, "http:", ""), "/", "\")
MsgBox (myWorkBookBasePath)
and in case of a Sharepoint site hosted using https
myWorkBookBasePath = "https://sharepoint/Documents/Reports/2013/"
myWorkBookBasePath = Replace(Replace(myWorkBookBasePath, "https:", ""), "/", "\")
myWorkBookBasePath = Replace(myWorkBookBasePath, Split(myWorkBookBasePath, "\")(2), Split(myWorkBookBasePath, "\")(2) & "#SSL")
MsgBox (myWorkBookBasePath)
MkDir in VBA can only access filesystem and does not understand URL's, so anything you can open in Explorer you can access with MkDir.
Related
Say, I'm writing a VBA inside my excel file sample.xls. Now I want to get the full path of sample.xls in my VBA. How do I do it?
If you mean VBA, then you can use FullName, for example:
strFileFullName = ThisWorkbook.FullName
(updated as considered by the comments: the former used ActiveWorkbook.FullName could more likely be wrong, if other office files may be open(ed) and active. But in case you stored the macro in another file, as mentioned by user #user7296559 here, and really want the file name of the macro-using file, ActiveWorkbook could be the correct choice, if it is guaranteed to be active at execution time.)
this is a simple alternative that gives all responses, Fullname, Path, filename.
Dim FilePath, FileOnly, PathOnly As String
FilePath = ThisWorkbook.FullName
FileOnly = ThisWorkbook.Name
PathOnly = Left(FilePath, Len(FilePath) - Len(FileOnly))
strScriptFullname = WScript.ScriptFullName
strScriptPath = Left(strScriptFullname, InStrRev(strScriptFullname,"\"))
If you need path only this is the most straightforward way:
PathOnly = ThisWorkbook.Path
if you need path only without file name:
ActiveWorkbook.Path
it would return D:\Folder
if you need file path with file name also:
ActiveWorkbook.FullName
it would return D:\Folder\sample.xls
if you need file name only:
ActiveWorkbook.Name
it would return sample.xls
so if you want combine file path and file name to get full directory don't forget to add "" between. otherwise its simpler using .Path
ActiveWorkbook.FullName would be better I think, in case you have the VBA Macro stored in another Excel Workbook, but you want to get the details of the Excel you are editing, not where the Macro resides.
If they reside in the same file, then it does not matter, but if they are in different files, and you want the file where the Data is rather than where the Macro is, then ActiveWorkbook is the one to go for, because it deals with both scenarios.
There is a universal way to get this:
Function FileName() As String
FileName = Mid(Application.Caption, 1, InStrRev(Application.Caption, "-") - 2)
End Function
Friends,
I currently am pulling data from an excel file located in OneDrive into my spreadsheet using the following source.
= Excel.Workbook(File.Contents("C:\Users\Bob Saggat\OneDrive - USS Enterprise\Prototype\InvestorsMasterProto.xlsm"), null, true)
I use the following code to successfully post data to files with different users from the OneDrive folder in VBA modules with the following code:
Workbooks.Open("C:\Users\" & Environ("username") & "\OneDrive - USS Enterprise\Prototype\OrdersMasterProto.xlsm")
I expected using Environ("username") in the Power Query Editor in the code for the source of the file to work.
= Excel.Workbook(File.Contents("C:\Users\" & Environ("username") & "\OneDrive - USS Enterprise\Prototype\InvestorsMasterProto.xlsm"), null, true)
When I do so I get the following error: Expression.Error: The name 'Environ' wasn't recognized. Make sure it's spelled correctly.
What is the solution here?
Some GoogleFu suggests I host the file on a SharePoint instead of in OneDrive but I have absolutely no experience in SharePoint and I presume there is a simple solution I am missing.
As always, your wisdom and kindness is greatly appreciated.
Thank You
--
Now, I call this module on load to find the username and put it into a named ranged. This works.
Sub GetUsernameForDataSource()
Dim OrderRunTemplate As Worksheet
Set OrderRunTemplate = ThisWorkbook.Worksheets("Order Run Template")
LocalUsername = Environ("username")
'May need to clear the CurrentUser named ranged first then add the user so it doesn't add multiple users
'Adding LocalUsername to named range to use as source in PowerQuery
ThisWorkbook.Names.Add Name:="CurrentUser", _
RefersTo:=LocalUsername
End Sub
Yet when I go into PowerQuery and enter the following
= Excel.Workbook(File.Contents("C:\Users\" & Excel.CurrentWorkbook(){[Name="CurentUser"]}[Content]{0}[Column1] & "\OneDrive - USS Enterprise\Prototype\InvestorsMasterProto.xlsm"), null, true)
I get the error:
Expression.Error: We couldn't find an Excel table named 'CurrentUser'.
Details:
CurrentUser
Any suggestions?
assuming you can get the value of Environ("username") into a named range such as abc, then you can read that named range in powerquery using
Excel.CurrentWorkbook(){[Name="abc"]}[Content]{0}[Column1]
and in your code as
= Excel.Workbook(File.Contents("C:\Users\" & Excel.CurrentWorkbook(){[Name="abc"]}[Content]{0}[Column1] & "\OneDrive - USS Enterprise\Prototype\InvestorsMasterProto.xlsm"), null, true)
I am creating a macro in Excel that should open a PDF document on a specified page with chrome.
Generally, the opening part works. My problem is that when I add the page number (e.g. #page=15) to the url, the shell encodes the "#" symbol into "%23", which Chrome is not able to interpret correctly (file not found).
Here is my code
'The path to the file, replaces spaces with the encoding "%20"
Path = Replace((filePath& "#Page=" & iPageNum), " ", "%20")
Dim wshShell, chromePath As String, shellPath As String
Set wshShell = CreateObject("WScript.Shell")
chromePath = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe\")
shellPath = CStr(chromePath) & " -url " & Path
If Not chromePath = "" Then
'how I first tried it:
Shell (shellPath)
'for testing purposes, led to the same result though:
Shell ("""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"" ""C:\Users\t.weinmuellner\Desktop\Evon\PDF Opening\PDFDocument.pdf#page=17""")
End If
Is there a different way to do this with Chrome? I haven't found anything that reads the installation path dynamically.
You just need to specify the protocol file:/// if you want to load files from the local hard disk. Then # gets not translated into %23.
Shell ("""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"" ""file:///C:\Users\t.weinmuellner\Desktop\Evon\PDF Opening\PDFDocument.pdf#page=17""")
If Adobe Acrobat Reader is installed on the system I would suggest to use the funcion openPDF from Daniel Pineault. This will open the file in Adobe Reader directly. You can find the source code of the function here
A test could look like that
Sub TestSO()
Dim fileName As String
Dim pageNo As Long
fileName = "Path and filename of PDF"
pageNo = 20
OpenPDF fileName, 20
End Sub
I want to open a batch file via vba. This always was no problem but lately the path is not correct.
The path is always:
C:\Users\ [username] \Documents>
The batch file itself seems to be found because the code is correctly passed through:
e.g. C:\Users[username]\Documents>echo ...
I tried with absolute and with relative path. Both leads to the same problem.
pathDir = ThisWorkbook.Sheets("Info").Range("B1").Value
...
functionPath = pathDir & "\" & paramFunction & ".bat"
Call ShellExecute(0, "open", functionPath, paramECU & " " & paramBackend & " " & paramVIN, "", SW_SHOW)
What could be the problem?
Default shell path of excel is
C:\Users[username]\Documents
So, in vba you can check by using the code
msgbox CurDir
If you need to change path of shell, the command is
ChDir "C:\Desire Path"
In case of you is
ChDir pathDir
If the path is in other drive (not C:) you should change drive before change directory. For example drive D:.
ChDrive "D"
ChDir "D:\Desire Path"
then call ShellExecute without prefix path.
Say, I'm writing a VBA inside my excel file sample.xls. Now I want to get the full path of sample.xls in my VBA. How do I do it?
If you mean VBA, then you can use FullName, for example:
strFileFullName = ThisWorkbook.FullName
(updated as considered by the comments: the former used ActiveWorkbook.FullName could more likely be wrong, if other office files may be open(ed) and active. But in case you stored the macro in another file, as mentioned by user #user7296559 here, and really want the file name of the macro-using file, ActiveWorkbook could be the correct choice, if it is guaranteed to be active at execution time.)
this is a simple alternative that gives all responses, Fullname, Path, filename.
Dim FilePath, FileOnly, PathOnly As String
FilePath = ThisWorkbook.FullName
FileOnly = ThisWorkbook.Name
PathOnly = Left(FilePath, Len(FilePath) - Len(FileOnly))
strScriptFullname = WScript.ScriptFullName
strScriptPath = Left(strScriptFullname, InStrRev(strScriptFullname,"\"))
If you need path only this is the most straightforward way:
PathOnly = ThisWorkbook.Path
if you need path only without file name:
ActiveWorkbook.Path
it would return D:\Folder
if you need file path with file name also:
ActiveWorkbook.FullName
it would return D:\Folder\sample.xls
if you need file name only:
ActiveWorkbook.Name
it would return sample.xls
so if you want combine file path and file name to get full directory don't forget to add "" between. otherwise its simpler using .Path
ActiveWorkbook.FullName would be better I think, in case you have the VBA Macro stored in another Excel Workbook, but you want to get the details of the Excel you are editing, not where the Macro resides.
If they reside in the same file, then it does not matter, but if they are in different files, and you want the file where the Data is rather than where the Macro is, then ActiveWorkbook is the one to go for, because it deals with both scenarios.
There is a universal way to get this:
Function FileName() As String
FileName = Mid(Application.Caption, 1, InStrRev(Application.Caption, "-") - 2)
End Function