Variable declearation and option explicit in vba excel 2013 [duplicate] - vba

So I'm having to run someone else's excel app on my PC, and I'm getting "Can't find Project or Library" on standard functions such as date, format, hex, mid, etc.
Some research indicates that if I prefix these functions with "VBA." as in "VBA.Date" then it'll work fine.
Webpages suggest it has to do with my project references on my system, whereas they must be ok on the developer's system. I'm going to be dealing with this for some time from others, and will be distributing these applications to many others, so I need to understand what's wrong with my excel setup that I need to fix, or what needs to be changed in the xls file so that it'll run on a variety of systems. I'd like to avoid making everyone use "VBA." as an explicit reference, but if there's no ideal solution I suppose that's what we'll have to do.
How do I make "VBA." implicit in my project properties/references/etc?
-Adam

I have seen errors on standard functions if there was a reference to a totally different library missing.
In the VBA editor launch the Compile command from the menu and then check the References dialog to see if there is anything missing and if so try to add these libraries.
In general it seems to be good practice to compile the complete VBA code and then saving the document before distribution.

I had the same problem. This worked for me:
In VB go to Tools ยป References
Uncheck the library "Crystal Analysis Common Controls 1.0". Or any library.
Just leave these 5 references:
Visual Basic For Applications (This is the library that defines the VBA language.)
Microsoft Excel Object Library (This defines all of the elements of Excel.)
OLE Automation (This specifies the types for linking and embedding documents and for automation of other applications and the "plumbing" of the COM system that Excel uses to communicate with the outside world.)
Microsoft Office (This defines things that are common to all Office programs such as Command Bars and Command Bar controls.)
Microsoft Forms 2.0 This is required if you are using a User Form. This library defines things like the user form and the controls that you can place on a form.
Then Save.

I have experienced this exact problem and found, on the users machine, one of the libraries I depended on was marked as "MISSING" in the references dialog. In that case it was some office font library that was available in my version of Office 2007, but not on the client desktop.
The error you get is a complete red herring (as pointed out by divo).
Fortunately I wasn't using anything from the library, so I was able to remove it from the XLA references entirely. I guess, an extension of divo' suggested best practice would be for testing to check the XLA on all the target Office versions (not a bad idea in any case).

In my case, it was that the function was AMBIGUOUS as it was defined in the VBA library (present in my references), and also in the Microsoft Office Object Library (also present). I removed the Microsoft Office Object Library, and voila! No need to use the VBA. prefix.

