Excel - VBA

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:

Disable the Excel Compatibility Checker

|

I've received several comments on my blog that people dislike Excel 2007's Compatibility Checker, and would like to turn it off globally. While it's true that you can turn it off for each workbook, the main user interface seems devoid of a way to actually tell Excel that you never want to check your workbooks for compatibility when saving to 2003 or earlier versions.

This issue seems to hit me most when trying to an Excel 2007 workbook that uses conditional formats or colours (colors) back into the Excel 2003 file format.

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.

Have Excel read a range of data to you

|

Introduction


When keying in large amounts of data, it can be helpful to have Excel read the input back to you... either as you go, or after you have finished. In either case, this lets you focus on reading the numbers from the page, comparing what you have typed to the original values.

This article covers both scenarios individually.

Macro Weaknesses


  1. The speech application reads information back phonetically. So if you are concerned about having names or words actually pronounced correctly, you may have to modify them a bit in the text.
  2. The speech application reads quickly enough, but there is a noticeable delay between when the speech ends and when text will actually start registering in the cell. It may not be a good idea to activate immediate read-back if you are a quick typer.

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:

Force user to enable macros

Introduction:
There are times when you may need to force your users to enable macros to work with your files. Maybe you've programmed some special functions to work with your file, for example. Unfortunately, Office's security settings can get in the way if you really need your macros enabled, as your users could have their system set up to disable macros by default, or they could choose to disable them for some reason. The macro solution below gives you a way to deal with this issue.

Macro Purpose:

  1. Notify the user that they need to enable macros to work with your file.

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.

Syndicate content