How to deal with Excel popup when using Workbooks.OpenXML - vba

I'm trying to use vbs to open XML via Excel.
Below is my code:
Set myxl = createobject("excel.application")
myxl.DisplayAlerts = False
myxl.Application.Visible = true
myxl.Workbooks.OpenXML "SampleXML.XML",1,xlXmlLoadImportToList
But when code runs, Excel will pop up an option to ask how to open this file.
I'm sorry that I don't have enough reputation to post images. This popup will ask you whether open XML as "As an XML table" (which I want, but I don't want popup), "As a read-only workbook", "Use the XML Source task pane".
How do I aviod this pop up?

The problem is xlXmlLoadImportToList. It is an Excel Constant and not a VBScript Constant. It has a value 2. You can check that in the Immediate window of Excel as shown below.
Try this :)
Const xlXmlLoadImportToList = 2
Set myxl = createobject("excel.application")
myxl.DisplayAlerts = False
myxl.Application.Visible = true
myxl.Workbooks.OpenXML "C:\Test.XML",1,xlXmlLoadImportToList

Related

Word VBA Application.Quit issue

If (Documents.count = 1) And (ActiveDocument.Name = ThisDocument.Name) Then
Application.DisplayAlerts = False
Application.Quit (wdDoNotSaveChanges)
Else
Application.DisplayAlerts = False
ActiveDocument.Close (wdDoNotSaveChanges)
End If
I'm having issues with the above code, in that when the code clears that first If statement (meaning it is the only document open and the name of the document is as expected), it still won't close out of the Word Application completely. It instead will only close the document and then leave a "blank" Word window open.
I know it is clearing the first If statement because I wrote a quick check of each element to a debug file, and everything shows up as expected. Additionally, if I step through the code it indeed moves along as it should.
Interestingly/Frustratingly if I step through the code in debug mode and get to the section in the code for Application.Quit, it does indeed quit the entire program! So I'm really not sure why it doesn't work when I just run the code as opposed to stepping through it.
Have tried:
1 - Adding an 'Exit Sub' line after Application.Quit
2 - Setting the Word Application as an object explicitly:
Dim wObj As Object
Set wObj = CreateObject("word.Application")
'Application.Quit (wdDoNotSaveChanges) '
wObj.Quit (wdDoNotSaveChanges)
Set wObj = Nothing
3 - Adding a before close event:
Sub DocumentBeforeClose()
ActiveDocument.Saved = True
End Sub
Any help would be much appreciated!
When having this issue in Excel, I found I had to tell Excel to close all documents after calling Application.Quit.
Because Excel can retain a reference to any workbook after the workbook's window closes (workbook still present in VBA Editor, unreleased object references)…
Because I can't always know that my document is the first document that has been opened by this instance of Excel…
Because I must accept the presence of an Excel add-in that can cause workbooks to appear modified immediately after a save (due to changes to invisible worbook properties)…
Because my usecase requires Excel to quit…
Adapted for Microsoft Word:
'Actual handling of unsaved documents omitted from this answer
If savedAllDocuments Or UserDoesNotCare Then
Application.Quit wdDoNotSaveChanges
Dim doc As Document
For Each doc in Application.Documents
doc.Close SaveChanges:=False
Next doc
End If
I had a simular issue with Application.Quit (quitting Word from within an Excel macro). It turned out, that it was related to the status of Application.ScreenUpdating.
The macro hung when in Excel ScreenUpdating was set to false, but continued correctly with:
Application.ScreenUpdating = True

How do I get a VBA function to use the current query as an argument in Access?

I have a function that I use to open a query into a new and unnamed Excel spreadsheet from a form control, using the onclick event of the control to hardcode the query as the argument of the function, like this
`Call OpenExcelCurrent("Points_Totals")`
Users can then save the spreadsheet or not, as they wish.
What I would like to do is make this function dynamic and available via the Autokeys macro so that when I use the keystroke it runs the function against the query I am working on to produce a spreadsheet, then when I switch tabs to another query I can use the keystroke again to get another.
My question is, how do I get the function to look at the query that is currently open so that I can run my function on it? After digging around I am guessing I need to do something with "acQuery" somehow but it is beyond me at the moment! Any help or pointers would be appreciated.
The function that I am using for the onclick event is below. I freely admit that this was taken from another answer here and tweaked slightly as my coding ability is relatively low.
`Public Function OpenExcelCurrent(MyQueryName)
'Step 1: Declare your variables
Dim MyDatabase As DAO.Database
Dim MyQueryDef As DAO.QueryDef
Dim MyRecordset As DAO.Recordset
Dim i As Integer
'Step 2: Identify the database and query
Set MyDatabase = CurrentDb
Set MyQueryDef = MyDatabase.QueryDefs(MyQueryName) 'Query name in the database, needs to be in quotation marks under button_click event
'Step 3: Open the query
Set MyRecordset = MyQueryDef.OpenRecordset
'Step 4: Clear previous contents
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Visible = True
.Workbooks.Add
.Sheets("Sheet1").Select
'Step 5: Copy the recordset to Excel
.ActiveSheet.Range("A3").CopyFromRecordset MyRecordset
'Step 6: Add column heading names to the spreadsheet
For i = 1 To MyRecordset.Fields.Count
xlApp.ActiveSheet.Cells(2, i).value = MyRecordset.Fields(i - 1).Name
xlApp.ActiveSheet.Cells(2, i).Interior.Color = RGB(255, 178, 102)
xlApp.ActiveSheet.Cells(2, i).Font.Size = 11
xlApp.ActiveSheet.Cells(2, i).Font.Bold = True
Next i
xlApp.Cells.EntireColumn.AutoFit
xlApp.Cells.EntireColumn.Font.Size = 10
End With
'MsgBox "Your spreadsheet will need to be saved in a location of your choice", vbInformation, "Reminder"
End Function`
Cheers,
Graham
Graham,
I think all you need do is instead of passing in the query name as a parameter to your function, instead just reference Application.CurrentObjectName.
So...
Public Function OpenExcelCurrent()
...
Set MyQueryDef = MyDatabase.QueryDefs(Application.CurrentObjectName)
That should get the name of the query you have highlighted when you run your macro.
You may want to have some logic in your code to make sure the currently selected object is actually a query, to avoid any nasty errors if you accidentally run the macro whilst having another object selected.
Let me know if this works.
Ash

