Using "GrantAccessToMultipleFiles" array feature in Word 2016 for Mac - vba

I apologize in advanced if this is a basic question. I am new to this community and VBA. I'm using the following Macro in Word 2011 without issue:
ActiveDocument.Save
On Error Resume Next
Dim saveName As String
saveName = ActiveDocument.FullName
saveName = Left(saveName, Len(saveName) - 5) & ".pdf"
ActiveDocument.SaveAs fileName:=saveName, _
FileFormat:=wdFormatPDF
End Sub
I want to be able to use this in Word 2016 for Mac. I know there are sandboxing restrictions which is causing a permission dialog box to appear every time I want to run this macro. IF I understand correctly: once I give permission to a specific folder, that permission will be saved and I won't be prompted again when running macros that need access to that specific folder. So, for instance, I gave permission to my Dropbox folder. However, as part of my workflow, I am routinely creating new subfolders and each subfolder (under my Dropbox folder) apparently needs individual permission also.
I did some searching online and found that "GrantAccessToMultipleFiles" allows for an array to be created to give permission to files so that these dialog boxes do not keep coming up. My question then is: can I somehow give permission to my Dropbox folder and ANY subfolders that exist or are subsequently created?

I think you cant. As a workaround you can save the file to the UBF8T346G9.Office folder and move the file via applescript. Its a bit tricky but doable.

Related

Can I pick a dropbox file through FileDialogue and store the path in my database?

I have a database hosted on SharePoint.
I have a continues form called frmAttachments. It has one PK field, one text field (txtAttachmentPath) and one combo (cboFileType). Text field txtAttachmentPath has a property "Is Hyperlink" set to "Yes".
I made a command button that open FileDialogue, picks the file and pass the path to the txtAttachmentPath field as its value. When I click the txtAttachmentPath field, it opens the file. All beautiful so far...
However, I can only pick files on my local machine (the same would apply to any other user). Is possible that I can pick and store the path of a dropbox file using the same technique: click on command button, get navigated to the organisation's DropBox folder, select a file and get that path stored in my txtAttachmentPath field, so that it can be seen and opened by any database user?
The code below (for my command button cbtAttachment) works like a charm, but I can only pick and store files from my local machine, meaning that other dbs users cannot access it.
Private Sub cbtAttachment_Click()
Dim f As Object
Dim strFullFilePath As String ' Full file path
Set f = Application.FileDialog(3)
f.allowMultiSelect = False
f.Show
strFullFilePath = f.SelectedItems(1)
Me.txtAttachemtPath = strFullFilePath
End Sub
I had a similar issue with hyperlinking to specific directories in Dropbox.
Install Dropbox desktop, if the application is not installed already. Dropbox Application Install Link
You will want to save the attachment paths so you do not run into issues with differing user profiles
SetPathToDropbox = Environ$("%UserProfile%") & "\Dropbox - \path\to\document\or\file"
A similar post for OneDrive was what help me through this Right Here.
I still haven't found a solution for navigating to a file on our organisation's DropBox (FileDialogue cannot see it for some reason), but I did make it work through OneDrive where colleagues can save paths to shared reports, photos, etc. within the DBS, and then open those files on their individual laptops, desktops, etc.

VBA, problems opening .xlsm file on Share Point with Workbooks.Open method

