Excel 2016 custom task pane - excel

I have problem with one CustomTaskPane in mutiple Excel 2016 documents. I create and display task pane with my WinForm user control
Dim cll As cMyControl = New cMyControl
Private Sub ThisAddIn_Startup() Handles Me.Startup
CustomTaskPanes.Add(cll, "Panel")
End Sub
This panel I can see in all workbook.
Private Sub Application_WorkbookActivate(Wb As Microsoft.Office.Interop.Excel.Workbook) Handles Application.WorkbookActivate
CustomTaskPanes.RemoveAt(0)
CustomTaskPanes.Add(cll, "Panel")
End Sub
The problem is that the panel is created but empty. Is any way how to transfer user control between CustomTaskPanels? Or how to attach created panel to another Excel window? Because the I don't want to create new cll object for each workbook activation.

Excel 2016 is a SDI and then you need to store a reference of each taskpane. You need to use Globals.ThisAddIn.Application.Hwnd to identify the Excel window.
See here for a solution : https://stackoverflow.com/a/24732000/3205529

Related

Connecting password-protected Excel to Access

I want to link a password-protected Excel document to Access - this is not working due to the it being password-protected.
Does anyone have an alternative method to do this?
It has to be password-protected unfortunately and cannot be moved to a secured folder so not sure how to do it.
Any advice?
Try using a hidden form to automatically open the Excel file when the database file is opened.
Add this to a new Module:
Option Compare Database
Public xl As Object
Function OpenExcelFile()
xl.Workbooks.Open "path to file.xlsx", , , , "password"
End Function
Function CloseExcelFile()
xl.Quit
set xl=nothing
End Function
Create a blank form and set the HasModule property to true. then add the following to the form's code module.
Private Sub Form_Load()
OpenExcelFile
End Sub
Private Sub Form_Close()
CloseExcelFile
End Sub
Now create a new macro with an OpenForm task to open your form. Set the window mode to 'Hidden'. Save the macro with the name 'AutoExec'. This makes it run when the db is opened.
When the db is opened the macro will run and open the form hidden. The form load event will fire, creating a public Excel.Application object that opens your excel file (you should be able to remove the password from the code if you want the user to be prompted for it). The Excel application will remain open until the hidden form closes (when you close the database). At that point the form close event will fire, causing the Excel Application to quit. As long as the Excel file is open, you should be able to use linked tables and queries.
**You could add xl.Visible=true to the OpenExcelFile function if you want it to be visible to the user.
This link as a similar idea: https://www.connectionstrings.com/how-to-open-password-protected-excel-workbook/

How can I post-process the data from an Excel web query when the query is complete?

As a spreadsheet developer, I am trying to stitch together two sets of rows: one from a web query to a web service I own, and the other a set of manual rows added by the spreadsheet's user (not me).
Excel's built in Web Query / Connections object only provides two modes: I can turn on "Enable background refresh" which makes the web query asynchronous, or uncheck it.
With it unchecked, Excel freezes up while the query executes, which is undesireable. With it checked, there doesn't seem to be any kind of callback or event hook available to be notified, so that I can operate against the refreshed web data.
Is there another way to do this?
An Excel web query utilizes an object called a QueryTable to carry out the business of retrieving and displaying the data.
A QueryTable can be accessed by VBA.
And just like the chart object a querytable object has events that can only be responded to by using the WithEvents keyword from a class module, like so:
Private WithEvents MyQueryTable As QueryTable
Private Sub MyQueryTable_AfterRefresh(ByVal Success As Boolean)
'Do your post processing here...
End Sub
Excel supports the ability to open a URL as another Excel workbook, via the Workbooks.Open method:
From MSDN:
Sub OpenUSDRatesPage()
Dim objBK As Workbook
Dim objRng As Range
'Open the page as a workbook.
Set objBK = Workbooks.Open("http://www.x-rates.com/tables/USD.HTML")
'Find the Canadian Dollar cell.
Set objRng = objBK.Worksheets(1).Cells.Find("Canadian Dollar")
'Retrieve the exchange rate.
MsgBox "The CAD/USD exchange rate is " & objRng.Offset(-6, -1).Value
End Sub
The call is synchronous, so you can operate on the resulting data in the new workbook immediately after the Open call.
While the workbook is loading, Excel will display a progress bar. When you're done, you can call .Close to close the web data workbook. (e.g., for the MSDN example, you'd call objBK.Close when you're done.)
The caveats of using this approach:
You're on the hook to migrate the data from the web workbook to your own (ThisWorkbook) yourself, unlike a refreshable Excel Web Query that has a set destination.
If your web endpoint has a document name that matches the name of a document open in Excel, the user will get a warning that a document with the same name is open.

Microsoft Excel: Is it possible to auto set column width based on the contents size?

