Excel vba refreshing links window interrups macros - vba

I have a macros that opens a file, makes changes and saves it under some other name. It does so many times. The problem is, the file has links to other files, so when my macros opens that file Excel generates a window that asks whether or not those links should be refreshed.
It interrupts macros and requires manual mouse click, which is something I'd really wish to avoid. Is there any way to ensure it wouldn't happen within macros?
I tried
Dim cn As WorkbookConnection
For Each cn In resultWorkbook.Connections
cn.Refresh
Next
But it didn't work - the windows keep appearing.

Include the UpdateLinks parameter when calling Workbooks.Open. The UpdateLinks argument (emphasis added):
Specifies the way external references (links) in the file, such as the
reference to a range in the Budget.xls workbook in the following
formula =SUM([Budget.xls]Annual!C10:C25), are updated. If this
argument is omitted, the user is prompted to specify how links will be
updated. For more information about the values used by this parameter,
see the Remarks section. If Microsoft Excel is opening a file in the
WKS, WK1, or WK3 format and the UpdateLinks argument is 0, no charts
are created; otherwise Microsoft Excel generates charts from the
graphs attached to the file.

Related

Excel 365 Formulas - How to save LAMBDA function permanently (for all existing and new files)?

I would like a global solution every time I start Excel365 with my key add-in also firing up.
I defined several LAMBDA custom functions, saved them in my add-in, and I would like to make them available globally - not just only for the current workbook. Is it possible? I tried saving a workbook with definition as an add-in (.xlam) but the formulas are not available. The don't seem to be available when as an add-in. When I am in the add-in parent file on the desktop, all is good and everything is running. But once I post the file back for your as an add-in, The Lambda(s) and the defined name space go away.
You might try creating a template for excel to load when it is opened.
Customizing how Excel starts so it utilized the template.
My additional work around is also simple. If the LAMBDAs were saved in the original Workbook with "Workbook" as the saved parameter. Then each of those worksheets in the original LAMBDA workbook is "infected" with all of the workbook LAMBDAs. Therefore, I simply copy a blank worksheet from the original Workbook to the "new" workbook. Then by virtue of "infection" the new workbook is also newly infected with all of the new LAMBDAs. It works just like COVID, who knew?

Make VBA automatically accept opening writeprotected files

I have a chunck of VBA code, which opens 2 files, copy the content in each, and paste that into a third file.
The problem is, that the two files (lets say "alm" and "fiber") often are used by other users, thus when i use Set alm = Workbooks.Open(alm_path) I get an error, since Excel cannot open it. I assume it is due to the file being opened by another user, and I then have to open it as write protected. Is there any smart way to do so? I am fairly new to VBA code
As mentioned in the comments you can open the workbook as ReadOnly, an example below:
Dim alm As Workbook
Set alm = Workbooks.Open(Filename:=alm_path, ReadOnly:=True)

Excel - Keep macros enabled when sheet is saved as new

