Powerquery opening files with bad extensions - html

One of my datasources comes as Excel "xls" files. I put that in quotes because, under the hood, the authoring system has generated an html file and named it myFile.xls.
When manually opening this, Windows gives me a warning, but Excel will open it. From Excel VBA, I can inhibit-warning, and it opens silently and correctly. But trying to open a folder full of such files, PowerQuery just hangs. I've been manually opening each "xls" file and saving as .xlsx. Any suggestions for getting rid of this manual step?
[Running Excel 2013 under Windows 7.]

Add a reference to Microsoft Scripting Runtime in order to use the Scripting.FileSystemObject object.
Dim app As Excel.Application
'If macro is running within an Excel instance, and you want to use that instance uncomment the next line
'Alternatively, use Application directly
'Set app = Application
'If you want to create a new instance, uncomment the next line
'Set app = New Excel.Application
Dim fso As New Scripting.FileSystemObject
Dim sourceFolder As String
sourceFolder = "C:\path\to\folder\of\pseudo-excel-files"
Dim destFolder As String
destFolder = fso.BuildPath(sourceFolder, "_converted")
If Not fso.FolderExists(destFolder) Then fso.CreateFolder(destFolder)
Dim fle As Scripting.File
For Each fle In fso.GetFolder(sourceFolder).Files
Dim destPath As String
destPath = fso.BuildPath(destFolder, fle.Name & ".xlsx")
Dim book As Workbook
Set book = app.Workbooks.Open fle.Path
book.SaveAs destPath, xlWorkbookDefault
book.Close
Next

Related

MSAccess - TransferDatabase - File locked open?