Get Printer Options in Excel VBA

I have a macro that I want to run before and after a user prints a worksheet in a Microsoft excel 2010 document. I am using the Workbook_BeforePrint method to do this. What I wanted to know is, how do I get the printer options that the user already selected when they printed the worksheet, so I can include those when I print the document using vba? For instance, my code below only prints 1 page no matter how many pages the user selected to print. I know I can use Thisworkbook.ActiveSheet.Printout copies:=2 or something like that, but I don't know how to get how many pages the user selected. Also, I'm not sure of anything else I might need to include for the print. I don't want anything from the options that the user selected to change. This would have been a lot easier if Microsoft had included a Workbook_AfterPrint method :( . Thanks in advance!
Here is what I have so far inside the workbook_beforeprint method
If (ThisWorkbook.ActiveSheet.name = "Printable Schedule" And user.colorPrintable = True) Then
Application.EnableEvents = False
Application.ScreenUpdating = False
Cancel = True
user.colorPrintable = False
Call updateEmployeeShifts
ThisWorkbook.ActiveSheet.PrintOut
user.colorPrintable = True
Call updateEmployeeShifts
Application.ScreenUpdating = True
Application.EnableEvents = True
End If
How about just using Application.Dialogs(xlDialogPrinterSetup).Show so that you can select the options you want and then print. Would that work?

Customizing a ribbon with VBA in Excel

I've already learned that creating a custom tab is in Excel not possible in this! post (unlike i.e. MSProject)
Specifically, can I change the paths of the macros to the current location ?
Edit
It seems that this page may lead to the answer, but I still don't know how, though
Some more in-detail description of the situation:
The user will download a file, which contains a list of materials, equipment, labor. Each column contains information about quantities, pricing etc
When the user click on this button I want to create (I have created manually), a macro called 'Main' in another workbook launches and copies the whole sheet (the contents) to itself and performs some things that procedures do, on it.
So the problem I'm facing, is twhen I'm sending a new version to the client, he has to put it in the exact location or it won't work. Since there's a mix of Mac and Windows computers involved, I'd rather see a situation where the button is assigned to the procedure when the user opens WorkBook B (the one that contains the code).
This way, a new version has to be openened only once, and then for continuous use, the user can just open the downloaded file, click the appropriate button and WorkBook B will open itself and execute.
Maybe there's other ways to go about this. I haven't checked if it's not easier to assign a button to the quick access toolbar...
This is some code I use to add a custom toolbar:
Set cbToolbar = Application.CommandBars.Add(csToolbarName, msoBarTop, False, True)
With cbToolbar
Set ctButton1 = .Controls.Add(Type:=msoControlButton, ID:=2950)
Set ctButton2 = .Controls.Add(Type:=msoControlButton, ID:=2950)
Set ctButton3 = .Controls.Add(Type:=msoControlButton, ID:=2950)
End With
With ctButton1
.Style = msoButtonIconAndCaption
.Caption = "Set &Picklists"
.FaceId = 176
.OnAction = "SetPicklist"
End With
With ctButton2
.Style = msoButtonIconAndCaption
.Caption = "Set &Defaults"
.FaceId = 279
.OnAction = "SetDefaults"
End With
With ctButton3
.Style = msoButtonIconAndCaption
.Caption = "&Visibility Settings"
.FaceId = 2174
.OnAction = "VisibilitySettings"
End With
With cbToolbar
.Visible = True
.Protection = msoBarNoChangeVisible
End With
The 'OnAction' controls the macro that runs... If you wanted to expand that to run a macro on a specific workbook, use "whatever.xls!MacroName"

excel macro calling another excel macro in vba

I have made a macro that calls another macro saved on my shared drive. I have taken the help from the suggestions given in the previous question I asked. It is working on the files that are on my local drive, but as soon as I open it from a file from the shared drive it stops working.
The macro name is mymacro.xla
These are the codes:
Dim i As AddIn
Set i = Application.AddIns.Add("M:\nit\USER\nitin kumar\NQK\macro\run1.2.xla", True)
i.Installed = True
I have created a button whose codes is given below:
Dim CoBa As CommandBar
Dim Ctlconst As CommandBarControl
On Error Resume Next
Set CoBa = Application.CommandBars.Add(Name:="Quote Daily Report", temporary:=True)
With CoBa
Set Ctlconst = .Controls.Add(Type:=msoControlButton)
With Ctlconst
.Caption = "Lexington Macro"
.Style = msoButtonIconAndCaptionBelow
.OnAction = "Accounts_Summary"
.FaceId = 483
.TooltipText = "NITIN"
End With
.Visible = True
.Position = msoBarBottom
End With
It's not working any suggestions would be of great help.
The least effort solution would be to copy the XLA locally before adding it.
The root cause could be one a number of things, locking, a read-only folder or security restrictions. It's better not to mess about with that and also that gets you around a bunch of potential problems such as:- what happens if you update the xla while someone is using it?