VBA Excel: Using/extracting data stored in CHM-file - vba

I am trying to extract data from a chm-helpfile using VBA. The CHM-file was downloaded and contains important data (such as the possible values for a dropdown-list) that I want to use as criteria for my analysis vba-tool. I would like to extract these tables with possible values and be able to follow the hyperlinks mentioned in the file.
After I manually unzip/extract the chm-file, I was able to use the htm-files, but it requires manual action, and I can't automate the unzip-action.
Is there any way to open the file to use this data?

Assuming you have hh.exe installed you can decompile as follows:
Arguments for hh.exe
-decompile folder chm
Example Code
Set your own path Information instead of D:\Project ...
Public Sub HTMLHelp_Decompile()
' Arguments for hh.exe: -decompile folder chm
On Error Resume Next
Shell """hh.exe""" & _
" -decompile D:\Project\Temp D:\Project\ddt.chm", vbNormalFocus
If Err.Number <> 0 Then MsgBox Err.Number & " " & Err.Description, vbExclamation, "Decompiling failed"
End Sub

Related

Simple command Appending Excel Data to MS Access. "Data type mismatch in criteria expression"

I keep getting this error:
Run-time error '3464': Data type mismatch in criteria expression
I know this is a common error to get, but in my case none of the questions in the forum seems applicable.
This is my code, and I have checked several times that the field names are the same in Excel and Access. Could it be that one of my fields in the Access table is a check box? I'm using True/False as values in Excel for that field.
Private Sub cmdImportMaintJobs_Click()
xlsMaintFile = OpenFileDialog(CurrentProject.Path & "\", "*.xls?", "MS Excel sheet", "*.xls; *.xlsx", "Select Excel file with Maintenance Jobs data")
If Len(xlsMaintFile) > 0 Then
CurrentDb.Execute "INSERT INTO tblMaintJob SELECT * FROM [Maint Jobs$A1:I50000] IN '" & xlsMaintFile & _
"' [Excel 5.0;HDR=YES;IMEX=1] WHERE MoldID <> 0 "
MsgBox "All done!", vbInformation, "Information"
End If
End Sub
I would highly recommend to import the data from Excel into temporary table first, all fields should be text. Then you'll be able to copy imported data to working table with explicit conversion and validation. Otherwise you'll run into similar problems with other data files

LOG file for a VBA userform event activity

I am trying to get an MI LOG for the usability of my userform i have created for my team.
I currently reached at a stage where i can only keep a track log of when the workbook has been opened and by who. But i want to go a bit further and also LOG what activities are performed on the user form such as what the user is searching and the results it pulls out.
See code below that i have currently in place: THE BELOW CODE IS PLACED IN MY MODULE:
Sub LogInformation(LogMessage As String)
Const LogFileName As String = "C:\TEXTFILE.LOG"
Dim FileNum As Integer
FileNum = FreeFile ' next file number
Open LogFileName For Append As #FileNum ' creates the file if it doesn't exist
Print #FileNum, LogMessage ' write information at the end of the text file
Close #FileNum ' close the file
End Sub
Public Sub DisplayLastLogInformation()
Const LogFileName As String = "C:\TEXTFILE.LOG"
Dim FileNum As Integer, tLine As String
FileNum = FreeFile ' next file number
Open LogFileName For Input Access Read Shared As #f ' open the file for reading
Do While Not EOF(FileNum)
Line Input #FileNum, tLine ' read a line from the text file
Loop ' until the last line is read
Close #FileNum ' close the file
MsgBox tLine, vbInformation, "Last log information:"
End Sub
Sub DeleteLogFile(FullFileName As String)
On Error Resume Next ' ignore possible errors
Kill FullFileName ' delete the file if it exists and it is possible
On Error GoTo 0 ' break on errors
End Sub
AND THIS CODE BELOW IS PLACE ON "ThisWorkBook"
Private Sub Workbook_Open()
LogInformation ThisWorkbook.Name & " opened by " & _
Application.username & " " & Format(Now, "yyyy-mm-dd hh:mm")
End Sub
THE RESULTS I GET FROM A TXT FILE BELOW:
> Number Checker.xlsm opened by #username : 2017-08-30 09:12
> Number Checker.xlsm opened by #username : 2017-09-02 09:19
> Number Checker.xlsm opened by #username : 2017-09-07 09:21
The userform itself is a simple search tool and pulls back results depending on the search, this is where I need help to track on what the user searched for and what results i.e txtbox1 feedback . Is this possible or I'm running on a dead end? :(
Any help would be much appreciated. Thank you
I just want to say thank you all for your advice & help on pointing me to the right direction, i think i have managed to solve it out now, all i needed to do was to put this code under the click event :
LogInformation ThisWorkbook.Name & " - " & " Closed by - " & _
Environ("username") & " - " & Application.username & " - " & Format(Now, "yyyy-mm-dd hh:mm") & " - " & " Number Searched For - " & _
Me.check_number.Value & " - " & Me.number_status.Value
[problem solved] next task now :p
I've created a simple utility that helps me with that. It is called VBA Telemetry.
It connects VBA and Microsoft Azure Cloud for REAL-TIME logging and tracking of Events & Errors from VBA with 1 line of VBA code.
So you can log and track your VBA projects (Excel workbooks, Access projects) wherever in the world they are. And you see what is going on in your Azure Portal Application Insights resource (this is a new product from Microsoft Azure).
For example, if you want to track an Event you can do this with this function:
TrackEvent "CommandButton1ClickEvent"
There is also one more function TrackMetric where we can send also some custom data. For example, we can send, how long did it take for a loop to complete on a user machine.
TrackMetric "Loop1Duration", 100
Or if you want to track Errors (or exceptions) here is a sample code line:
TrackError Err.Description, Err.Number, "CommandButton1_Click"
You need a free account on Microsoft Azure cloud and a free version of this utility.
Here is a video (45 seconds) where I'm showing how to log (track) Events:
Link to 45 sec video Track Events
You can see the details on how to do this in this article, there is also a youtube video (detailed webinar) in this article:
Link to the article
P.S.
As I've said at the beginning this helps me in my project for logging and error tracking in VBA Projects (Excel, Access), this is why I've made this also available for others. If you don't mind that from time to time a msgbox pops up you can use the free version, if not a one-time payment of few dollars will help me to further develop this little utility.
Hope this helps,
Davor

Access autocad object properties without opening it by VBA

I have been using folder browser for VBA, I could paste the code of it, but bottom line is that I get returned file name as a string.
Is there any way to access drawing properties (i.e number of layouts) without open?
Public Sub TestFileDialog()
dwgname = FileBrowseOpen("C:", "*", ".dwg", 1) 'dwgname is typeof string
End Sub
Its only the first step (use of FileBrowseOpen function is shown, but also i can use FolderBrowse and collect all .dwg inside of folder),actually i had in mind to batch export all layouts of selected .dwgs to currenty open one. Is there any chance for that?
To effectively read a .dwg file you'll need to open AutoCAD, otherwise the information is not accessible. Some properties may be, such as author, but not number of layouts...
But you can use AutoCAD Console (accoreconsole.exe) to run a headless AutoCAD and use APIs to read any information you need. This is really fast for reading lot's of files and the user will not see it running (but it needs to be installed anyway).
http://aucache.autodesk.com/au2012/sessionsFiles/3338/3323/handout_3338_CP3338-Handout.pdf
you could stay in VBA and use ObjectDBX
it leads to a very similar approach as accoreconsole.exe on in .NET does, i.e you won't see any drawing open in UI since it works on the database itself
It requires adding library reference (Tools->References) to "AutoCAD/ObjectDBX Common XX.Y Type Library", where "XX.Y" is "19.0" for AutoCAD 2014
a minimal functioning code is
Sub main()
Dim myAxDbDoc As AxDbDocument
Dim FullFileName As String
FullFileName = "C:\..\mydrawing.dwg" '<== put here the full name of the file to be opened
Set myAxDbDoc = AxDb_SetDrawing(FullFileName)
MsgBox myAxDbDoc.Layers.Count
End Sub
Function AxDb_SetDrawing(FullFileName As String) As AxDbDocument
Dim DBXDoc As AxDbDocument
Set DBXDoc = Application.GetInterfaceObject("ObjectDBX.AxDbDocument.19") '<== place correct AutoCAD version numeber ("19" works for AutoCAD 2014)
On Error Resume Next
DBXDoc.Open FullFileName
If Err <> 0 Then
MsgBox "Couldn't open" & vbCrLf & vbCrLf & FullFileName, vbOKOnly + vbCritical, "AxDB_SetDrawing"
Else
Set AxDb_SetDrawing = DBXDoc
End If
On Error GoTo 0
End Function
Still, you must have one AutoCAD session running from which make this sub run! But you should have it since talked about "currently open" drawing

A better VBA editor for Autocad

I'm developing some VBA macros in Autocad. The built-in editor is obsolete, but I wasn't able to find any better way to edit the .dvb files.
A .dvb file does contains many other source files packed in, and so far I think that Autocad is the only software that can unpack them...
The only way it seems to be able to do this, is to export every file from the .dvb manually; but since I have about 30 files there, it doesn't seem like this is a good way to do things.
Any ideas on how to do this better?
You can export all your files with this code :
Public Sub Export()
Dim vbe As vbe
Set vbe = ThisDrawing.Application.vbe
Dim comp As VBComponent
Dim outDir As String
outDir = "C:\\Temp\\VbaOutput"
If Dir(outDir, vbDirectory) = "" Then
MkDir outDir
End If
For Each comp In vbe.ActiveVBProject.VBComponents
Select Case comp.Type
Case vbext_ct_StdModule
comp.Export outDir & "\" & comp.Name & ".bas"
Case vbext_ct_Document, vbext_ct_ClassModule
comp.Export outDir & "\" & comp.Name & ".cls"
Case vbext_ct_MSForm
comp.Export outDir & "\" & comp.Name & ".frm"
Case Else
comp.Export outDir & "\" & comp.Name
End Select
Next comp
MsgBox "VBA files were exported to : " & outDir
End Sub
If you don't feel like modifying your code with the above Export() subroutine, you can export your VBA code from the .dvb file using the VBA2VB Form Converter macro written by Leslie Lowe, or the modified version written by Augusto Gonçalves of the Autodesk Developer Network. This macro has the added bonus of being able to convert simple VBA forms to VB6 forms. You'll need to do this if you want to be able to port the project to .NET in the future, as AutoCAD is dropping VBA support. The modified version of the macro is especially nice, as it will create the .vbproj ASCII file that you'll need to do the migration, which you'd otherwise need a copy of the old Visual Basic 6 IDE to make.
FWIW, A .dvb file can be opened using an archive utility like 7-Zip, if you want to see what's in it -- but it appears to be compiled, and isn't helpful if you want human-readable or exportable code.

With VBA, how can I check if, in Windows Explorer, the file extensions for known file types are hidden?

Without opening Windows Explorer. I want to check for this from Word VBA.
Thanks.
EDIT:
This code works now:
Set WshShell = CreateObject("WScript.Shell")
If WshShell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\HideFileExt") = 0 Then
MsgBox Prompt:="In Windows Explorer, set the folder options to hide file extensions of known file types." _
& vbCrLf & vbCrLf & " This prevents the file extension from appearing as part of the document number in" _
& "the document body and page headers.", Buttons:=vbOKOnly + vbCritical, Title:="Critical"
End If
You need to query the registry value
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\HideFileExt
Extensions are shown if this value is 0 and hidden if it is 1.
You can use the RegRead method on the WshShell object to read this value. You can add an reference to the Windows Script Host Object Model type library in your VBA project to get strong typing when you use the WshShell object.