vba variations across office suite - vba

what kind of functionality variation is there across vba for excel vs vba for access vs vba for word, etc..
i know that probably 95% it's the same. but what is that 5% of differences consistent of?

Open a VBA editor and go to the object browser (F2). Click the dropdown that says <All Libraries>. Here you can see all the libraries available by default in the current application. Regardless of which application you're in, you'll always see the Office, VBA, and stdole libraries. You'll also see a library with functions specific to the current application: Word, Excel, etc.
If you look at the application-specific libraries you'll see that they contain objects and methods specific to that application - cells, worksheets, and formulas in Excel; paragraphs, styles, and mail merges in Word; etc.

Related

Outlook Search Folder Criteria

Is it possible to set criteria of a Search Folder in Outlook to a large number of keywords, like this:
subject contains "abc" OR "def" OR "ghi" ...
Search for the word(s) box in Search Folder Criteria does support comma-separated list of values, but the max. length of that box is quite limited (255 chars I guess). The number of keywords I have is very large (hundreds of them). Adding them manually through Advanced tab is also a pain, so I'm looking for a more programmer's way. One of the following should work:
If Outlook stores these criteria in a flat file somewhere (like Thunderbird does), I'll edit it directly and inject my keywords into it.
If I could manipulate the criteria through Object Model (VBA), that's also a good solution.
If VSTO can do it, I have that experience as well.
Does someone know if any of the above method works?
Note: I'm using Outlook 2013 if that has something to do.
You can develop either a VBA macro or an Outlook add-in (VSTO can be used for developing an add-in). Try using the AdvancedSearch method of the Application class. See Advanced search in Outlook programmatically: C#, VB.NET for more information.

Add VBA Project to Visio Document by Using Code

I've been asked to create a macro to update a few hundred or so Visio drawings, and keep them updated.
The update involves putting all objects of a certain type on their own layer - simple.
Now, this is easy enough to do, but when a user adds a new object some time in the future it will likely be on a default layer. So I had hoped to be able to include a VBA macro which is triggered by the Save event to re-assign objects to their layers.
The problem here is that I'd need to include this macro in every document since Visio doesn't have an application level VBA project.
Is there any way to introduce a VBA project to ALL Visio documents using code (VBA or otherwise)?? Or is there an alternative I might not have considered? Unfortunately an Add-in is not really an option due to available resources.
You have a couple options here:
Force every user to allow programmatic access to the VBA project for their documents, and use VBA automation to add code. This works nicely when you have programmatic access, but this can be difficult to assure.
If you're not using Visio 2013, you can actually save a document as VDX (xml) and replace the data for the VBA project with your own (you'd save out a document as VDX manually, and copy out the chunk of data for the VBA project). As I said, this wouldn't work with Visio 2013 since they seem to have eliminated the VDX format. You probably can get away with something similar with the VSDX XML format for 2013.
You can 'migrate' everyone's documents to a new VST file you provide. This would just involve copying and pasting all the content from a document into the new document that has your code in it. You have to be careful though to make sure all the document- and page-level data comes along, too (meaning DocumentSheet and PageSheet and any Document XML properties that may be important, and attributes like Author, Description, etc...)
Item 1 is the easiest, aside from the pain getting programmatic access to VBA Projects, unless you can have people send you documents to migrate.

VBA favourite code management

Just like Visual Studio allows us to drag our favourite code to toolbox and then use it later in any project. Do VBA allows this kind of functionality by any chance.?
What is the best way to manage the favourtie/reptitive vba code which i can use it in multiple workbooks?
In Excel you can possibly use Personal.XLSB file which could be a kind of container for all subroutines which you refer to quite often. You can create and organise them in modules, class modules. Some UserForms can be placed there as well. Each time you open Excel Personal.XLSB would be the first opened workbook then.
How to create 'Personal.XLSB' if you don't have it? Go to excel, start recording macro but before you press OK choose something like 'Personal Macro Workbook' on the second list. Do not forget to save it each time you leave Excel to keep all changes in your code.
VBA doesn't have a similar feature, no.
You can export your classes and modules to standalone files, and import them into other VBA projects. And some apps, such as Microsoft Word, have features to share macros between documents, in the case of Word by attaching those macros to the Normal template. But there is no feature to reuse small snippets of code.
Have a look at MZTools ... there are versions for VB of various flavors, and for VBA. I'm not sure if it's suitable for handling huge numbers of code snippets but for smallish amounts it should be fine. It's free and has dozens of other hugely useful features.

Using data from Ms-Excel to fill a pre-defined template in Ms-Word through VBA

Summary: I have to fill reports in excel and then manually copy the same in doc as required by my seniors to track bugs in different applications. The excel and doc report is different for every application. So I want to save work as I fill a excel and export it in the agreed template in word.
Problem : I am trying to write a macro which will search some variables in pre-defined template (doc) and replace these variables with data from excel. The variables are nothing but some text like <-AppName->, <-AppID-> (self created). Now there is a twist as there is one sheet in excel which has issues marked as high medium and low. I want to export this list to same doc as three different tables marked as high medium and low.
Question: How to populate variables (as above) in doc using the data from excel via VBA? If at all I can populate variables how do I manage to export the data from excel to doc in three different tables since it will have multiple lines.
Thanks in advance.
I haven't gone in-depth to look at this but I don't see why it wouldn't work.
1) Set up your library references in your VBA project so you can access Word's VBA API.
2) In Excel VBA method, set up an array of your search/replacements.
3) In that same module, open the Word file(s) and replace the variables. Like I said I haven't fiddled around with the API so I can't say if you can do this easily...but also check this out.
4) Save the word documents as new files, close them.
I'm not sure what you mean "three different tables" but once you figure out how to find/replace that part should be a breeze.

How to Reuse Code with VBA

What is the best way to avoid duplicating code when working in VBA?
I'm used to languages where I can just add an import statement and get access to all a class's public properties and functions, so I can just create a utility class with some common functions and have access to that in any project I choose to import it to. Any time I want to update one of those functions, one edit is all it takes to get it working across all projects.
Is there any good way to replicate this functionality in VBA?
What follows focuses on Excel but I am pretty sure the same would apply to any Office products.
The easy way is to save your reusable code as an addin (*.xla for Excel 2003, *.xlam for Excel 2007+). You then add the addin to Excel and all the spreadsheets you open will have access to the custom functions you have in your addin. If you add specific VBA code to a spreadsheet, you can add a reference to your addin and your VBA code will have access to all the public sub, function and classes of your addin.
In my organisation, we use 3 home made addins - they are stored in C:\Program Files\OrganisationName. And everybody has access to them. When an update is made, we only need to copy the new version to everybody's hard drive and restart Excel and they have the new version.
The addins contain utilities functions such as functions to:
read data from / write data to spreadsheets / files / databases.
usual data manipulation such as removing duplicates from a list
advanced statistical functions
etc.
A few drawbacks:
If you have several instances of Excel open, only one can update the addin, the other instances are in read-only mode
If Excel crashes, the auto recovery mode generally does not save the changes you made on your addin (TBC on newer versions) - there are a few tools to auto save regularly
An alternative is to develop xlls or COM libraries in VB or C# for example, but this is something I have not tried.
There are plenty of tutorials online if you need a more detailed procedure.