General VBA & Programming

Count Files (with a specific extension or not) in a folder and subfolders

| |

Introduction:
This macro can be used to count how many files exist in a folder and subfolders. In addition, if passed the file extension, it can also be used to count only files of a specific type. (For example "xls" files.) If you are looking for a macro that counts files within a specific folder only (ignoring subfolders), please see this entry.

Macro Purpose:

  1. Count all files in a folder and subfolder
  2. Count all files of a specific type in a folder and subfolders

Examples of where this function shines:

Break All Links In An Excel Workbook

|

Macro Purpose:
This code quickly replaces all external links in an Excel workbook with their values. It is something that can be very handy when you need to send a heavily linked workbook outside of your immediate network, to a user who doesn't have access to the directory containing the linked files, of if you want to solidify values so that they don't change next time you open the file.

Note: If you also want to change the colour of the background cell (so that you can see where the link was,) as well as places the original link path in a comment, please see this article.

Count Files (with a specific extension or not) in a folder

| |

Introduction:
This macro can be used to count how many files exist in a folder and, if passed the file extension, can also be used to count only files of a specific type. (For example "xls" files.) I've actually published two articles along this vein at [url="http://www.vbaexpress.com"]VBA Expresss[/url], but this one uses the File System Object (some know it as the File Scripting Object, or just FSO,) to do it. The biggest reason for the re-write is that I needed to use this in an Excel 2007 instance, which does not support the FileSearch method.

If you are looking for a version to count the files in a folder AND subfolders, then look here.

Print To PDF Using Microsoft's PDF/XPS Add-in

| |

Introduction:


This article contains code that can be used to print worksheets or entire workbooks to a PDF file, using Microsoft's free Save as PDF or XPS add-in for Office 2007. These routines will NOT work in versions of Office prior to 2007. If you are running an earlier version of Office, and are running on Windows XP, then have a look at my PDF Creator articles.

Versions Tested:


These routines have been tested successfully using the following versions of the software:

Send Email Via Groupwise

| | | | | |

Macro Purpose:

  1. This is a stand-alone routine that will allow a user to automate the process of sending an email via the Novell Groupwise email client using VBA.

Examples of where this function shines:

  1. Assume that you want to email out a notifcation to a mailing list that you maintain in Excel or Access. This routine can be configured to send to one or more users at the same time, including one or more attachments.

Macro Weakness(es):

  1. Passing a file with commas in the filename will fail since arguments are passed to the function as a comma separated string.

Error Messages: User-Defined Type Not Defined

| |

There are a great many errors that we can run into when programming, and just on of those is a Compile Error, specifically "User-Defined Type Not Defined".

This error could mean one of a few different things, which may not all be listed here. Two of the most common, however, are:

1. You are trying to create your own Constants and did not declare the Enum.
For an example of this, see my article on Creating Your Own Constants For Functions

2. You are automating an external program, (using Early Binding,) and forgot to set a reference to the required library.

Error Messages: Can't Initialize PDFCreator

| |

A problem than can occur when working with PDFCreator via code is that sometimes the code releases PDFCreator too early, and it isn't properly closed.

What is happening?
At the end of the PDFCreator routines on this site, we release PDFCreator with (a variation of) the following line:

Set pdfjob = Nothing

As long as the PDFCreator.exe task is (still) running, we'll get the error "Can't initialize PDFCreator." every time we try to run the PDF creation code:

So if you're seeing the above error, the issue is that a PDFCreator process is running on your system. If you opened PDFCreator manually, you may not have closed it. Or, if you had previously run a PDFCreator routine, then it had not finished doing what it needed to do, so the process that was actually running on the system stayed alive. We can check this by going into the Task Manager, (right click the taskbar and choose Task Manager,) and verifying that a PDFCreator.exe task is running.

Using a Treeview Control

|

Introduction:
This is a tutorial on the creation of a simple Treeview control on an Excel userform. For the purposes of this example, we'll be populating the Treeview control with worksheet names and the addresses of cells with formulas in them.

The article is intended for intermediate level VBA coders, meaning that you should be comfortable with navigating the Visual Basic Editor (VBE), and preferably have created a least a few userforms in your experience. With the exception of Treeview specific items, I will not be covering terminology or explaining things in great detail, so if you're a beginner, you may need some additional help to follow all the steps.

Creating Your Own Constants For Functions

| | | | | |

Introduction:
One of the items that is very useful in code is the use of constants. These items are simply text masks that translate to numbers, making it easier for us to remember how to interact with the code.

For example, if you put "? vbOkOnly" in the immediate window, you'll see that it resolves to 0. Likewise, "? vbYes" resolves to 6. It's much easier to remember the words than the numbers, which is why Enumerations (or Enums) exist.

Creating your own Constants
The good news is that beginning with Excel 2000, we have been given the ability to program our own Enumerations to help in our coding!

Using Help in Your Applications

| | | | | |

Introduction:
One of the biggest parts lacking in user constructed solutions is documentation in the form of help files. As coders, we usually find it easier to build the application than explain to someone how to use it properly. After all, what we write in code is rarely read by others, and most novices think it's gibberish anyway. Actually explaining what you do in plain english (or whatever language you use) can be quite difficult. Ironically, it is this very fact that tells us why help files are important... write it down once, nicely, and you should never have to explain it again.

Syndicate content