I am working through Excel for Engineers and trying to adapt it for LibreOffice Calc. I have run into a problem. I know this is easier to do without using macros but humor me. One of the exercises is to start recording a macro, type:
=RAND()
hit enter and stop recording.
When I run the macro nothing happens. I try to use any other Calc built in function and the same thing happens. Looking at the macro basic file and sure enough nothing is happening.
Can I use built in functions when recording a macro? If so how?
Currently, the LibreOffice macro recorder does not record adding built-in functions. In fact, the recorder has many limitations and generally results in poor code (mostly using the dispatcher).
Instead, write API code based on documentation such as https://wiki.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Function_Handling.
Sub CallSpreadsheetFunction
funcAcc = createUnoService("com.sun.star.sheet.FunctionAccess")
oSheet = ThisComponent.getSheets().getByIndex(0)
oCell = oSheet.getCellRangeByName("A1")
oCell.setValue(funcAcc.callFunction("RAND", Array()))
End Sub
A good place to start learning about LibreOffice macros is http://www.pitonyak.org/oo.php.
Note that learning LibreOffice Basic will not help very much to learn about MS Office VBA. The two languages and platforms are quite different.
Related
This question already has an answer here:
Develop MS Access 2016 AddIn (Ribbon / VSTO) with Visual Studio 2015
(1 answer)
Closed 3 years ago.
I searched a lot for docs to make add-ins for Microsoft Access, Microsoft Docs Access don't have a way to make an add-ins, even visual studio for office developer add-ins don't have Ms-Access Template,
Can I make add ins for Ms-Access?, if so can someone reference a link for how to do that?
Well, you can go to all the trouble to create VSTO add-ins, but if you just need to call + use some .net code from Access, then why bother with all that work?
Just create a COM object in .net, and you can then use + call that code from Access VBA with ease.
So, create a .net class.
Force the project to x86. (assuming access x32)
Check the box [] register for COM inter-op. (this is only required on your development computer).
You also under assembly need to check the box:
[x] Make assembly COM visible
However, above is set by default. So really only ONE check box setting is required here for all of this to work on your part.
Now, just create a class in .net, say like this:
Imports System.Runtime.InteropServices
<ClassInterface(ClassInterfaceType.AutoDual)>
Public Class Class1
Private m_Company As String = ""
Public Function MyHello()
MsgBox("Hello world")
End Function
Public Property Company As String
Get
Return m_Company
End Get
Set(value As String)
m_Company = value
End Set
End Property
End Class
In Access, you can now set a reference to the .net class (tools->references in VBA editor).
Of course, once you have this all working, you change your code to late binding in VBA as all good developers do.
With the above class, then in VBA, you see that even intel-sense works for the .net class methods and properties.
So in VBA, note this screen shot:
Note how all of the subs/functions appear as methods of the object in VBA.
So, calling + consuming .net code you write is rather easy and FAR less code then attempting to setup a VSTO add in.
In fact, I recommend the above not only for Access, but for word, Excel etc.
So the above is oh so much less work then messing around with the tools and setup to create an office add-in.
The end result is with VERY simple coding in .net, such code can be consumed by VBA + Access. And the added bonus is that such code can also with great ease be used in Excel, Word and even windows VBS script files. In fact FoxPro, or even say sage 300 accounting system can thus directly consume your .net code in their provided programing languages. So any language or system that supports COM objects (ActiveX) can thus use the above simple add-in.
In other words, the code you write and create can now be used by all of office with ease, and you don’t have to wire up a messy add-in for each office program.
So, to save world poverty and starving children, just create simple class in .net, and consume it from Office as per above.
About the only issue is now distribution. You have to supply the one .dll from .net, and execute regasm on the target machine for this to work. However, that is a one line batch file, and if you using any kind of installer, such installers have provisions for executing (registering) the .net object via regasm anyway. If you created a true office add-in, you will STILL be required to build and setup an installer – and that installer can be a lot of work as compared to a very simple regasm command to register the .net object.
And if you really want to make this simple, you can side load .net and not even have to register the .net object.
At the end of the day the above super simple class example works just fine in Access + VBA, and works with less work and hassle then it will take you to get some VSTO add-in template working with office.
I have been looking online how to open an application through Access using VBA. I have since found the below example code, that does work:
Application.FollowHyperlink "J:\Prepay\GE Debt logger.accdb"
I have resolved my problem, but I did notice that whilst trying to find my answer, most advice online suggested the below method or something similar:
Shell "Z:\DATA\METRO\ComTool\Update\Update.mdb"
My questions is, the methods I was finding online involved modules, or the use of shell, I wonder why? Why not just use the .followhyperlink, is there a reason not to?
The shell function doesn't work only with executables.
I tried it with MSAcesss 2010 and it not worked.
FollowHyperlink is working, but start an executable with a security question.
When I'm coding VBA in Access 2003, I keep the Immediate window visible and I use Debug.Print and Stop to solve my challenges. But I've never known how to get help from the "Locals" window. I understand what's in there for the most part. But it usually seems like I'd have to dig around for the item I want, and it could take a while to find it in all those folded structures.
(And "Locals" doesn't bring up anything from the built-in VBA help files. Maybe I'm missing a file ...)
I bet I could start using Locals, if I knew what it's good for in a practical way. Do do you have an explanation or anecdote that would clue me in?
Locals provides a couple of benefits: You can F8 through the code and watch how a variable changes by looking at the locals window as opposed to issuing debug.print statements or hovering over the variables. You can see the contents of an array a heck of a lot quicker than issuing lbound and ubound and a bunch of other statements to check values. Not just arrays either, but any complex data structure such as a custom class module.
I'm making some significant changes to some VBA code, and some variables are being deleted and/or renamed. It would be a lot easier to find all the locations I need to update if the compiler would complain to me that the variables don't exist instead of creating it on the fly.
How do I force VBA/Access to require variables to be declared?
You need to use Option Explicit at the top of each VBA code module including forms and reports.
You can set this for all future created modules and VBA code behind forms and reports by going into the VBA editor >> Tools >> Options >> Editor tab and ensuring Require Variable Declaration is checked.
From Access 2003 help:
Require Variable Declaration — Determines whether explicit variable declarations are required in modules. Selecting this adds the Option Explicit statement to general declarations in any new module.
I also use camel case when I Dim my variables. ThisIsAnExampleOfCamelCase. As soon as I exit the VBA code line if Access doesn't change the lower case variable to camel case then I know I've got a typo.
Some History on OPTION EXPLICIT and Access VBA
To follow on from Tony's answer, here's some explanation of why there are issues with OPTION EXPLICIT not being on in some Access code modules.
In Access 95 and Access 97 (the first two Office versions with VBA), Access had a different code editor than the other office programs. In Access 2000, Microsoft implemented the VBE from the other Office apps in Access. At the same time, MS chose to make Access VBA modules behave like the modules in the other apps, which defaulted to not having OPTION EXPLICIT.
Thus, in Access 2000, by default, modules were created without OPTION EXPLICIT.
This was, of course, a really stupid design decision on MS's part, and they reversed it later (I can't remember if it was Access 2002 or 2003 that rectified the problem and defaulted to OPTION EXPLICIT in all new modules again). The reason it was dumb (and MS should have known this) is because Access is a DATABASE APPLICATION DEVELOPMENT tool, and thus is operating on data that is strongly typed. Thus, the code environment should be strongly typed by default so that it is in harmony with the data it is working with.
In Excel or Word, the data is not strongly typed, and it thus makes more sense to use variant data types for just about everything, simply to make it easier for everyone. The downside of implementing that by not using OPTION EXPLICIT is that you can end up with typos that automatically intrdoduce new variables [such as the variable "intrdoduce" -- if I was writing this post with OPTION EXPLICIT, that wouldn't have happened without producing a runtime error! :)]. This is a problem with all such languages that work this way (I pull my hair out working in PHP, where variable names can be distinct by case, i.e., $Var is not the same variable as $var; but I digress), but MS made the decision to implement it that way in Word and Excel on the theory that the people writing code there are going to have an easier time of it if they aren't forced to declare their variables.
So, MS made the mistake of making Access's version of the VBE like the other apps, even though there was no logic internal to Access's own purposes that supported that move. And MS then backed out that change and returned to the previous status quo (i.e., OPTION EXPLICIT by default in all modules).
Thus, you will often see apps that began life in Access 2000 that have modules all over the place without OPTION EXPLICIT. When I have to work on such an app, my first task is to implement OPTION EXPLICIT in all modules and then fix the damned thing so it will compile (which is often quite tough, given that the thing was programmed without it).
This question was quite helpful for Excel, you might see if it'll work for Access:
Lost Variables
Essentialy, MZ-Tools will search through your code and tell you what is not being used. The version for VBA can be found here. Use the Review Source Code feature.
I have somewhat experience with what people call excel "macros". The vba code that controls the activeX components, right?
But I still do not know the true meaning of the term macro :). What kind of code is called macro? Is it something like vba or js implemented into a program? Like vba to excel or vba to autocad or js to flex?
Wikipedia has the answer, under Macro.
Definition:
The term originated with macro-assemblers, where the idea is to make available to the programmer a sequence of computing instructions as a single program statement, making the programming task less tedious and less error-prone.
Usage:
Keyboard and mouse macros that are created using an application's built-in macro features are sometimes called application macros. They are created by carrying out the sequence once and letting the application record the actions. An underlying macro programming language, most commonly a Scripting language, with direct access to the features of the application may also exist.
There's lots more, if you follow the link.
When it comes to MS Office products (Excel, Access, Word, PowerPoint etc), "macro" is VBA (Visual Basic for Applications) code.
NOTE: In Access databases macro can also mean another type of automation besides VBA code.
Since you mentioned Excel...I will talk about Excel.
If you suspect (or know) there is an Excel file with macro, try hitting the Alt + F11 key when you have the Excel file open.
You should see a VBA screen open.
This is where you might be able to see the VBA code (ie, if it is not locked behind a password by the programmer who wrote the VBA code).
Macro (VBA code) is used to automated certain tasks.
So in Excel VBA programmer could put a button on a sheet and when you click the button some activities might take place in the Excel file - activities like calculations, copying, pasting, deleting etc..
Read up a little on "recording a macro in Excel".