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".
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 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.
I am working on several different access projects that share a lot of code. Right now what I do is importing modules, which is fine as long as I'm the only one working on it, but for the future I need a better solution (also better than password protection).
What would be the best way to share code? I have Visual Studio 2013 for that purpose. I have tried creating a dll and com objects, but I couldn't add the files as a reference to Access, I think maybe it's because it was .NET, I'm not really sure.
As you can see I am not familiar with this subject, and so any hints/links/buzz words would bee highly appreciated.
Thanks, Yotam
One way to share code among VBA projects is to store the common code in a separate .accdb file (say, "CommonCode.accdb") and then add a Reference to that file in the various VBA projects that will use those common procedures and functions.
To add such a reference you need to click the "Browse..." button in the References dialog and then choose "Microsoft Access Databases (*.accdb)" from the "Files of Type" drop-down:
I believe that the best way to share a VBA code is not to let it readable, but compiled.
You can do this by creating an Access Runtime, .accdr.
version control with ms-access: check here and there
well ... these are the links to my own answers to this question, but I do not mind if you check and prefer other's answers
I recenty stumbled upon some files described as "HTML Applications" on my Win XP machine.
What are they?
Who would ever use them? Why do I have like 2 or 3 of them on my PC?
How do they generally work? I mean hey - HTML is for adding formatting to text - HTML Applications? What the? Microsoft?
HTAs are good for things like VB scripts that you want an interface for other than MsgBox or a console window.
Since it's HTML, you can use buttons, text areas, check boxes, etc to show information to the user and get input from them, and use CSS to style it all. Since HTAs run on the local machine, you have access to everything you can do with VBScript for computation and file access, WMI for system management, program automation with COM objects, data access with ADO, and so on.
I once wrote an HTA that installs, updates, and compares Word templates on a user's machine from a common folder. The user can see their template folder next to the common folder to know if they are up to date, and hit the Update button if not.
Another one manages and verifies the installation of a program on a user's computer, copying over the exe if necessary, making sure registry entries are set correctly, putting a shortcut on the desktop, letting the user test and see the results of the installation, and so on. It also logs all of this info to a common place for me to check on.
One of my biggest HTA projects was a Project Manager system. The interface showed me all of the Excel, Word or Access projects I had going on. It would open the selected project in its particular environment, and showed me all of the pieces of it. It allowed me to import and export code modules from a common library using VBE automation (the Visual Basic Editor COM interface).
I'm about to put one together to show current and "dead" printer drivers on a user's machine. With me coaching them over the phone, they will run the HTA which will list all of the installed printers. They will put a check mark next to the ones they want to keep, then hit a button to delete all of the others. Fairly easy for them, and saves me from going to each and every PC to fix this.
Many of these kinds of things only make sense in a Windows environment, but you can write some pretty general purpose stuff with it too. Anything you can express in VBScript or JScript (JavaScript) and want an HTML/CSS front on is a good candidate for an HTA. I also even wrote a basic network chat system in it at one point.
There are lots of little HTAs around for converting data from one format to another, say converting comma separated data to columnar, or adding or removing various kinds of formatting like quote-printable escape codes, converting hex formatted text into plain text, and on and on. Copy text into one input text area, check a few options and press the Go button, then copy the converted data from the output text area. One I wrote was an SQL formatter. It would take SQL code and wrap it up as either a VB or Delphi string, and also
go from wrapped back to plain SQL code, with basic indenting and "pretty printing" to clean it up.
I don't do as much with HTAs as I used to, but still think they are a pretty cool technology for the kinds of jobs that fit in that niche.
See here for Introduction to HTML Applications (HTAs).
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.