VBA Overwrite Addin Workbook - vba

So I have done some research but I can't get the feature I am looking for to work.
I am deploying a workbook to a lot of people on a network. I have an add-in on a network drive that all of the workbooks reference to. It works great when running the macros, no problem at all. My issue is updating the macros when people have one of the workbooks open. People leave things open for days sometimes, I know, cause I do the same thing sometimes.
I found this link, with the code below but I can't get it to work.
https://www.excelguru.ca/content.php?152-Deploying-Add-ins-in-a-Network-Environment
The macro saves it as a .xlsm or a macro enabled workbook. It won't save it as a .xla
Sub DeployAddIn()
'Author : Ken Puls (www.excelguru.ca)
'Macro Purpose: To deploy finished/updated add-in to a network
' location as a read only file
Dim strAddinDevelopmentPath As String
Dim strAddinPublicPath As String
'Set development and public paths
strAddinDevelopmentPath = ThisWorkbook.Path & Application.PathSeparator
strAddinPublicPath = "F:\Addins" & Application.PathSeparator
'Turn off alert regarding overwriting existing files
Application.DisplayAlerts = False
'Save the add-in
With ThisWorkbook
'Save to ensure work is okay in case of a crash
.Save
'Save read only copy to the network (remove read only property
'save the file and reapply the read only status)
On Error Resume Next
SetAttr strAddinPublicPath & .Name, vbNormal
On Error Goto 0
.SaveCopyAs Filename:=strAddinPublicPath & .Name
SetAttr strAddinPublicPath & .Name, vbReadOnly
End With
'Resume alerts
Application.DisplayAlerts = True
End Sub
I've tried doing
.SaveAs FileFormat:=xlAddIn
Or
.`SaveAs FileFormat:=18`
Niether of those work. SaveCopyAs doesn't have a fileformat option.
Any suggestions or any other methods that might work?

You keep a copy of the XLAM on your local machine that you update, and then when you are ready to deploy it, you copy the XLAM to the server. Everyone else runs off a copy that is on the server (you dont want them to copy it to their local machine, btw) - The key is that when you copy it to the server, you have to set it to read-only in the file properties. You have to do that every time you copy it. Then you can overwrite it at any time.
Comment if you have any questions - I do this every day for work.

Related

Disabling macros from other Excel sheets

I'm working on a few VBA macros in Excel for a project. Is there a way to ensure that VBA macros don't launch from other Excel workbooks when you are opening them through a macro?
For example, let's say I have a macro inside one workbook to open some other Excel file:
Sheets("Sheet1").Select
PathName = Range("D3").Value
Filename = Range("D4").Value
TabName = Range("D5").Value
ControlFile = ActiveWorkbook.Name
Workbooks.Open Filename:=PathName &; Filename
What I've usually done is include a Application.EnableEvents = False to the subroutine to ensure that I'm not triggering code upon opening the other workbooks. This is also the methodology suggested by this prior SO post.
But two questions come to mind:
Is this "secure"? Are there other ways that users could write their own macros that execute on my active application even if I disable events?
Are there other workarounds besides fully disabling events? This seems somewhat limiting and almost "throwing the baby out with the bathwater".
This seems to open the file without the macros and it's read only.
When testing this, I can't even see the macro module in VBA editor for the opened file..
Application.AutomationSecurity = msoAutomationSecurityForceDisable
Workbooks.Open Filename:=strFilepath & strFilename, ReadOnly:=True
Application.AutomationSecurity = msoAutomationSecurityByUI

Macro: Programmatically Remove Known Excel Passwords

Is there a means to remove a KNOWN password from an Excel file via a VBA macro? Web searches only return the method to crack unknown Excel passwords.
I've automated all other aspects of the data handling except downloading files from an FTP site and subsequently removing the password.
You'll need to open the workbook specifying the passwords to open and modify, then saveas specifying an empty password for open and modify. To avoid getting prompts when overwriting the existing file, you need to disable alerts.
e.g.
Sub OpenAndSaveWithoutPasswords()
Dim wb As Workbook
Application.DisplayAlerts = False
Set wb = Workbooks.Open(Filename:="YOUR PATH AND FILENAME", Password:="OPEN PASSWORD", WriteResPassword:="MODIFY PASSWORD")
wb.SaveAs Filename:="YOUR PATH AND FILENAME AGAIN", Password:="", WriteResPassword:=""
Application.DisplayAlerts = True
End Sub
Note: Workbooks.Open seems to require brackets, wb.SaveAs requires no brackets (don't ask me why).
Just resave the workbook without a password:
ActiveWorkbook.SaveAs

Excel workbook trying to save to network directory when it shouldn't when running macro

I'm trying to get a macro to work where it saves a file a certain format and reopens it to avoid compatibility issues, however, when the macro runs it tries to save to a network directory that is no way specified in the code.
Dim wkbook As String
wkbook = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) & ".xlsm"
ActiveWorkbook.SaveAs wkbook, FileFormat:=52
ActiveWorkbook.Close
Workbooks.Open Filename:=wkbook
Use ChDir to set the directory.
ChDir("C:\mydirectory")

Blank worksheets opened up every time Excel starts - accidentally created after splitting Spreadsheet using VBA code

