Setup that selects automation server (written in .NET) in excel - excel

We have some excel formulas written in .NET and exposed to Excel via COM. The user who wants use our formulas have to:
run some bat file that registers dll's into COM (using RegAsm)
then in Excel (2010) select File|Options|Add-ins|Excel Add-ins|Automation|Select our server|GO|GO
We want to avoid the (2.) using some automatic setup. Does anybody have any idea how it can be accomplished?
Thanks in advance.

You have not had an answer in some time. I do not know that this will work, but it may be worth trying:
Sub Add_an_Addin()
Dim oAddin As AddIn
Dim oTempBk As Workbook
Set oTempBk = Workbooks.Add
Set oAddin = AddIns.Add("E:\CostBenefit1.0.xla", True)
oAddin.Installed = True
oTempBk.Close
End Sub
FROM: http://vbadud.blogspot.com/2007/06/excel-vba-install-excel-add-in-xla-or.html

Related

How to deal with co-authoring while editing an Excel file in Sharepoint via VBA

I have an excel file stored in Sharepoint (which is also accessible with Microsoft Teams), with the path: https://organization.sharepoint.com/PathOfFile/myFile.xlsx
The file can be edited by multiple at the same time with the co-authoring feature in Sharepoint.
I want to use another excel file stored locally in my computer to access and modify the one in Sharepoint. This local file has a button with this VBA code in it:
Sub UpdateSP():
f_name = "https://organization.sharepoint.com/PathOfFile/myFile.xlsx"
Workbooks.Open f_name
Workbooks("myFile.xlsx").Activate
ActiveWorkbook.Sheets("sheet1").Activate
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
ActiveCell.Value = 9999
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = 0000
ActiveWorkbook.Close SaveChanges:=True
End Sub
In principle it works, the file in Sharepoint is modified. But things go wrong if there's someone editing the file while I run the code, then two versions of the file seem to be created, one for the online-live editing, and the one for my code.
If this happens, the online version of the file won't show the changes made by the code, and whenever the file is opened with the excel app, a pop-up will show asking which version of the file should be kept, losing all the changes done in the disposed version.
I have tried to use the CanCheckOut and CheckOut methods, but CanCheckOut always returns False for whatever reason (there are some questions here with the same issue but I havent been able to find a solution).
Can someone suggest a solution to this issue? Thanks.
I'm not 100% sure it will work on SharePoint, but in theory, ADODB is a library for VBA that has the syntax of objects to use Microsoft's Jet Engine so you can open files AdLockOptimistic---ally. ((look up lock types in ADO.net))
This works on a file directory basis, so if the DB being modified is open, it will handle the update.
Instead of using Excel's Application to open the file, you would establish an ADO connection, and then specify the type of Lock in order to access the Excel's sheets and tables inside it.
This works for shared / network drives, so I'm guessing since SharePoint can be mapped as a file explorer drive, then ADO should work and is worth a try.
Here's a basic example to get you started: ADO question
Try enabling the autosave after activating the workbook.
To do so, add this line:
ActiveWorkbook.AutoSaveOn = True
after the Workbooks("myFile.xlsx").Activate line.
I have had similar issues with collaborative files and making sure the autosave is enabled has solved it.
To be able to incorporate changes that way your code must run inside a coauthoring context.
Instead of opening the document from another doc or local copy, the code must be running inside the same document being opened from the same source URL (Sharepoint or OneDrive), that way the add-in or macro can make changes that Excel itself will handle on a coauthoring context.
I recommend taking a look at Coauthoring in Excel add-ins of the Office Dev Center, including the linked articles inside (specifically "coauthoring", redirecting to the support center, and "About coauthoring in Excel (VBA)" at the bottom with more samples).
CanCheckOut will always return false if a workbook is open. Thus you must check before you touch it. The CheckOut command will not open the file so we must also have an open statement after CheckOut.
Using your example it would look like this;
Option Explicit
Public Sub UpdateSP()
Dim fName As String
fName = "https://organization.sharepoint.com/PathOfFile/myFile.xlsx"
If Workbooks.CanCheckOut(fName) Then
Workbooks.CheckOut fName
Dim myFile As Workbook
Set myFile = Workbooks.Open(fName)
Dim mySheet As Worksheet
Set mySheet = myFile.Sheets("Sheet1")
Dim startRange As Range
Set startRange = mySheet.Range("A" & mySheet.Rows.Count).End(xlUp).Offset(1)
startRange.Value = 9999
startRange.Offset(0, 1).Value = 0
myFile.Close SaveChanges:=True
Else
MsgBox fName & " can't be checked out at this time.", vbInformation
End If
End Sub

excel with vbscript which is using third party COM ADD-IN

I want to automate a process in excel with vbscript which is using third party COM ADD-IN
Sub LoadAddIn()
Dim oComAddIn As COMAddIn
Set oComAddIn = Application.COMAddIns.Item("C:\Program Files (x86)\Thomson Reuters\Eikon\EikonOfficeShim.dll")
oComAddIn.Connect = True
CreateFolder
End Sub
But while running excel from vbscript, it is opening without any add-ins enabled and formula which is using the above add-in are not working.
I googled so many websites but not getting exact syntax to use to connect third party add-in while executing excel from vbscript.
I can open the EIKON AddIn and start the process using the following:
'Start EIKON Update
Dim RetVal As Long
RetVal = ExecCmd("C:\Program Files (x86)\Thomson Reuters\Eikon\eikon.exe -officeexcel -a " & sPath)
Note that sPath is the full string path of the file I am opening.
Caveat: The file does not automatically close. I'm still working on that part.

Running vbs script using cmd

