gkrogers
gkrogers

Reputation: 8356

Spurious "User-defined type not defined" error in Microsoft Word VBA

I have a Microsoft Word template with some code and some references, that has been working fine for months but has just started throwing up a spurious "User-defined type not defined" error whenever I open it or try to compile it.

I know it's a spurious error because I haven't made any significant changes to the code. In fact, I've rolled the code back to the last deployed version (which I know works fine) and I still get the error. I've also commented out all the code in the template and I still get the error. I've also removed and re-added all references (same error), and removed all the references and added them back, one by one, until the resultant compile errors are resolved, at which point I'm left with the spurious "User-defined type not defined" error. (I'm going to call this a UDTND error, from now, to avoid driving you all mad.) I think the error started popping up after I rebooted my PC. It only happens with this template, but I don't see how it can be anything to do with this template.

Interestingly, the error is subtly different from a genuine UDTND error in the following ways:

I've tried Googling it but I just get a bazillion results from novice developers asking why they get this error, with responses telling them that they either need to declare the missing type, correct the spelling of the offending variable type, or add a reference to a missing library. I've been banging my head against my screen all afternoon, and that's helped about as much as all the other things I've tried (i.e. not at all). I have a feeling that this is something to do with a messed-up reference, but afaict they're all fine, and I've removed and re-added them, which I would expect to resolve that sort of problem.

Any ideas...?

Upvotes: 2

Views: 6486

Answers (6)

Cool Blue
Cool Blue

Reputation: 6476

I had the same problem with Excel 2013.

It started when I did a search and replace on the name of a Custom Class. I changed the name of the Class after I did the search and replace on all references to it and the spurious error started immediately after that.

I reverted to an earlier version ad confirmed that the problem was not there and then did the same search and replace and re-name and got the exact same behaviour again.

The Custom Class that I changed the name of only had one consumer and it was also a custom class. I exported, removed and re-loaded the sole consumer class and the problem was fixed.

Upvotes: 0

Lance Roberts
Lance Roberts

Reputation: 22842

Check out this link for a Microsoft bug that might be related.

TLDR:

The reference to a package/addin/whatever probably needs to be re-referenced. Check Tools -> References in the Menu.

Also it appears that if you install Microsoft Security Advisory 960715, that certain controls are killed. There are fixes which may or may not work for you. A good article is on this blog:

VSOD Blog

Upvotes: -1

rdongart
rdongart

Reputation: 400

I had a very similar problem.

My problem appeared (I think) just after I made a Search and Replace that I canceled (Ctrl+Z). There was not highligting of the problem, only the ""User-defined type not defined" error message when I compiled.

I tried:

1) restarting computer
2) changing reference ordering
3) removing functions/procedures, modules one at a time.

Didn't work. My project was written in Excel VBA and here was the solution I found.

THE SOLUTION:

I opened a new Excel file and opened the Visual Basic Editor. I then copied all Forms, Modules, and Class Modules one by one into the new file. I then Copied the Control Objects (3 Commandbuttons) from the old sheet into the new one. Now the new file was identical to the old project - only the ""User-defined type not defined" had gone and problem was solved.

Upvotes: 1

5Arete23
5Arete23

Reputation: 11

I have had "User-defined type not defined" problem on several occasions when compiling Microsoft Visual Basic 6 (MSVB6) code that was compiling without a problem earlier. It seems to happen after I have had a long coding session without rebooting the computer. As you can guess, I have been using Microsoft operating systems. I currently am using Windows XP. Rebooting the computer usually fixes the problem as it so often does on Microsoft operating systems.

I have read that fully qualifying declarations also can help, e.g., "Dim oBar as Foo.Bar" instead of "Dim oBar as Bar". I have not tried this approach however.

Upvotes: 1

user183402
user183402

Reputation:

Yea references would be the first step in troubleshooting this problem as already stated, but failing that id start commenting out the code in any event procedures running at start up (my experience is only with Access VBA though)

Upvotes: 0

Smandoli
Smandoli

Reputation: 7019

Your trouble-shooting on References is sound. Once upon a time (and I don't recall the precise error) I was at the same point, and the reference ordering was the key. When you're designating References, you'll notice a "Priority" adjustment feature. Experiment with that and you may solve this.

Upvotes: 1

Related Questions