Cannot save file to Sharepoint Online using VBA (Permissions error) - excel

This is one of my first times using VBA. I have a command button that is supposed to be saving a file as, to my sharepoint online documents page. I can use the "excel services" and save to that documents page from excel manually, but when i try the same in VBA it is saying I do not have permission. Below is the code I am using, any advice would be much appreciated!
Private Sub CommandButton1_Click()
Dim path as string
dim filename1 as string
path = "https://xxxxxx.sharepoint.com/sites/xxxxx/Shared Documents"
filename1 = Range("B3").text
activeworkbook.SaveAs FileName:=path & filename1 & ".xlsx", _
FileFormat:=xlopenxmlworkbook
End Sub

If your current file is in the same folder as the destination you would like to save in, try this change for the definition of path:
path = "https://xxxxxx.sharepoint.com/sites/xxxxx/Shared Documents/"
The missing final / in your code is causing the FileName argument to be invalid, because path & filename1 & ".xlsx" evaluates to
https://xxxxxx.sharepoint.com/sites/xxxxx/Shared Documents[filename1].xlsx
Which means if permissions weren't restricted on the /xxxxx folder you would have written a badly named Excel workbook in that location.
ALTERNATIVE SOLUTION
Potentially another solution for your problem. Save an empty workbook in the location you wish to save your new Excel files. Open the empty Excel file, and run your macro there. Change the path line to:
path = ActiveWorkbook.Path & "\"
Try this to see if it works instead. This is how I got around Sharepoint permissions problems.

Related

Update Excel Web App from Local Excel VBA

I have a pretty basic knowledge base of VBA. I've been using it the past year to write programs that copy the data from excel to websites.
Right now I have a local excel sheet full of data (which is updated daily), that I am trying to have automatically copied into the Excel Web App via VBA.
I'm hoping someone can tell me how to reference the cells in the Web App from my local VBA (assuming it's possible). I can't seem to find the answer anywhere else.
Any help would be greatly appreciated, thank you!
The Excel Web App opens files from OneDrive, therefor, saving the file in the OneDrive folder on your local machine will do what you ask.
All three of the following examples presume the purpose is to save a macro-enabled workbook as a macro-enabled workbook of the same name in the default OneDrive folder location "C:\Users\user\OneDrive"
The first two examples save a copy of the active workbook to OneDrive; in contrast, the third example moves the file without creating a copy and cannot be used if the file is open.
Example 1: save a copy of the active workbook to OneDrive. The original workbook remains the active workbook. Subsequent changes remain with the original and are not applied to the copy in OneDrive.
Sub SaveCopyToOneDrive()
Dim destinationFolder As String
destinationFolder = Environ("USERPROFILE") & "\OneDrive\"
ActiveWorkbook.SaveCopyAs destinationFolder & ActiveWorkbook.Name
End Sub
Example 2: save the active workbook in OneDrive. The file in OneDrive becomes the active workbook and the original workbook is closed without saving.
Sub SaveToOneDrive()
Dim destinationFolder As String
destinationFolder = Environ("USERPROFILE") & "\OneDrive\"
ActiveWorkbook.SaveAs destinationFolder & ActiveWorkbook.Name, FileFormat:=xlOpenXMLWorkbookMacroEnabled
End Sub
Example 3: move the file from an explicitly defined location to OneDrive. The target file cannot be the VBA host for this macro.
Sub MoveToOneDrive()
Dim shortFileName As String
Dim fullFileName As String
Dim destinationFolder As String
fullFileName = "C:\MyDataFiles\File.xlsm"
Name fullFileName As destinationFolder & shortFileName
destinationFolder = Environ("USERPROFILE") & "\OneDrive\"
shortFileName = Mid(fullFileName, InStrRev(fullFileName, Chr(92)) + 1, Len(fullFileName))
End Sub

Mac Excel 2011 VBA Dir() function file recognition issue

I am trying to create a macro that will allow me to import data from all files ending in ".xlsx" in a specified folder into a single spreadsheet. I've mostly figured it out, but there's a recurring issue that requires a manual work-around until I figure out a solution.
The source .xlsx files contain a standard template for inputting data and are populated and submitted to me by other users. I download these files into a specified folder on my computer. However, the Dir function does not recognize any of the .xlsx files in the specified folder unless I open the file in Excel and click "Save". Once I do that, the Dir function works perfectly and all the .xlsx files are listed in the Immediate window. I'd appreciate any insight anyone has regarding why this is happening and if there's any way to fix the code so it recognizes the files without opening and saving.
Sub Test()
Dim strPath As String
Dim strFile As String
strPath = ActiveWorkbook.Path & Application.PathSeparator & "ReferenceFolderName" & Application.PathSeparator
strFile = Dir(strPath, MacID("XLSX"))
Do While Len(strFile) > 0
strFile = Dir
Debug.Print strFile
Loop
End Sub

Excel VBA: Automated Excel Backup Stopped Working due to "Compile Error: Cannot find file or library"

