Running vbs script using cmd - excel

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...

Related

Word freezes, when macro calls Excel to open

I am trying to import data from an Excel sheet into Word.
But I am failing pretty quickly. Word gets stuck right at the beginning.
My code is a simple as that:
Sub ImportDataFromExcel()
Dim XLapp As Excel.Application
Set XLapp = New Excel.Application
End Sub
Here is what happens:
Excel opens up.
But Word freezes immediately. I have to force quit Word.
Word would not get to any further line of code I had included. It stops at „Set XLapp = New Excel.Application“.
I tried a couple of things:
I wrote similar code to open PowerPoint. That worked.
I wrote similar code into PowerPoint, to open Word from there. That worked.
I wrote the same code, that I posted here, into PowerPoint, to open Excel. That failed the same way!
And I also tried on a different Mac. Also there, same thing: Word freezes, when calling on Excel.
I am using Office 365 with Word and Excel for Mac, Version 16.24.
As macro-reference in VBA I use the „Microsoft Excel 16.0 Object Library“.
I am running MacOS Mojave (10.14.3) on my Mac.
Can anyone please help me?
I have no experience with this issue on Mac Os, but maybe rewriting the same code in a different way, will work. Try this:
Dim XLapp As New Excel.Application
Hope this helps.
Try this please
Sub ImportDataFromExcel()
Dim XLapp As Object
Set XLapp = CreateObject("Excel.Application")
End Sub
Maybe MAC do not recoginized the excel library

Copy and Paste between different Excel instances

I bought a software (with a large database), and its output is a simple Excel workbook, not saved anywhere (no path), named generically "Book1", that simply pops up on my screen.
Every time I ask the software for this output, I need to copy the content of this workbook and paste into another workbook, a mother-workbook, as I named it, to consolidate all the data.
I have to repeat this action dozens of times a day, so I thought it would be a great idea to create some VBA code to automate this task.
So... I made a very simple one:
ActiveWorkbook.ActiveSheet.Range("A1:C32").Copy
Workbooks("Mother-Workbook.xlsm").Worksheets("Sheet1").Range("B6:D37").PasteSpecial Paste:=xlPasteValues
The problem is... Each time the software outputs a new workbook, it seems that it is created in a new instance of Excel, which my macro can't reach. I mean, I run the code, but nothing happens, because my mother-workbook doesn't find the generic, unsaved and located in another excel instance "Book1".
If I open the mother-workbook after the output is opened, OK, the code works, because both are in the same instance. But as I need to keep the mother-workbook open all the time, I can't do this. I don't want to save each new output file either. It would take me a lot of time.
I'm using the 2016 version of Excel, but already tried the 2010 as well. My OS is Windows 10 Pro.
Any thoughts?
This code should do it.
Dim xlapp As Object
Set xlapp = GetObject("Book1").Application
xlapp.ActiveWorkbook.ActiveSheet.Range("A1:C32").Copy
Workbooks("Mother-Workbook.xlsm").Worksheets("Sheet1").Range("B6:D37").PasteSpecial Paste:=xlPasteValues
xlapp.DisplayAlerts = False
xlapp.Quit
Note that you need to close "Book1" at the end of your code to make sure that the next time an Excel file is created it will also be called "Book1" and not "Book2". And might as well close the Excel instance while we are at it!
For more information on the GetObject function, you can have a look at this page
Thanks a lot, DecimalTurn and Patrick Lepelletier!
The GetObject really helped me. The "closing" command worked better like this:
Sub CollectA()
Dim oApp As Application
Dim oWb As Workbook
Set oWb = GetObject("Book1")
Set oApp = oWb.Parent
oWb.ActiveSheet.Range("A1:C32").Copy
Workbooks("Mother-Workbook.xlsm").Worksheets("Sheet1").Range("B6:D37").PasteSpecial Paste:=xlPasteValues
oWb.Close False
oApp.Quit
End Sub
Cheers!

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!

Setup that selects automation server (written in .NET) in 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

Resources