I have an excel macro-enabled sheet that users open, fill out a list of information (name, date, etc) and click a save button. This creates a new file name based on the information, and saves the file to a network location. The users then continue to work with the sheet.
I forced the users to enable macros (excellent tutorial here: http://datapigtechnologies.com/blog/index.php/forcing-your-clients-to-enable-macros/) and that works well, however once the file name changes, macros are disabled. I assume this due to the fact that it is an untrusted network location. The sheet will be used by many different computers, so I would like to find a vba workaround rather than relying on an excel setting.
Edit: I think I have the answer: As #mehow said based on the link I have posted, I can first run the part of the macro that finds the file name, then lock the workbook all except the start page, then save the workbook as the new name, then hide the start page and show everything else. That way if macros are disabled once the sheet is saved it should force the user to re-enable without compromising the protection. I'll post once I find outwhether it will work.
Edit: It works! Thanks.

Standalone code for Excel

Can VBA code be written to perform actions on any Excel file?
When I create a project in Visual Studio, it asks for an Excel file to be linked to it. All the code that I write is in ThisWorkbook.vb and hence acts only on the Excel file linked to the project.
Ideally, I want a script that:
When the user double-clicks, he/she should be allowed to select an Excel file of choice for the actions to be performed on that file.
If the above is not possible, I'd at least like to invoke the VBA script from within an Excel file.
Basically, the VBA code should be independent of any Excel file.
I looked at VBScript, but the syntax for it seems to differ slightly.
You've mentioned Visual Studio, VBA, and VBScript. The solution I'm outlining works directly with VBA rather than Visual Studio or VBScript. (However, you might adapt Visual Studio (C# or VB) along the lines of what I'm outlining below.) Hope it's helpful, so here goes:
Here's what I do, and, it ultimately results in an .xlam Excel AddIn as #chris above has commented.
Overview
I start with ordinary .xslx workbook, to which I add a VBA project, making it an .xlsm. Then create some VBA Subs (macros). Then create some Excel QAT (Quick Access Toolbar) buttons for the workbook, which are bound to (i.e. they invoke) the VBA subs/macros. Then convert the workbook (with VBA in it) to an .xlam, which is an Excel AddIn. When you're all done, the buttons are accessible from any workbook (and the VBA code has access to any user workbooks as well as those originally in your .xlsm). Further, the workbook associated with the .xlam is invisible. So it just looks like you've added some buttons to the QAT that appear on all users .xlsx windows. The .xlam is pretty easy for users to install (though I provide a buttons to uninstall/reinstall/check version). You can upgrade an .xlam independently of users' workbooks; users' workbooks can thus be data only (.xlsx, no VBA).
Details
Write some Excel Subs you want to use later
You need to be aware that the buttons can only invoke macros (VBA Subs) without parameters, so the macros will have to check things like ActiveSheet and ActiveWorkbook and Selection to figure on what sheet the button was pressed, hence what user data to really operate on. (If you need to refer to your workbook with the VBA code in it, use “ThisWorkbook”). You should be aware that there can be naming conflicts, so try to name the parameterless subs with rather long names, such as MySomewhatUniqueProjectName_button1, etc…
Add Buttons to your .xlsm
Using Excel 2010 (I think this works with 2007 or later), I put workbook-specific buttons on the QAT part of the ribbon, which connect to macros (VBA subs) in the VBA code.
To do this, you from the Quick Access Toolbar customization drop down (the tiny down arrow at the very top row of the Excel window, the last icon from left to right) choose "More Commands…". When the “Customize Quick Access Toolbar” dialog box comes up, from the (2nd) "Customize Quick Access Toolbar:" heading (top to the right), choose "For XYZ.xlsm" from the dropdown instead of the "For all documents (default)". And from under "Choose Commands From:", use "Macros" (instead of “Popular Commands”) from the dropdown. Once you have those two things selected, you can move VBA subs from the left box to the right box using “Add >>”. Each so moved will become buttons visible in your QAT. As you’re doing this you can also edit the icon and text for the buttons, add a separator as needed (I always end with a separator in case other .xlam’s use the QAT). (Now is a good time to save this .xlsm.)
Convert the .xlsm into a .xlam
Then I convert the .xlsm to an Excel add-in, by merely saving it as an .xlam file. This will end up (1) hiding the workbook associated with the code you have (though it is still accessible to itself.). Further, now, (2) the (invisible, as now it's an .xlam) workbook will load whenever Excel is loaded. (To keep this fast for when users use Excel but don’t run my VBA code, I don't run any code when the .xlam is loaded, I only run code when a button is pushed.)
You can manage the AddIn using Excel’s AddIn manager. To update the AddIn, you have to use some trickery. While you can copy over it when Excel is not running, on the other hand, you cannot directly uninstall the AddIn, you can only disable it from Excel. But once disabled, you can delete the .xlam, and relaunch Excel, go to the AddIn manager to try to work with the (now gone) AddIn and you’ll get Excel saying it can’t find it, so do you want to delete it. Say yes, and it will be uninstalled.
FYI, Notes
I keep the .xlsm to edit later, but you can actually debug and edit the .xlam and later convert it back to an .xlsm with a minor bit of trickery: find its "ThisWorkbook" entry in VBA, and then the "IsAddIn" property, set to false, its workbook will suddenly appear and you can save as .xlsm, or edit its workbook and set the property back to true to resave as .xlam directly.)
Answer 1
You can do that in VB.Net too. Just make a regular project. (comment by #SLaks)
This worked for me very well and was exactly what I was looking for.
Answer 2
The very descriptive answer posted by #ErikEidt
I haven't tried this, but it seems like a good and alternative way of getting macros to work.

Accessing Excel Object without using Shape.Activate() on a Word document using VBA

I have a tried reading an embedded excel document in a word document. I followed the code specified at this blog article:
http://vbadud.blogspot.com/2010/08/how-to-read-excel-sheet-embedded-in.html
Dim oWB As Excel.Workbook
Dim oIShape As InlineShape
For Each oIShape In ActiveDocument.InlineShapes
If InStr(1, oIShape.OLEFormat.ProgID, "Excel") Then
oIShape.OLEFormat.Activate
Set oWB = oIShape.OLEFormat.Object
oWB.Sheets(1).Range("A1").Value = "ProdID"
End If
Next oIShape
It works fine but the Activate line causes the document to flicker on each excel document I read. I tried to remove the oIShape.OLEFormat.Activate code but it causes the next line to throw a "Runtime error '430' (class does not support Automation or does not support expect).
The question is there any other way to access embedded excel without calling the Activate method?
This is tricky! The short answer is, no. Not with an embedded Excel.
I did some experimentation and some research. Since I could not find any sources that specifically explained the behavior. this is somewhat a guess on my part. It appears that when you embed the Excel spreadsheet into your word document essentially Word stores a link of spreadsheet, which displays only the appearance because it needs to be interpreted with the Excel program. Until you actually active the shape, you cannot interact with it because that cannot be done with Word directly. This article alludes to the behavior, but doesn't explain it. Here's a quote:
If you edit the object in Word, click anywhere outside the object to return
to the destination file.
If you edit the object in the source program in a separate window,
click Exit on the File menu of the source program to return to the
destination file.
You may have noticed that even if you use. Application.ScreenUpdating = false it still does the flickering you mention. This is because you are using a different application when you access the shapes! Every time you active the shape, the object specific menus etc load.
A possible work around:
If instead of embedding Excel Spreadsheets via the insert menu, you can instead add a control. On my machine using Office 2003 the comparible one is: Microsoft Office Spreadsheet 11.0 This is technically a web control, but the methods and behavior are very comparable to an Excel workbook.
Using the control instead of the handy inserted object, with a slight variation of your code I was able to comment out your activate command and the code ran as expected. Specifically, I had to change these lines:
Dim oWB As Spreadsheet instead of Excel.Workbook.
If InStr(1, oIShape.OLEFormat.ProgID, "OWC11.Spreadsheet.11") Then instead of "Excel"
Basically you can decide... Activate your embedded object that requires Excel to interpret, or use a different control that doesn't require activation.