How to change the "fullpath" property of References in VBA (Tools -> Refences)? - excel

I'm sure that there are different ways to phrase this question, but that is the end result that I want to achieve.
So, I have a setup where code is written in C# and added as functions to Excel. It relies on having a specific .tlb file in the Tools->References that can be found in the VBA window.
While I was testing this, the .tlb file (and the rest of them) was on my local drive, but now that the project is working, I need to transfer it to a network drive. The problem is that I can't find any way to change the actual file (or filepath) that is being referenced - it's always looking at my local path.
I've tried a few things:
Followed the steps listed here https://support.microsoft.com/en-us/help/308340/how-to-check-and-remove-incorrect-project-references-in-the-visual-bas
Tried several VBA codes using the .References.Remove expression. This does not actually remove the reference from the list, it only unticks it.
I've tried to remove the file from my local drive (causing an Excel error that a reference has been moved, deleted or renamed - good) and then add a reference from the new location that I want. This resulted in one of two things:
1) If I try to add it manually - nothing happens, the existing reference remains unticked and nothing new is added (that I know of).
2) If I try to do it via .References.AddFromFile "filepath" expression it ticks the reference, if it was unticked (this does not make the external formulas work), or an error that a reference with such a name already exists, if it was ticked.

Recompile on the network drive with the following silly way.
Open the VBA editor
Go into each module
Insert a line (doesn't matter what you write)
Press ENTER
Remove the line that you've inserted
When finished, in the menu click Debug \ Compile
Source: by Andreas Killer
https://answers.microsoft.com/en-us/msoffice/forum/all/ms-excel-error-cannot-run-the-macro-the-macros-may/3f3106b2-ae60-4d21-ac94-67e54e605922

Related

Excel VBA macro failing to save workbook on coworker's computer, but fine on mine

I'm encountering an issue trying to get some macros and VBA scripts working on a new coworker's computer. I've been using the same code for years on my machine and several others without issue, but when my new analyst tries to run it, it throws an error stating it couldn't save the file. He can manually save the file without issue, but running the script throws the error. The really weird part is that it lists the file path, but replaces the filename with a seemingly random hex byte.
On another file, if he opens it, closes with or without saving, then I open the file and try to save it using a macro, it fails saying "cannot access file...". If I copy and paste the file, open and run the macro again, it saves over the offending file no problem.
We've checked permissions and settings and haven't found anything that was different between the two systems. We're both on VMs. His hardware allowance is less than mine but otherwise they're both IT-managed and identical. I'm at my wits' end... Any advice on what may be the source of my grief would be helpful, even if not a solution.
A couple of things I'd check before trying to step through debugger mode.
Coworker does not have write access to the directory (I think you said you checked already)
The new users' Trust Center Settings are not correct
File > Options > Trust Center > Trust Center Settings... > Macro Settings > Ensure the box is checked to trust the VBA model and that macros are not disabled
The new user does not have the same libraries referenced.
Alt + F11 > Tools > References... > Check if coworkers' libs match yours
Is he using a new/different version of Excel that you? A lot of older code got bojangled when Office 365 hit the scene.
After checking settings, I would ask the following:
What error is thrown when coworker attempts to run the macro?
When does the error get thrown? It could be that you have code that is attempting to edit the file (or has another file stuck in an edit) at the same time you're trying to save it.
I would see why the file name is getting corrupted. It sounds like coworkers' machine is looking to a bit of memory or memory address that is somehow getting forgotten. Like the code is referencing a variable that is out of scope.
User Access Control has been enabled on his VM that is somehow preventing him from saving the file correctly.
Your code does not properly Quit the Excel application, so its still running in the background on coworkers' machine. Have them open/close without saving a file and check Task Manager to see if Excel is still up to confirm

Add-in macro conflict Excel

Initially I have 1 function: CusFun in an Add-in1
I copy all code in Add-in1 into module in Excel file, save file 1.xlsm
I open 1.xlsm to work, now I have 2 functions CusFun
I changed the name (and only the name) of the functions in macro in module1 in 1.xlsm as CusFun_Port
Now I have 2 UDF: CusFun and CusFun_Port when I work with 1.xlsm
But CusFun_Port (all of them) always return blank while CusFun work normally.
Why use macro-enabled file instead of add-in? Because I need to send the file to many users back and forth. I initially thought only I use them, using add-in save me the trouble of clicking "Enable content". With add-in, every computer use it must install it. And when the file is sent to another computer, all the custom functions in the file must be re-targeted to that computer's add-in location. My users are not proficient in any of those tasks. And those tasks takes far more time than clicking "Enable content".
What can I do to make sure that both functions in add-in and file-specific macro work?
Finally, I found it. Because I change only the name at the top of the function, it doesn't work. Because all of them are recursive, the function's name inside did not change, hence it doesn't work. There is no conflict here.

Using Directories in Excel

I'm trying to get some data out of excel files. Never used excel before but I understand VB. So I have a file with a button my boss implemented as well as his code he used. Here is the code:
(you can see the "Get Data" button in there)
My exact problem is that I don't know a lot about excel so if someone could help me figure out what is going on as well as figuring out how to correctly use directories that would be awesome! For more context when you click the button it gets all files in the directory and then the user clicks a file then excel follows a macro to get some data out of sed file.
Right now it returns nothing but there is 5 files in that directory?
Here is what the default directory looks like after edit:
And here is the files in my directory:
As #Tim Williams says ... Insert the back slash after the folder name
DirNow = Dir(Range("DefaultDirectory") + "\WFP*", vbDirectory)
EDIT
Basic troublseshooting
Does it it compile?
Looks like "UserFormDataa" - should maybe be "UserFormData"
Add Debug.Print DirNow after it's set and show us the display
If that's not right add line
Debug.Print Range("DefaultDirectory") to makes sure range is defined properly
Remove vbDirectory - unless you're going to handle processing files under the matching sub-folders
You are missing the terminal backslash on your default directory.
C:\Users\CUCCOMTT\Desktop\Excel Project\
Otherwise you're looking for files named
C:\Users\CUCCOMTT\Desktop\Excel ProjectWFP

Matlab: open files 'outside Matlab' by default

I'm looking for a way to have Excel files in my Matlab folder open 'outside Matlab' (i.e., by MS Excel in most cases) directly by double-clicking the file, rather than right-clicking and selecting 'Open Outside Matlab'.
The .xls files reader built in Matlab can be terribly slow for large files, and an unwanted double-click on a file can cost quite some time in which Matlab is unresponsive.
Thanks.
When you click something in the Current Folder tab, it's actually running the open command, which itself calls finfo to determine what it means by "open" for a given extension. You can see this by creating a breakpoint in open.m directly after the line [~, openAction] = finfo(fullpath); and double clicking - when it hits the breakpoint you'll see it returns openAction as uiimport.
In theory, you can create custom methods for extensions by creating on the path a function openabc where abc is the extension, which should be returned as the openAction.
However, if I look at my finfo.m it first searches for said functions and then regardless of whether or not it finds them if there is an inbuilt method it overwrites them with the standard behaviour. There's even a comment:
% this setup will not allow users to override the default EXTread behavior
If you are willing to muck about in the inbuilts, you may be able to do it like this (backup first! - this could affect other things). I did it temporarily by shadowing the existing finfo like this:
edit finfo.m (Now save a copy to the current folder)
Add these lines after the loop that defines the openAction (in my version, around line 85):
if any(strcmp(['.' ext], matlab.io.internal.xlsreadSupportedExtensions))
openAction = 'winopen';
end
From the folder containing your edited finfo.m, type which finfo -all. You should see two copies, the MATLAB one labelled as shadowed. Opening something from the current folder window should now open Excel externally.
I don't believe there's any straightforward way to do that. It's built in to MATLAB that Excel files will open in the import tool when you double click on them, and there's no way to change that.
You might be able to get around it by changing the file extension on your Excel files to something other than .xls or .xlsx. That would stop MATLAB from opening it in the import tool. Then in Windows, you could associate the new file extension with Excel.

Visual Studio extension code completion- multiple file data

I'm looking to create code completion for a custom language in Visual Studio. I already have code for some simpler editor interaction, like syntax highlighting and brace matching. Now I want to move to code completion. But I've run into a slight problem- data sources.
Like in a language such as C#, I implicitly share code between more than one file. This implies that, in order to code complete one file, I need to know the contents of the other files. More specifically, whilst I could simply iterate through the project and project items and crack open the files, this is a suboptimal solution. For example, I wouldn't be able to code complete unsaved changes the user has made. For another, I already did a lot of processing work lexing or parsing the contents to fill the user's request for syntax highlighting and whatnot, and I have no desire to duplicate that work.
How can I access the contents of the other files in the project, and obtain their ITextBuffers so I can re-use the work I already did?
There is no ITextBuffer provided for files which exist on disk but are not currently open in Visual Studio. It is possible to create an instance of ITextBuffer for any arbitrary file by using ITextDocumentFactoryService::CreateAndLoadDocument. This takes a string and will give back an ITextDocument instance from which you can access an ITextBuffer.
In order to find the existing ITextBuffer for files that are currently open you can do the following
IVsRunningDocumentTable::FindDocument This takes a file path and returns a cookie representing that file if it is currently open (Example)
IVsRunningDocumentTable::GetDocumentInfo This takes a cookie and returns back an IVsTextLines instance (Example)
IVsEditorAdaptersFactoryService::GetDataBuffer takes an IVsTextLines and gives back an ITextBuffer (Example)

Resources