Background:
As part of version control subroutine in my VBA macro, I'm opening a .xlsm file stored in a folder on our corporate Share Point and saving it on a local hard drive.
The file gets opened by Workbooks.Open method, i.e.
Workbooks.Open Filename:="https://<mySharePointFolderPath> & "/file1.xlsm"
When I rolled this out initially, it worked fine for everyone in the team (myself including), apart from one user. At first I thought it was that user's own network issue (they worked from home that day), but when they tried this in the office a few days later, the macro failed again on the above line. The error message says:
"Run-time error '1004':Excel cannot access the file. There are several possible reasons: -The file name or path does not exist.-The file is being used by another program."
None of these applies in our case though.
So I started testing this and somehow managed to cause it to fail for me as well. I can't remember exactly what was it I did, but it was either when I had the file1.xlsm open in Excel the moment I tried to open it again from SP, or when I temporarily removed the file from SP but tried to open it with the macro anyway. Most importantly though, now it fails for me every time I try while it continues to work fine for everyone else (apart from that other user).
Problem:
Unable to open the file from Share Point using:
Workbooks.Open Filename:="https://<mySharePointFolderPath> & "/file1.xlsm"
Other factors:
The affected user and I are both able to:
go to SP folder (via internet browser), download file1.xlsm and open it.
open other .xlsm files in that folder using the above method.
open the .xlsm file in question with Workbooks.Open after it has been renamed (to i.e. file2.xlsm)
open file1.xlsm after modifying the above line to: Workbooks.Open Filename:="\\<mySharePointFolder> & "\file1.xlsm"
but it takes much, much longer to resolve AND still triggers the error 1004 message.
However
opening the file from Excel/File/Open menu (i.e.
File/Open/SharePoint Sites, etc.) gives both of us the following error
message:
"No items to show - We can't open this workbook. It's set to show only certain named items, but they aren't in the workbook. You might
want to contact the author for more information."
While the workbook does have some named ranges which are hidden and
other named ranges that are visible but refer to xlVeryHidden
worksheets, they all are in the workbook, I am absolutely sure about
that. Plus, if this was the cause of the issue, then it should not
work for other users of after simply renaming the file.
So, it behaves almost as if Excel somehow remembered that it once failed trying to open file1.xlsm and is failing every attempt since (no, it's not listed under disabled add-ins).

Create a new file in any desktop using an excel file with a command button to execute that task

I have an excel file, BookDatabase.xls, which holds a database of books and has a command button that backup some contents of a worksheet to a new excel file.
My problem is BookDatabase.xls will be used in a laptop and a desktop, how can I make this button to work in both devices.
Below is my portion of my code for the creation of the file and I'm expecting that this will not work in the other device because the path is only for the desktop.
ActiveWorkbook.SaveAs Filename:="C:\Users\Karen\Documents\BookDatabase.xls"
I'm not very good with VBA and would like to learn more. I hope you can also place a comment in the code so the I could better understand the function of the code lines.
If the problem is only that the User Name is not the same on the laptop, the following should work:
ActiveWorkbook.SaveAs Filename:=Environ("USERPROFILE") & "\Documents\BookDatabase.xls"

Excel Add-In Configure Variables and Save

I have been asked to create an Excel Add-in for people to load a series of xml documents into Excel spreadsheets from Excel. I will write an add-in and publish its location for people to download their own local copies for use. I would Ideally like to have some constants for the VBA configurable by each end user, to customize their experience and function.
I thought it would be nice to have one button to run the Add-in and another button called "Configure" or something, which would edit the values of some of the variables in the VBA, and save the new values so that next time the user open Excel, the Add-in remains configured for them. Is anything like this possible and do you have some suggestions about what path I should follow to get there?
Is there any way to get the variables to persist? I guess I would need to save the values somewhere on the local version of the Add-in, but if so, what is the best way to store a set of parameters?
Any general advice would be most welcome; I have not actually written the add-in yet, being still in the design stages.
I do not advise using cells to store settings as the user may F it up.
However VBA does support Registry edits.
SaveSetting "Macro name", "Settings", "Username", "John Doe"
The above code will save a setting or registry key called Username with the name John Doe in the appname called Macro name and the section Settings.
So in short you only need to change the last two strings when you save new settings. The first two should (to make it easy for you) be the same all the time.
The keys will be stored in : HCU\Software\VB and VBA Program Settings\Your app name\
To get the setting you use:
GetSetting("Macro name", "Settings", "Username")
Another solution is to use txt-file.
It's still better than cells but not as good as above mentioned registry.
settings = "Username=John Doe, Worktitle=Janitor"
MyFile = "C:\myapp\settings.txt"
fnum = FreeFile()
Open MyFile For Output As #fnum
Print #fnum, settings
Close #fnum
Now you have a txt file with the settings that you can read and parse.

How do you take a vba addin and make an installer?

I have written a vba module that installs an addin button to excel. I would like to deploy it on many computers as easy as possible. Currently, these are my steps.
Save the excel as *.xlsm
Open the doc on the computer that I want to install
Save the document as an extension (*.xlam) which places it in the addin folder
Go to options>Add-Ins>Manage /Go>
Click the check box for my add in
Is there a way to automate this process? Write something in vb?
You can build an exe or msi installer with a tool such as Advanced Installer (the free edition). One thing you can do is copy files to a specific location and if you put it in the XLSTART folder then that add-in will automatically load for the user when starting Excel. There's a complimentary folder within the Excel program files directory, usually something like this
C:\Program Files (x86)\Microsoft Office\Office15\XLSTART
which if you drop xlam files into that folder they'll load by default too. The user specific option is
C:\Users[UserName]\AppData\Roaming\Microsoft\Excel\XLSTART
there are PROS/CONS to both models. One is will all users have access or just the user that installs. The benefit of the user specific option is it requires limited rights for installation (no admin privileges)
As others have said, you can easily copy the files to the needed folder in each users Apps directory. They will then need to check the box in the Add-Ins menu, but at least the file will be there. I use this at work with some success by putting the Add-ins and .bat file on a shared drive that everybody has access to. You can then just provide a link to the .bat file which will copy the file from the shared drive to the person's computer in their Apps directory. Here's an example .bat that I use:
xcopy "\\server\share\folder\Addins\CRWScleanup.xlam" "%APPDATA%\Microsoft\AddIns\" /y
Initially I had the .bat files setup to detect and handle Win7 and WinXP because we have a mix at work, but then I realized that the folder location after %APPDATA% (which leads to a different place for XP vs Win7) is the same for each version of Windows. I.e. \Microsoft\AddIns\ %APPDATA% is a global Windows variable and its value will vary for each user (that is good).
It works brilliantly once the Add-In is installed because to update it all I have to do is put the new version on the shared drive and have the user click the link to the .bat while Excel is closed. The new version gets copied over and the user doesn't have to do anything.
With VBA you have to rely on Office files (Excel ones, in this case) and cannot move to executables/installation packages. You might create a program (or a macro) performing the steps you want in an automated way. But if what you want is relying on a standard installation package, which the user might execute (as usual, when installing a program), you would have to move to VB.NET.
VB.NET and VBA are not too different (well... actually, VB.NET includes many more things, but "understands" most of the VBA code) and VB.NET is quite programmer friendly; so a conversion from VBA to VB.NET wouldn't take you too long. In VB.NET you have different ways to interact with Excel; from your question, I understand that you want the Add-in alternative: it generates a custom "installation package" which, once clicked, will install the give Excel Add-in in the target computer. Relying on this option is easy: in your Visual Studio (you need a VS to work with VB.NET), open "New Project" and, within the Visual Basic Templates, select Office (, your version) and Excel Add-in.
NOTE: useful link provided my Mehow: it refers to an old VS (2008) but things haven't changed too much since then.
NOTE 2: the aforementioned suggestion is available in any fee-based VS version since the 2008 one. Not sure about the support in free versions (Express ones).
The easiest way would be to add some install code to the xlsm file on open.
This code could then do the following:
Turn off the install code by changing a variable on sheet (this will
stop the on open code running next time the file opens);
Save a copy of the file as an xlam to the add-in folder;
Activate the add-in;
Then close the xlsm file.
All you need to do is then email the xlsm file out and ask people to open it.
The code is quite simple, but I can show if you need.
I researched a lot of the methods described above and on other sites, but have managed to create my own install and uninstall using Excel add-ins (*.xlam) themselves. My add-ins now install themselves the first time and I have an uninstallation script as well. Works like a charm.
I used my own variation and derivations of Ivan's Solutions': https://grishagin.com/vba/2017/01/11/automatic-excel-addin-installation.html
Sub SaveAsAddIn()
'Alt+F8 SaveAsAddIn Run
Dim sName As String
Dim sFilename As String
Dim o As Object
On Error GoTo Finally
Try:
With Application.ThisWorkbook
sName = Split(.Name, ".")(0) 'name of ThisWorkbook without extension
sFilename = Application.UserLibraryPath & sName & ".xlam"
.Save
.Worksheets.Add After:=.Worksheets(.Worksheets.Count) 'add a blank sheet at the end
On Error Resume Next
Application.AddIns(sName).Installed = False 'uninstall the previous version of the AddIn
SetAttr sFilename, vbNormal
Kill sFilename
Application.DisplayAlerts = False
For Each o In .Sheets 'delete all sheets except the last one
o.Delete
Next
.SaveAs Filename:=sFilename, FileFormat:=xlOpenXMLAddIn 'save ThisWorkbook as AddIn
Application.AddIns(sName).Installed = True 'install ThisWorkbook as AddIn
.Close
Application.DisplayAlerts = True
End With
Finally:
End Sub