I have a pre-defined Excel template which i would be filling it with data using .Net C# code.
I know the size of some columns but some of them i don't know, so when i export data into the excel, the presentation doesn't look good.
This xls sheet is sent to customer. 1000's of documents generated every day like this. It's a tiresome work to every time open a newly generated excel document and change the width of the column to autofit by double clicking the column.
Is it possible in Microsoft Excel to AutoFit the size some column and not for few other columns?
range("a2:d2").Columns.AutoFit
You could also use something like
range("a2").CurrentRegion.Columns.AutoFit
or
range("a:d, f:g").Columns.AutoFit
If you want the VBA for it is:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Application.ScreenUpdating = False
For Each Value In Target.Columns
Worksheets(Sh.Name).Columns(Value.Column).AutoFit
Next Value
Application.ScreenUpdating = True
End Sub
Go to the Developer Tab (have to turn it on via File>Options>Customise Ribbon.
Open Visual Basic
Select Thisworkbook
Copy the Above VBA Code
Paste into the VBA Window
Close the VBA Window and the VBA editor

Cached variables not being stored in VSTO Excel workbook

I have a VSTO (Visual Studio Tools for Office) workbook with several cached variables. The relevant code for one of these is:
Public Class ThisWorkbook
<Cached()>
Public RetentionPC As Double
Private Sub ThisWorkbook_Startup(sender as Object, e as System.EventArgs) Handles Me.Startup
If Not IsCached(RetentionPC) then
RetentionPC = 0.5
End If
End Sub
End Class
This code runs and the value of the property is set, but it does not persist when the workbook is saved.
What am I missing?
I dont think you will be able to store cached values like that. If you wish to store information I would recommend using custom properties (either CustomProperties on a Worksheet or CustomDocumentProperties on a Workbook, depending on what you need).
Another way would be to store information to User or Application settings. This would store it across multiple Workbooks:
http://msdn.microsoft.com/EN-US/library/ms250653

Is it possible to deploy a VB application made in Excel as a stand alone app?

Basically wondering if there's a way for me to create a VB application in Excel and have it run without a full version of MS Office. The VB application would load an Excel sheet that would import a CSV onload, then print a PDF of the sheet and close.
If you have any questions, let me know.
No. Not without converting to a standalone application.
If you had were familiar with VB6 (and had access to it; it's no longer for sale), you could create a VB6 app. that references the excel COM components (still need to be installed on each target PC).
Otherwise, build an app. using VB.NET and use Office VSTO 2010 (need to reference the Office PIAs)
How to: Target Office Applications Through Primary Interop Assemblies
Just a little conflict. In office, you code with VBA, which is different than VB. What you would need to do is create a VB app that uses excel libraries or something to do some meaningful work.
The short answer is no.
You could write an external visual basic script that calls in to office and opens excel using some excel libraries, if memory serves me correctly however - you'd still require office installed on this machine. (Unfortunately I can't find a link at the moment to back this up)
Your best bet is to parse the CSV data yourself and generate a PDF from that.
There is some information here: http://www.rlmueller.net/ReadCSV.htm on how to Read CSV data using VBS (to get the examples to run, you should simply have to rename the .txt to .vbs and double click it.)
I'll leave you to find out how you'd then generate the PDF.
I don't think however this is the best solution to your problem - a full .NET application or perhaps some Python would likely serve you better.
Code will go in several different places, "ThisWorkbook" object and the "UserForm" code.
"ThisWorkbook" contains code that will determine if the UserForm is the only Excel thing (workbook) open, and if it is it will hide the Excel application and hide the workbook itself. and if other workbooks are open it simply hides the workbook. I have it set to hide the application and the workbook in both cases so that a new instance of Excel can be opened after the UserForm is running without pulling up the workbook associated with the UserForm. The code for this is below (goes into the "ThisWorkbook" object):
Private Sub WorkBook_Open()
Dim wb As Workbook
Set wb = Workbooks("MyBook.xlsm")
If Workbooks.Count > 1 Then
wb.Windows(1).Visible = False
Else
wb.Windows(1).Visible = False
Application.Visible = False
End If
UserForm1.Show vbModeless
'Put defaults and populations here
End Sub
The UserForm1.Show vbModelessallows for Excel to be used while the UserForm is active.
A couple of notes on this section:
"UserForm1" is the name of my UserForm, change this to the name of yours
Where I Set wb = Workbooks("") change inside the quotes to the name of the
workbook the UserForm is in
The IfElse statement could be eliminated and moved to the If, if you don't need any other action on the opening with no other workbooks open
The next section of code goes in the UserForm Code. I have a button set up to show the Excel workbook in order to edit it and whatnot, you could have a region you click if you don't want a button to show up. When you want to activate the Excel sheet and the application will need to be activated. I unload (deactivate) the active thing (the UserForm). This bit of code isn't necessary if the user doesn't need access to the spreadsheet:
Private Sub See_Excel_Click()
Dim wb As Workbook
Set wb = Workbooks("MyBook.xlsm")
wb.Windows(1).Visible = True
Application.Visible = True
wb.Sheets("Sheet1").Activate
Unload Me
End Sub
Within the userform there should be a way to handle what happens when the userform is closed, as the excel application and workbook will stay open hidden in the background. I have the action close the workbook and the application. A quick note, if you set the Cancel = True then the red x button won't close the userform. The code I use for this is:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then
Cancel = False
Dim wb As Workbook
Set wb = Workbooks("MyBook.xlsm")
wb.Windows(1).Visible = True
Application.Visible = True
ThisWorkbook.Saved = True
ThisWorkbook.Activate
If Workbooks.Count > 1 Then
ActiveWorkbook.Close
Else
Application.Quit
End If
End If
End Sub
That is it for the code that goes inside the UserForm. And the code that is necessary to have the UserForm in VBA act as it's own application while allowing for Excel to operate normally at the same time as the UserForm.
To summarize what happens:
When the Workbook is launched the workbook is hidden, and if no other workbook is open the the Excel application is hidden.
The UserForm is initiated to allow for Excel to be used at the same time
When the spreadsheet is activated again excel is re-enabled and the application and un-hide the worksheet
When the user form is closed, the workbook is closed, and if there are no other workbooks the excel application is closed
If you set defaults or populate ComboBoxes put them in the "WorkBook" object code.

Resources