I have an inventory xls file, that includes a diagram of the office. When the xls is opened, the VBA automatically makes a backup of the file before any changes are made. That has worked for the past year. Today it has broken.
Yesterday, the file was working fine.
1) I added some info to the xls sheet - nothing unusual; the same sort of info I've added over the past year. I did NOT edit the VBA.
2) I added some objects (shapes-circle, square, etc) to the tab with the office diagram.
Today, the file is not working.
1) The VBA debugger gives an error on open: "Compile Error: Cannot find file or library."
2) Numerous text boxes/shapes have vanished off the tab with the diagram. I did not remove them.
UPDATE 1: I moved a shape on the diagram, and all the text boxes reappeared.
Any suggestions to fix this are appreciated.
'Saves an exact copy of the file upon opening it to the \Back_Tracker location and added today's date to the filename.
Private Sub Workbook_Open()
Dim WBPath As String, WBName As String, TimeStampStr As String, PassW As String
WBPath = ThisWorkbook.Path
WBName = ThisWorkbook.Name
'PassW = "something"
Const cstrBACKUP As String = "Backup_Tracker"
If InStr(1, WBPath, cstrBACKUP) = 0 Then 'prevent backups from making backups of themselves.
TimeStampStr = Format(Now(), "YYYY-MM-DD_hh-mm_")
'Application.StatusBar = "Saving backup..."
ActiveWorkbook.SaveCopyAs Filename:=WBPath & Application.PathSeparator & cstrBACKUP & Application.PathSeparator & TimeStampStr & WBName
'Application.DisplayStatusBar = False
'Application.DisplayStatusBar = True
End If
End Sub
My experience with Excel VBA is that the code can sometimes corrupt when opening. It used to be the case with Access as well, but I'm not sure if that was eventually fixed.
My solution was to either make backup copies after each edit, or use source control for the Excel file. Its the only way to be sure.
Found the problem.
Unchecked this, and it's working now.
Reference
Open the database or application.
Open a module in Design view or press ALT+F11 to switch to the Visual Basic Editor.
On the Tools menu, click References.
Clear the check box for the type library or object library marked as "Missing:"

Using Workbooks.Open on a known file location yields "Run-time error 1004 <file> could not be found"

I am building a utility to grab data from specific files and transfer that data to another file. The source files all have the same naming convention which includes an identical string that can be used to differentiate them from other files in a directory. The intent is to use that string with wildcards to open the file and pull the required data.
I thought I had Workbook.Open working with wildcards yesterday, after consulting this StackOverflow post and this entry in the Office Dev Center, but today I try running my test code and VBA claims not to find the file (though the text suggests otherwise).
There is not much to go wrong in my test code:
Dim strFilePath As String
strFilePath = "C:\...\KMMacros\DeepLinkTransferFromSabaForm\"
Dim strFileName As String
strFileName = "*SabaEntryForm.xlsx"
Workbooks.Open FileName:=Dir$(strFilePath & strFileName), ReadOnly:=True
The error reads
'TEST_KM6.7-3_BRSTA_SabaEntryForm.xlsx' could not be found. Check the
spelling of the file name, and verify that the file location is
correct.
and debug highlights
Workbooks.Open FileName:=Dir$(strFilePath & strFileName), ReadOnly:=True
Because the error specifically mentions the file I expected to open, and because my code does not give the specific file name, it seems like VBA has found the file, even though the error states otherwise.
How can VBA be finding the name of the file and then claim that it cannot find it? What am I doing wrong?
Dir() only returns the filename, not the whole path, so if your current directory is different from the folder you pass to Dir then you will have this problem.
Dim strFilePath As String
strFilePath = "C:\...\KMMacros\DeepLinkTransferFromSabaForm\"
Dim strFileName As String
strFileName = "*SabaEntryForm.xlsx"
Workbooks.Open FileName:=strFilePath & Dir$(strFilePath & strFileName), _
ReadOnly:=True

Unintended file dialog when updating links in Excel 2003 VBA

I'm keeping sets of interrelated Excel 2003 spreadsheets for each of my company's projects.
I want to copy some template XLS files to the project name and change the links that connect them to each other.
For example, the file TEMPLATE_ScanProgress.xls links to TEMPLATE_Film_Review.xls.
I am copying them both to 123456_ScanProgress.xls and 123456_Film_Review.xls, and updating the link in 123456_ScanProgress.xls.
Sample code of what I'm doing:
If Dir("WorkOrder & "_ScanProgress.xls") = "" Then
FileCopy "TEMPLATE_ScanProgress.xls", WorkOrder & "_ScanProgress.xls"
Workbooks.Open Filename:=WorkOrder & "_ScanProgress.xls", UpdateLinks:=0
ActiveWorkbook.ChangeLink "TEMPLATE_Film_Review.xls", _
WorkOrder & "_Film_Review.xls", _
xlLinkTypeExcelLinks
Workbooks(WorkOrder & "_ScanProgress.xls").Close SaveChanges:=True
Else
FileExists = True
FileExistsWarning_7 = WorkOrder & "_ScanProgress.xls"
End If
The problem is that when the code tries to update the link I get a file dialog asking me to choose a file for the change, even though I already specified which file I want in the code.
Try setting DisplayAlerts to False. DisplayAlerts is on the Application object and is used to suppress dialog boxes for example when overwriting a file. It may work in this case too.

Resources