Does anybody know how to call the import data built-in dialog excel from a macro (vba)?
I've tried Application.Dialogs.Item(...).Show but I canĀ“t find the right dialog.
Please help.
Thanks in advance.
The closest I can find using the dialog system is:
Application.Dialogs(xlDialogImportTextFile).Show
You can get a reference to the command bar button (at least for me in both 2k3 and 2k7) via:
Set button = Application.CommandBars.FindControl(ID:=6262)
But calling the Execute method on the button fails. Sadly, the short answer seems to be that it's not possible.
You can add QueryTable objects by hand. While not an optimum path, you could design your own simple interface for selecting the source data.
If you choose the Object Browser and search for say, xlDialogImportTextFile, you will get a list of possible dialogs.
EDIT:
Perhaps something on these lines would suit:
'Allow user to select text file
sf = Application _
.GetOpenFilename("Text Files (*.txt), *.txt")
If sf <> False Then
'Open text file
Workbooks.OpenText sf
End If
I don't think there is a VBA equivalent, because in one case you are returning data to a worksheet, while in the other case, the data is put into a recordset in memory.
This kludge should pop up the dialog for you, however:
SendKeys "%ddd"
Related
I have a website that generates some Excel spreadsheets and provides a download link to the end-user. One of these spreadsheets is a fairly convoluted one with multiple charts based on multiple pivot tables.
For a few of our users, when they choose to "Open" the file immediately (as opposed to "Save As" to put it in a known path), we are running into problems when trying to refresh the pivot tables:
Cannot open PivotTable source file: 'C:\Users\joeuser\AppData\Local\Microsoft\Windows\Temporary Internet Files\Low\Content.ie5\W9IZXYBM[excel_file_name.xlsm]PivotTableSourceSheetName'
If the user chooses "Save" instead of "Open", the refresh works fine, so our users have a workaround, but can anyone shed light on what might cause only a few users to have this problem with the "Open" scenario? We haven't spotted anything glaringly different between machines that "Open" the file just fine and those that don't. Same browser versions, same Office versions, etc.
Code:
Sub LinkBreaker()
Dim Links() As Variant
Dim i As Long
Links = ActiveWorkbook.LinkSources(1)
If Not IsEmpty(Links) Then
For i = 1 To UBound(Links)
ActiveWorkbook.BreakLink Name:=Links(i), Type:=xlLinkTypeExcelLinks
Next i
End If
End Sub
Theory:
This code will convert all external links in the worksheet into their values. I think that this will eliminate your problem with the external file. You should put this code in the Workbook_Open event.
Cannot open PivotTable source file: 'C:\Users\joeuser\AppData\Local\Microsoft\Windows\Temporary Internet Files\Low\Content.ie5\W9IZXYBM[excel_file_name.xlsm]PivotTableSourceSheetName'
The file referenced in error is an xlsm (macro enabled xlsx). This may cause issue. "Open" opens file in IE cache folder treated as internet content, it should have sth to do with the restrictions MSO imposed on internet oriented content. "Save"ed file are local file, which don't suffer from those restrictions. Considering the difference. Maybe change to xlsx ? Besides that I don't think there is anything you can do to circumvent it.
I have the following code that works in all other circumstances except in a single where it returns the error Can't move focus because it is invisible, not enable, or type that does not accept focus. The data in sheet consists only basic numbers and words. My objective is to select a range from one work book and paste it to another. It appear that excel does not recognise anything to be in the cells, although there in fact is. Does anyone know why this may be happening?
Set Users = Application.Workbooks.Open(PathA)
With Prices
.Sheets("Sheet").Range("A:AJ").Select
Selection.Copy
End With
'Set Risk = Application.Workbooks.Open(PathX)
With Risk
.Sheets("Sheet").Range("A1:AJ1048576").PasteSpecial Paste:=xlPasteAll
.Save
' .Close
End With
Users.Close
it looks like someone else had the same issue and was able to resolve it on an MSDN forum
http://social.msdn.microsoft.com/Forums/office/en-US/3263b079-7e4f-452c-8dcc-92c682b8370b/excel-form-cant-move-focus-to-the-control-because-it-is-invisible-not-enabled-or-of-a-type-that?forum=exceldev
maybe this fix will apply to your situation too.
... OK so on that page, one guy has a kludge that seemed to work for him:
Error was:
Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus.
How to Fix
1. Select the object with the problem, e.g. the Form or Control.
2. In the properties windows, select the name of the object and rename adding an "x" to the end, e.g. Rename CPanel to CPanelx.
3. Save the Form.
4.Now rename the CPanelx back to CPanel.
5. Run your form.
6. Problem solved.
Something screwy with Excel VBA, not sure what !, but this will get you working again.
Steve D.
and then someone else described the underlying problem, but his solution involved using design mode, which seems like it is maybe defeating the purpose of automating in vba, but the moderator seemed to like his answer better, so here that is as well
In normal use activeX controls are intended only to be activated, not selected, there's a difference. If you partifcularly want to Select the control (manually or with code), try putting Excel into Design mode first.
See also the topic "Why can't I select form and ActiveX controls?" in Excel's help (not VBA's help)
Peter Thornton
"Run-time error '-2147352565 (8002000b)': Can't move focus to the control because it is invisible, not enabled or of a type that does not accept the focus" Can be overcome but sequentially defining the area you wish to select. Obviously some may do this in any case, but it this specific instance it is needed to overcome the issue. This is an example for copying from one workbook to another and pasting where U is the sheet excel finds to be "invisible".
Dim U As Workbook
Dim Us As Worksheet
Set U = Application.Workbooks.Open(Path)
Set Us = U.Worksheets("sheet")
With Us
.Range("A:AJ").Select
Selection.Copy
End With
U.Close SaveChanges = True
With DestinationWorkbook
.Sheets("sheet").Range("A:AJ").PasteSpecial Paste:=xlPasteAll
.Save
End With
Just to help in case you didn't find the answer till now :
In my case this message came out when I redimensioned the application window while it was maximized :
e.g. Application.width = 100 (on Excel 2003 or 2007)
The solution in this case, is to first bring the application window to NORMAL.
e.g. ActiveWindow.WindowState = xlNormal
I created a form in Excel 2003 using a tutorial online but I had a different use case and now have run into a minor problem. I know very little VB and my searches have not produced anything helpful. I have a text input field and it won't wrap the text which would be preferred so the user can see what he/she wrote to prof read or edit before submitting. My guess is this code block requires something to specify that the text should wrap in that text box. Thank you in advance for any help you can provide.
Private Sub txtopportunity_Change()
End Sub
You don't have to do this in code. You can set the textbox to always wrap like this:
In Designer mode right click your text box and open it's 'Properties'
One of the peoperties is called 'WordWrap' Set it to True. AND set the 'MultiLine' property to True
If you really need to enable/disable this via VBA it would look something like
Me.txtopportunity.WordWrap = True
Me.txtopportunity.MultiLine = True
First: I'm using Excel 2007, but the code has to work for Excel 2003 as well.
My problem is the following: I need to access cells in a different workbook, which may be closed. The following code can be found all around the web:
Function Foo()
Dim cell As Range
Dim wbk As Workbook
Set wbk = Workbooks.Open("correct absolute path")
' wbk is Nothing here so the next statement fails.
Set cell = wbk.Worksheets("Sheet1").Range("A1")
Foo = cell.Value
wbk.Close
End Function
sadly, wbk is Nothing after the open statement (I'd love to give a better error message, but no idea how I'd do that; what I'd give for a real IDE and an useful language :/). The absolute path is correct and points to a valid excel xlsx file.
Also I assume the best way to do this, is to "cache" the workbook and not open/close it every time the function is called? Any possible problems with that (apart from having to handle the situation when the workbook is already open obviously)?
Image while stepping through:
I can reproduce this problem. It only happens to me when I attempt to paste this code into a user-defined function.
I believe this is by design (the quote is for XL 2003, but the same thing happens to me on XL 2010)
Using VBA keywords in custom functions
The number of VBA keywords you can use in custom functions is smaller than the number you can use in macros. Custom functions are not allowed to do anything other than return a value to a formula in a worksheet or to an expression used in another VBA macro or function. For example, custom functions cannot resize windows, edit a formula in a cell, or change the font, color, or pattern options for the text in a cell. If you include "action" code of this kind in a function procedure, the function returns the #VALUE! error.
http://office.microsoft.com/en-us/excel-help/creating-custom-functions-HA001111701.aspx
The only workaround I've found is to call this kind of code via a normal macro. Something like selecting the cells to apply it to, then looping over Selection or the like.
You can use this (similar to what Bruno Leite proposed, but much simpler to write):
Dim excelApp As New Excel.Application
excelApp.Visible = False
Set WB = excelApp.Workbooks.Open(FileName, xlUpdateLinksNever, True)
As UDFs are called repeatedly, you should make sure to do an excelApp.Quit before exiting the function (and a WB.close(False) before) to avoid having countless Excel instances running on your box.
I spent some thoughts on it and came to the conclusion that you cannot mess around with the workbooks of the current instance of excel while executing a UDF. On the other hand, opening a second instance of excel will do the job without interference.
To get data from Workbook without is open, you can use this, with ADO connection.
To use in Excel 2007 change this
Microsoft.Jet.OLEDB.4.0
to
Provider=Microsoft.ACE.OLEDB.12.0
and
Extended Properties=\"Excel 8.0;HDR=Yes;\
to
Extended Properties=\"Excel 12.0;HDR=Yes;\
[]'s
The workaround of putting my routine into a separate macro in the workbook module, and calling that macro from the Workbook_BeforeSave code, seems to have done the trick.
I've had a similar issue, but in my case it's a "Workbooks.Open(filename)" command at the start of a small routine embedded in Workbook_BeforeSave. VBA just skips right over the line of code as if it weren't there, it doesn't even report an Err.Code or Err.Description.
The only clue for me was that it's part of the Workbook_BeforeSave routine, and the limits with Functions above seem to indicate that could be a possible cause. So I dug around further to find more details.
It seems that Workbook_BeforeSave disables Excel from opening more files, and I guess there's a good reason for doing that, since the File > Open option is still visible in the File menu, but it can't be clicked. Strangely, the Open toolbar icon/button still works, and so whilst I can manually open the file from there, I wonder if it's because it's impossible to call this action from VBA code and that's why they allowed it?
You don't have to "Set" a cell, It's part of the workbook class (as far as I know). Just use the following...
foo = wbk.Worksheets("Sheet1").Range("A1").Value
I would suggest that you open you the new workbook upon opening the calling workbook, in the worbook_open event.
You then store the new workbook reference in a global variable.
Then the function called by your cell uses the said global variable instead of trying to open a new workbook. This way you go around the limitations.
PS : Of course global variable are to be avoided, some sort of container would be better than a direct global variable.
You can check the error in a proper way by using the following code:
filelocation = c:\whatever\file.xlsx
On Error GoTo Handler 'this is key as if the next row returns an error while opening the file it will jump to the Handler down there.
Set wkb2 = Workbooks.Open(filelocation, ReadOnly)
Handler:
MsgBox "File " & filelocation & " does not exist or cannot be reached, please review and try again"
I know that this does not answer the question (that's why I also landed in this thread, as I cannot open the file and can't understand why is that so)
Cheers,
RV
I need a way to programatically launch the macro-recorder in Excel, and supply the name for the new macro that will get created.
This can be from VSTO or VBA, or using the Office interop assemblies.
Any ideas how this can be accomplished?
In VBA:
Dim ctrlStart As CommandBarControl, ctrlStop As CommandBarControl
Set ctrlStart = Application.CommandBars.FindControl(ID:=184)
Set ctrlStop = Application.CommandBars.FindControl(ID:=2186)
ctrlStart.Execute
'name part would go here, but first you have to deal with a modal dialog
ctrlStop.Execute
It looks like the Execute method on the RecordMacro control opens a modal dialog. There is no way to feed a parameter to this, or to do anything like SendKeys. The only way I see to do it is to write a sub that will rename the macro after the fact. It will be a little complicated to determine what the name of the new macro is, and you will still have a dialog box to deal with.