In my case, I could not even open "References" in the Visual Basic window. I even tried reinstalling Office 365 and that didn't work. Finally, I tried disabling macros in the "Trust Center" settings. When I restarted Excel, I got the warning message that macros were disabled, and when I clicked on "enable" I no longer got the error message.
Later I re-enabled all macros in the "Trust Center" settings, and the error message didn't show up!
Hey, if nothing else works for you, try the above; it worked for me! :)
Update:
The issue returned, and this is how I "fixed" it the second time:
I opened my workbook in Excel online (Office 365, in the browser, which doesn't support macros anyway), saved it with a new file name (still using .xlsm file extension), and reopened in the desktop software. It worked.

Even when all references are fine the prefix problem causes compile errors.
What about creating a find and replace sub for all 'built-in VBA functions' in all modules,
like this:
replace text in code module
e.g. "= Date" will be replaced with "= VBA.Date".
e.g. " Date(" will be replaced with " VBA.Date(" .
(excluding "dim t As Date" or "mydate")
All vba functions for find and replace are written here :
vba functions list

For those of you who haven't found any of the other answers work for you.
Try this:
Close out of the file, email it to yourself or if you're at work, paste it from the network drive to your desktop, anything to get it to open in "protected mode".
Now open the file
DON'T CLICK ANY ENABLE EDITING OR THE YELLOW RIBBON
Go to the VBA Editor
Go to Debug - - Compile VBA Project, if "Compile VBA Project" is greyed out, then you may need to click the yellow ribbon one time to enable the content, but DO NOT enable macros.
After you click Compile, save, close out of the file. Reopen it, enable everything and it should be OK. This has worked for me 100% of the time.

In my case I was checking work done on my office computer (with Visio installed) at home (no Visio). Even though VBA appeared to be getting hung up on simple default functions, the problem was that I had references to the Visio libraries still active.

I found references to an AVAYA/CMS programme file? Totally random, this was in MS Access, nothing to do with AVAYA. I do have AVAYA on my PC, and others don't, so this explains why it worked on my machine and not others - but not how Access got linked to AVAYA. Anyway - I just unchecked the reference and that seems to have fixed the problem

I've had this error on and off for around two years in a several XLSM files (which is most annoying as when it occurs there is nothing wrong with the file! - I suspect orphaned Excel processes are part of the problem)
The most efficient solution I had found has been to use Python with oletools
https://github.com/decalage2/oletools/wiki/Install and extract the VBA code all the modules and save in a text file.
Then I simply rename the file to zip file (backup just in case!), open up this zip file and delete the xl/vbaProject.bin file. Rename back to XLSX and should be good to go.
Copy in the saved VBA code (which will need cleaning of line breaks, comments and other stuff. Will also need to add in missing libraries.
This has saved me when other methods haven't.
YMMV.

Related

A reference to 'C:\Solver32.DLL' could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component

Still green when it comes to programming, so bear with me. I'm currently trying to create a program in Visual Studio 2017 (VB.NET) intended to make modifications to a paired Excel spreadsheet using the Solver add-on. I've successfully created a reference for the Excel reference library but am unable to do so for the Solver reference library. I've tried creating references for both SOLVER32.DLL and SOLVER.XLAM, but both produce the same general error message:
"A reference to 'C:\xxxx.(DLL/XLAM)' could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component."
I've browsed for hours looking for viable solutions, some of which seem to suggest repackaging the .DLL file (use of TlbImp.exe) through use of CMD and Native Tools CMD for VS (run as Admin), but that hasn't generated success (generated following error: "The input file 'C:\xxxx.DLL' is not a valid type library.") Some of the posts I've read on linking the Solver suggest the .XLAM file is specifically what needs to be used, but that hasn't generated any success, either.
I've reinstalled Microsoft Office (both x32-bit and x64-bit), reinstalled Solver multiple times, completely closed and reopened Excel with each Solver reinstall.
If it should help, I'm using an updated version of Microsoft Excel 2016 (now x64-bit) and running Windows 10 (also x64-bit, of course). The file path for Solver is MicrosoftOffice\root\Office16\Library\Solver. I've tried linking the files while Excel is open as well as when it is closed.
I've tried switching gears and using Solver Foundations instead (for which I can successfully create a reference), but there doesn't seem to be a compatible command for my project (or at least one I can wrap my head around; I'm looking for the equivalent of 'SolverOK.'
Any help here would be greatly appreciated -- starting to lose my mind over this!

EXCEL User Defined Function - Can't find project or library without any MISSING reference

I developed a user defined function in my computer and save the document as XLSM. When I distributed it to other users in my company, they get "Can't find project or library" error as long as they open the workbook and then hit "Enable Macros".
If they hit Debug, the yellow line will fall on the head line of the user defined function, like this
Then I checked the Tools - Reference of VBE, but find nothing missing in all computers (mine and other users').
But the Reference is a little bit difference,
In my computer, it is
In other users' computer, it is
The only difference is the highlighted line. In my computer it is Microsoft office 16.0 Object Library, since I installed Project 2016 with Excel 2013. While others' are 15.0.
By the way, I get the above picture before I hit the "Enable Macro" button since once I hit it, the error message will show for infinite times and I will never be able to go into the Reference in VBE. Therefore it is possible that there will be a MISSING reference when the code is actually running.
This problem is very likely caused by the different versions of Microsoft Office Object Library.
You can try to export the VBA code and remove the VBA module in your xlsm file, and then reinsert the VBA code back in users' computers. Usually the problem will be solved.
The reason why this problem occurs is that Excel actually DOES save the compiled code of VBA, though we never see any compiled codes or compiled packages. When other users open your Macros Enabled file, Excel will automatically run the compiled machine code instead of the readable source code. Due to different interfaces (I guess it is called interface?) for different libraries in machine codes, Excel cannot find the library and error occurs.
So you can also try to decompile your VBA code before you distribute your XLSM files. Visit this page to download the tool for decompiling your XLSM file:
VBA Decompiler
This likely happens when you use early-binding somewhere in your code (I don't see this happening in the codepart on the screenshot).
A workaround would be to change your code to use late-binding. Please have a look at the answer of Siddharth Rout in
Preventing Excel VBA compile errors due to users having an older version of MS Office (MS Outlook) installed? for more information.

I don't have Powerpoint object library on the list of available references, even though I have installed

I try to write an Excel VBA program that automatically creates a Powerpoint presentation. As far as I know, at first I should go to tools -> references and on the list of available references find and switch on the reference to the Powerpoint object library. However, on the list of available references I don't have the Powerpoint object library. What can I do to have it available? I have Powerpoint install and it works without problems.
Yes, I know I can use Powerpoint from Excel VBA macro without referencing to this object library (I can use late binding), but I would like to avoid that.
Edit:
Hm... just several minutes after posting this question I can see I have this reference on the list of available references. But I am quite sure that before I didn't have it. Is it possible that it became available now because just one minute ago I started PowerPoint?
The PowerPoint library file is msppt.olb.
If, for whatever reason it wasn't shown in this list, you could add it by running the regsvr32 command to register this particular file, or re-install PowerPoint - both should do the trick. Registering a COM file in this way puts a whole bunch of additions into the registry to let Windows know where the file is, how it can be accessed & what it's dependencies are.
In this case (i) I'm glad to hear you've already fixed the problem and (ii) sorry but it's unlikely - I'm guessing you just didn't spot it in the list the first time around (which is pretty easily done).

Importing Autodesk.AutoCAD in VB.NET

For my project, I need to generate CAD drawing (almost a blueprint of a tank) through my VB.NET application. The app takes in some user input & based on this, it generates the geometry of the tank.
I am using Microsoft Visual Studio Professional 2012 & coding in the VB.NET language.
I have browsed through a lot of AutoCAD documentation but could not find exactly how & where are the modules hidden, for me to add them into my code.
I stumbled accross one particular document
which is where I found how to add the relevant files as reference to my VB solution. However, after adding some particular files I could then do Imports AutoCAD, but all the docs show that I must use Autodesk.AutoCAD. On inspection of the available modules/methods of the Imports AutoCAD I can see that it isn't complete as needed for the application development.
Can somebody please guide me as to how could I find or go about adding correct reference files to my VS solution to get full functionality from the AutoCAD modules? Thanks!
I posted this quite some time ago. It contains the information that you need
How to close a file in Autocad using C# keeping acad.exe running?
You can also download any of the AutoCAD .NET API's from here:
http://usa.autodesk.com/adsk/servlet/index?id=1911627&siteID=123112
Just in case someone else need the help.
Download the appropriate AutoCAD .NET API using the aforementioned link.
Put them in a location of your choosing.
general:
on the appropriate project in your visual studio's solution, right click -> Add Reference.
select Browse on left, click the browse button and go to the path where you saved the ObjectARX download.
Inprocess:
Navigate to the inc directory and
select the AcDbMgd.dll and AcMgd.dll then click add.
For these make sure to set the copy local property to false.
Interop:
Navigate to the appropriate directory: inc-win32 or inc-x64 depending on your processor.
select the Autodesk.AutoCAD.Interop.Common.dll and Autodesk.AutoCAD.Interop.dll then click add.
and unless you have an interest in the AutoCAD interface don't worry about the AcCui.dll
Hope this helps some one out :)
So, looking up more & more documentation led me to stumble across this
I found the dlls by simple file search for respective dlls metioned below in my local AutoCAD 2014 installation folder (C:\Program Files\Autodesk\AutoCAD 2014)
As per the documentation, following dlls are important ones -
AcCui.dll
acdbmgd.dll
acmgd.dll
Autodesk.AutoCAD.Interop.dll
Autodesk.AutoCAD.Interop.Common.dll
The credit for the last 2 dlls goes to Trae Moore.
How to add references to the dlls was mentioned in the documentation link.

Programmatically fill in Word template in redistributable app

I need some advice on how to go about a particular issue I'm trying to build a work around for, but keep finding myself between a couple of poor options.. The application has a large collection of word doc templates. The application is to on the fly generate one of the templates and fill in the bookmarked values.
I have a few options on how to go about this...
Use the Interop Library: I pretty easily fill in an actual, templated document with the Interop library by going through and grabbing all the bookmarks and filling them in and saving the document again.
Issue: I need to be able to support version of Word from 2003-2010; version becomes a concern here... if I link against the wrong version of the Interop.Word, might it cause compatibility issues for users of older versions? I don't have much to test with here, so I'm not sure...
VBA Macro: I've actually used this method to build excel spreadsheets before; write fields to a csv, read into a new document generated from template via VBA, and ta-da, new document.
Issue: Security settings on end-user PCs are likely to be extremely strict. VBA likely to be disabled.
Is there a better way of going about this that I'm not seeing? Is the Interop version likely to be less of a big deal than I thought? Other than OfficeXML, which is a catastrophe for the documents we're using (I've looked..), is there another good possibility?
Edit: submitted early.. damn browser -_-;
VBA is usually blocked in documents and templates that are opened. But templates in the Word startup directory are allowed. I'm not sure about Word 2007 and 2010, but in Words 97 to 2003 there was a wrdstart directory that could contain templates that were loaded on startup of word. The normal.dot file was in there, and you could add your own to contain scripts, macros toolbars and other stuff.
I think this hasn't changed in essence since then, although I don't do a lot of Word development anymore, so I'm not sure.
[edit]
Yes, it still exists. In Word 2010 you'll find the setting in File -> Options -> Advanced -> File Locations (button). There is a 'Startup' setting, pointing to a directory. Templates in this folder will be loaded on startup and their macro's should be able to be executed.
Check File -> Options -> Trust Center -> Trust Center Settings (button). There, you can specify allowed documents, and trusted location. The wrdstart directory is in there, but you can add your own.