I am attempting the following in an AccessDb via VBA:
Export a single table from current DB into a new DB in the same directory, via a Query/TransferDatabase. This seems to work as expected.
Dim ws As Workspace
Dim db_new as Database
strPath = CurrentProject.Path & "\Backend_Database\"
strDBFilename = strPath & Raw_Count_File.accdb"
Set ws = DBEngine.Workspaces(0)
Set db_new = ws.CreateDatabase(strDBFilename, dbLangGeneral)
DoCmd.TransferDatabase acExport, "Microsoft Access", _
strDBFilename, acTable, "tmp_RawCountFile", "Raw_TblMatchedTB"
Within the same function used above (to create the new file), I am attempting next to ZIP the new file into the same directory. The result is 1K Byte ZIP file (it's an empty ZIP ).
If I breakout the code segment that creates the ZIP file into separate function (i.e., under another button), the function works as expected and the proper ZIP file is created.
My Question:
I am guessing the new DB file and subsequent TransferDatabase is leaving the new_db file hanging open and inaccessible to the ZIP function. I attempted to set the various objects = nothing prior to the ZIP function, but same result. Only if I exit the first function and call a second function will it work as desired.
Can I add something to the end of the TransferDatabase function to ensure the resulting file will be available for the ZIP task?
My preference is not to add a secondary button press to this task...
Any suggestions to get me going?
Thanks!
Try to Set db_new = Nothing before zipping, in order to close the newly created db.

Data extracted from Acces to Excel is automatically getting over to extra sheet than the defined sheet

I am trying to get some data extracted from MS Access to Excel sheet which is a kind of already defined template. For example , i use an excel file with say one sheet named Result (with some pre-defined data) as a source file and then i copy the same to an output file. Then some OutputTable from MS Access is extracted in output file using DoCmd.TransferSpreadsheet with explicitly mentioned to get extracted in Result sheet . The data is coming fine but the sheet Result is not used though an extra sheet named Result1 gets created with same data as OutputTable of Ms Access.
Code i am using is mentioned below:
SourceFile = CurrentProject.Path & "\Template\" & "Input_Template.xlsx"
DestinFile = CurrentProject.Path & "\Output\" & "Output_" & sDateTimeStamp & ".xlsx"
FileCopy SourceFile, DestinFile
DoCmd.TransferSpreadsheet acExport, , "OutputTable", DestinFile, False, "Result"
I have these folders Template (having Input_Template.xlsx file) and Output folder created on my system under same path where the Database is placed.
Could anyone tell if i'm doing it in wrong way or is there any configuration required or i might be missing something. Any help would be highly appreciated.
Thank You!!!
Honey
TransferSpreadsheet is not suitable for exporting to pre-formatted template. Use CopyFromRecordset method from Excel library instead. Example below will dump content of OutputTable to "Result" spreadsheet of destination template file starting "B2" cell.
Dim xlApp As Object
Dim xlWork As Object
Dim xlSheet As Object
Dim rsExportResults As Recordset
Set rsExportResults = CurrentDb.OpenRecordset("OutputTable")
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWork = xlApp.Workbooks.Open(DestinFile)
Set xlSheet = xlWork.Sheets("Result")
xlSheet.Range("B2").CopyFromRecordset rsExportResults

VBS file only works when double-clicked

I am trying to use the Windows 8 Location API in order to get GPS coordinates from a built-in sensor, and return these coordinates to MS Access. However, due to the unsupported variant types used in the API, Access is unable to reliably use its objects. I am trying to come up with a workaround using a VBScript file, and somehow returning the values to MS Access (using 2010 and 2013). The easiest way I could think of was to spit out a txt file to be read by Access and then deleted.
My VBS File works perfectly when I run it from Windows Explorer (double-clicking the file) but I can't find a way to make it work properly when running it from VBA code. Here is the VBS File:
Dim latlongfactory
Dim rptLong, rptLat
Dim report
Dim keepSleeping
Dim fs, f
Dim ts
Set latlongfactory = Wscript.CreateObject("LocationDisp.LatLongReportFactory", "llf_")
Set fs = Wscript.CreateObject("Scripting.FileSystemObject")
f = fs.BuildPath(CurrentDirectory, "gpsTempFile.txt")
keepSleeping = True
latlongfactory.ListenForReports(1000)
Sub llf_NewLatLongReport(report)
rptLong = report.Longitude
rptLat = report.Latitude
keepSleeping = False
End Sub
Do While keepSleeping
Wscript.Sleep(20)
Loop
Set ts = fs.CreateTextFile(f, True)
ts.WriteLine rptLat & "," & rptLong
ts.Close
Set fs = Nothing
Set latlongfactory = Nothing
set report = Nothing
When run through VBA, it doesn't create the text file anymore, and I'm not entirely sure why. I tried adding a Msgbox at the end of the script just to see if the code was running. The Msgbox does come up.
The line I use to execute in Access VBA is:
Shell "Wscript """ & CurrentProject.Path & "\gpscoordinates.vbs""", 1
It runs the VBS file, but the text file isn't getting created and I cannot figure out why. Any help would be much appreciated!
If you want the output file to be created in the same folder in which the script resides, you can use the ScriptFullName property of the WScript object:
outputFolder = fs.GetParentFolderName(WScript.ScriptFullName)
f = fs.BuildPath(outputFolder, "gpsTempFile.txt")

Display alerts are not enabling in excel file from access vba

I'm writing contents of tables from access to excel(Opening the excel file from access) After writing it i'm trying to save all workbooks in the appXL application. For that i'm making DispalyAlerts false before saving and trun on back after saving. After writing contents to excel i'm closing the access. After writing ,when i'm trying to close the excel, it is not giving any alerts like Do you want to save the contents?
My vba code
Sub Writexl()
Dim appXL As Excel.Application
Dim wb As Excel.Workbook
StrwbPath="C:\temp\sample.xls"
Set appXL = CreateObject("Excel.Application")
With appXL
Set wb = .Workbooks.Open(StrwbPath)
.Visible = True
End With
'here code for writing contents
'save workbook after writing
appXL.Application.DisplayAlerts = False
For Each w In appXL.Application.Workbooks
w.Save
Next w
appXL.Application.DisplayAlerts = True
DoCmd.Quit acQuitSaveAll
Application.Quit
End sub
You are setting the DisplayAlerts to the Application's Applicaiton
appXL.Application.DisplayAlerts = True
is conceptually equivalent to
Excel.Application.Application.DisplayAlerts = True
So the property is being set on the parent application to Excel.
Try
appXL.DisplayAlerts = True
After writing ,when i'm trying to close the excel, it is not giving
any alerts like Do you want to save the contents?
As #David Zemens says, why would it display a message to save all changes when you've already saved everything. Have you tried changing a cell and then exiting Excel?

MS Access Auto Link Excel Spreadsheets

I have a directory structure where I am managing the requirements of a system with each component of that system having its own directory. the requirements of each component are stored in a excel workbook that has multiple worksheets(# of worksheets are static). I am currently using access as a central location to view the information in these sheets and perform queries on them. I hate having to manually link new excel files every time a new component documentation is added to the directory. Is there a way that when everytime I start access it will search the directory tree of stored excel files and automatically link them to access if they're not linked and update my save queries to include the new files. I was thinking that I could save the sub directory names in a table and all the filenames in those sub directory in another table that references the other table, so as it searches the filesystem it compares names to the table. Is this possible if so could someone point me in the right direction.
You can use Dir or the FileSystemObject recursively to get files from a directory tree. Access stores the link information of files in the connect property of the TableDef or you can get it from:
SELECT msysobjects.Database
FROM msysobjects
WHERE (((msysobjects.Database) Is Not Null));
You can get worksheets like so:
''Requires reference to the Microsoft Excel x.x Object Library
Dim strFileName As String
Dim objXL As New Excel.Application
Dim wkb As Excel.Workbook
Dim wks As Object
''objXL.Visible = True
strFileName = "C:\Docs\LTD.xls"
Set wkb = objXL.Workbooks.Open(strFileName)
For Each wks In wkb.Worksheets
DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel9, _
wks.Name, strFileName, True, wks.Name & "$"
Next
''Tidy up
wkb.Close
Set wkb = Nothing
objXL.Quit
Set objXL = Nothing
Or using an ADOX.Catalogue: http://forum.lessthandot.com/viewtopic.php?f=95&t=3712