I need a bit of help with Xcel 2010.
I have a spreadsheet budget workbook in Xcel 2010 that had 6 tabs of worksheets. I needed to make a copy of just one of those worksheets to pass on to someone who was not allowed to see the other worksheets.
I used this VBA code shown below to split up the workbook and it worked just fine. But, since doing so, now every time I open up any Xcel file I get three blank worksheets that open up at the same time and they are labeled as Tabelle1.xls, Tabelle2.xls and Tabelle3.xls.
I thought that using another VBA code (see below the other code) to delete the module would take care of that but it did not. Now there is no code/module at all if I open up Visual Basic but I still get three blank worksheets that open up separately from whatever Xcel file I open up. The three blank worksheets all have the .xls file extension but the version of MS Office I have uses .xlsx.
Other info: I am using Windows 7, this is my work computer and do not know anything about VBA coding myself. I was just looking for a quick fix by Googling and got myself in trouble.
The code to split up the Workbook
Sub Splitbook()
MyPath = ThisWorkbook.Path
For Each sht In ThisWorkbook.Sheets
sht.Copy
ActiveSheet.Cells.Copy
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteValues
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteFormats
ActiveWorkbook.SaveAs _
Filename:=MyPath & "\" & sht.Name & ".xls"
ActiveWorkbook.Close savechanges:=False
Next sht
End Sub
The code I used to delete the module:
Sub DeleteModule()
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Set VBProj = ActiveWorkbook.VBProject
Set VBComp = VBProj.VBComponents("Module1")
VBProj.VBComponents.Remove VBComp
End Sub
SectionBreak
Your symptoms are indicative of a workbook being in the XLSTART folder (an 'autorun' folder). The reason for the names being "Tabelle" are that the source of this workbook was likely a German locale.
Any workbook in this folder is opened whenever the Excel application starts up. It is a folder typically used to place your personal macro workbooks so macros within are automatically available when working on different workbooks.
In your case, this appears unintentional. Check the folder at, typically:
Win7/8: C:\Users\[Username]\AppData\Roaming\Microsoft\Excel\XLSTART
WinXP: C:\Documents and Settings\[Username]\Application Data\Microsoft\Excel\XLStart
and clear out any files you were not expecting there.
I refer you to http://office.microsoft.com/en-gb/excel-help/customize-how-excel-starts-HP010197489.aspx for more details over how you can customise the templates and workbooks Excel uses to achieve helpful results.

How to make Excel VBA Automation Execute for All End Users

I wrote the following code so that when an Excel spreadsheet is closed it will update its name with the current date and time:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ThisWorkbook.Name = "Name_Last Opened-" & Format(Date, "MM-DD-YYYY") & _
"_" & Format(Time, "HH.MM") & ".xls" Then
Else
ThisWorkbook.SaveAs Filename:="\\C:\... Name_Last Opened-" & _
Format(Date, "MM-DD-YYYY") & "_" & Format(Time, "HH.MM") & ".xls"
FName = Sheets("Name").Range("D1").Text
Kill FName
End If
End Sub
Private Sub Workbook_Open()
Range("A1").Select
ActiveCell.FormulaR1C1 = ThisWorkbook.Name
End Sub
Additionally, the code is located within VBAProject(Name of file), under MS Excel Object - ThisWorkbook.
This code works perfectly for me or the workstation that it was created on; however, it does not execute for anyone who opens it on their worstation. Would anyone know how to get the code to execute whenever the spreadsheet is opened and closed from any computer, not just mine?
Thank you,
DFM
It's possible that Excel's security settings aren't allowing other people's computers to run the script that could be interpreted as risky malware. Perhaps you changed your security settings so long ago that you forgot about it. See if you can modify another user's security settings to see if that will make the macro execute on the workbook close.
"Would anyone know how to get the code to execute whenever the spreadsheet is opened and closed from any computer, not just mine?"
I don't think it can be done with 100% certainty unless you can ensure that every possible user will have macro security set such that your macro can execute.
Assuming you can get past that one, you should perhaps check that the users all have the worksheet in the same hard-coded path on C:\ that you seem to be using. What happens if they open the workbook from a different location?
Also:
FName = Sheets("Name").Range("D1").Text
is getting a value from one place and
Range("A1").Select
ActiveCell.FormulaR1C1 = ThisWorkbook.Name
is putting it in another.
I think I'd try something like the following (which assumes from your code that you actually only want to change the file name if it has not changed since the minute of the current time changed):
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim dateTime As String
Dim oldPath As String
Dim newPath As String
dateTime = Format(Now, "MM-DD-YYYY_HH.MM") ' Format the while thing in one string - once
With ThisWorkbook
oldPath = .FullName ' what is it called now, and where did it come from?
newPath = .Path & "\" & "Name_Last Opened-" & dateTime & ".xls" ' what should it be called now?
If oldPath <> newPath Then ' only do something if not saved in last minute - is that what you really want?
.SaveAs Filename:=newPath
Kill oldPath
End If
End With
End Sub
Date() function needs administrator access to run.. so if your user is a non admin, then it will fail. Instead use now(). Most of the times this is some thing which we usually forget as we(people developing the tool) have admin access over our PC's
Fundamentally, you cannot ensure that all users will a) have a macro security setting of low or medium, and b) if set to medium, enable them when the file is opened.
Creating your own certificate would seem like the obvious answer, but in practice I find that the resultant messages and warnings are even more confusing/frightening for some end users, leading to much the same situation as with macro security. Third-party certificates avoid this, but are $$$ and almost surely overkill for an Excel workbook in a corporate environment.
What I've done where I need users to have VBA enabled is to set all sheets to xlveryhidden on save, except a custom locked sheet that only has a note saying macros must be enabled and a brief guide on how to do this. This sheet is hidden and the others restored by the workbook's workbook_open procedure, something that of course will not fire if VBA is disabled.