Could someone please tell me why the following VB script works fine if executed from within excel but won't work if it executed using the cmd: cscript c:\vb\test.vbs?. Below is my code I'm trying to get it working using the cmd. I'm using excel .xls (excel 97-2003).
Private Sub CopyData()
Dim x
Dim y
'## Open both workbooks first:
Set x = Workbooks.Open("C:\VB\CopyDataTest.xls")
'Now, copy what you want from x:
Sheets("Sheet1").Range("A:B").Copy
Set y = Workbooks.Open("C:\VB\Destination.xls")
'Now, paste to y worksheet:
y.Sheets("Sheet2").Range("A:B").PasteSpecial
'Close x:
y.Close
End Sub
If you need to run the script from cmd, you need to create an excel object. Try this:
Private Sub CopyData()
Dim x
Dim y
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\VB\Destination.xls", 0, True)
## Open both workbooks first:
Set x = xlApp.Workbooks.Open("C:\VB\Destination.xls")
Now, copy what you want from x:
xlApp.Sheets("Sheet1").Range("A:B").Copy
Set y = xlApp.Workbooks.Open("C:\VB\Destination.xls")
Now, paste to y worksheet:
y.Sheets("Sheet2").Range("A:B").PasteSpecial
Close x:
y.Close
End Sub
CopyData()
Mnimonic already gave an perfectly usable workaround for this, but an explanation could be usefull too.
You have written a piece of code in VBA (Visual Basic for Applications).
You tried to run it as VBS (VB Script)
VB script doesn't know about Office and other libraries already loaded when running the code inside excel.
You'll need to learn how to interact with the COM interfaces from office in VBscript.
Better solution now would be to program in VB.NET and interact with excel inside .NET:
Link: VB.NET and excell
The code will still look very familiar, but it's what Microsoft would like you to do now.
Attention: You will always need to have Excel installed on the PC running the script!
If you want to avoid that, maybe look at something like Aspose to do things without Office installed...

Excel Macro Stored in Access

So I'm importing data every day into Access to use for reporting. The data comes from several spreadsheets created by different individuals. Because those individuals like to format things incorrectly I created a macro that reformats their document so that it can be imported cleanly into Access for me to use. Works great but it gets tedious having to open up each Excel sheet to run this Macro.
What I'm trying to do is place the Excel Macro in Access and then run the formatting code before importing it all at once. I am a bit lost in approaching this. I'm aware of ways to run Macros already placed in Excel sheets but is there a way to run a macro that is stored in Access that works in excel. I also thought to maybe inject the Macro into the excel document and then run it.
To sum things up, what I'm hoping to do is from Access, store a macro, that can be used to alter Excel Files.
Is this at all possible? If so How? Is there another approach?
What you are asking to do is automate Excel from Access. Yes, you can do this. In Access, add a module, add a reference to the Microsoft Excel object model (Tools: References), and use this framework code to get you started:
Sub PrepExcelFileForImport()
Dim xl As Excel.Application
Dim wbk As Excel.Workbook
Dim wst As Excel.Worksheet
Set xl = CreateObject("Excel.Application")
With xl
.Visible = True
Set wbk = .Workbooks.Open("c:\temp\temp.xlsx")
Set wst = wbk.Worksheets("data")
With wst
' add your formatting code here, be sure to use qualified references, e.g.
.Rows(1).Font.Bold = True
End With
End With
wbk.Close SaveChanges:=True
xl.Quit
End Sub

How can I activate an Excel add-in from the command line in Windows 7?

Currently I'm writting VB functions and save them as an Excel addin .xlam file.
I want to have a .bat script so as to quickly deploy those addins.
Currently, to activate my .xlam addins, I have to Open Excel - File - Option - Addins - Browse to addin files... as below screenshot. This is absolutely manual, repeated & tiring thing to do.
So my need is to automate the activation process.
I was looking for exactly the same sort of thing this morning. I will eventually try something like this out, but I haven't yet. So, here is what I have come to so far:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.addins2.add.aspx
This is an example about how to use Excel automation from C#. From what I see, all these automation interfaces are really COM interfaces, so you are not restricted to C# or Visual Basic (maybe you can use some fancy scripting of Windows to work with them? what I will try is to use python with pywin32, but that's only because it suits my taste).
Then, for registering the addin(s), check this method:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.addins2.add.aspx
I actually saw an example somewhere about how to use it, but I can't find it right now.
Anyway, these are just ideas. I'm very interested on knowing how it all ends ;-)
you can insert this code in your *.xlam in the sheet "ThisWorkBook" this code install and activate the current AddIns, just by opening
Private Sub Workbook_Open()
Dim oXL As Object, oAddin As Object
URL = Me.Path & "\"
normalUrl = Application.UserLibraryPath ' Environ("AppData") & "\Microsoft\AddIns"
AddinTitle = Mid(Me.Name, 1, Len(Me.Name) - 5)
If URL <> normalUrl Then
If MsgBox("Can you Install AddIns ?", vbYesNo) = vbYes Then
Set oXL = Application ' CreateObject("Excel.Application")
oXL.Workbooks.Add
Me.SaveCopyAs normalUrl & Me.Name
Set oAddin = oXL.AddIns.Add(normalUrl & Me.Name, True)
oAddin.Installed = True
oXL.Quit
Set oXL = Nothing
End If
End If
End Sub
After one manually added time, we can update the addin by copy the addin file to Excel addin lair. Here is the .bat script to do it.
set fipAddin=".\FIPphase2.xlam"
set excelAddinLair="%APPDATA%\Microsoft\AddIns"
copy %fipAddin% %excelAddinLair%
Hope it helps!

Resources