Nisus macro language versus Visual Basic
Posted: 2025-05-04 08:01:10
I found this on the Web. I don't know if it is of any help.VB does not have a sort command! You cannot read in a bunch of data, sort it, and put it back out. Not possible.
Code: Select all
Sub SortTableByFirstColumn()
If Selection.Information(wdWithInTable) Then
Selection.Tables(1).Sort ExcludeHeader:=False, _
FieldNumber:=1, _
SortFieldType:=wdSortFieldAlphanumeric, _
SortOrder:=wdSortOrderAscending
Else
MsgBox "Please place the cursor inside a table to sort."
End If
End Sub
Code: Select all
Sub ImportAndSortCSV()
Dim fd As FileDialog
Dim filePath As String
Dim fileNum As Integer
Dim fileLine As String
Dim rowData() As String
Dim docTable As Table
Dim row As Integer, col As Integer
' Ask user to pick CSV file
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.Title = "Select a CSV File"
fd.Filters.Clear
fd.Filters.Add "CSV Files", "*.csv"
If fd.Show <> -1 Then
MsgBox "No file selected."
Exit Sub
End If
filePath = fd.SelectedItems(1)
' Read file and insert table
fileNum = FreeFile
Open filePath For Input As #fileNum
row = 0
Do Until EOF(fileNum)
Line Input #fileNum, fileLine
rowData = Split(fileLine, ",")
If row = 0 Then
' First row: create table
Set docTable = ActiveDocument.Tables.Add(Selection.Range, 1, UBound(rowData) + 1)
Else
docTable.Rows.Add
End If
For col = 0 To UBound(rowData)
docTable.Cell(row + 1, col + 1).Range.Text = Trim(rowData(col))
Next col
row = row + 1
Loop
Close #fileNum
' Sort by first column
docTable.Sort ExcludeHeader:=False, _
FieldNumber:=1, _
SortFieldType:=wdSortFieldAlphanumeric, _
SortOrder:=wdSortOrderAscending
End Sub
Code: Select all
Table:Sort Rows:Sort by Selected Column Ascending (A-Z)
It's been a while that I have opened LibreOffice, and I wasn't aware that they had a macro language. But maybe this is something added more recently. I could imagine that LibreOffice macros are reasonably doable to write (and actually useful
I used your words from the first post and mentioned in another forum that "VB does not have a sort command! You cannot read in a bunch of data, sort it, and put it back out." The guy who wrote the macros I posted was very quick to respond, obviously with good knowledge of VB, but he also added that he didn't know exactly what I wanted to achieve. So I think we should give him another chance.In the case I was referring to, I wanted to use VB to overcome a limitation in Word (or maybe it was Excel). For that it seems I would have had to write my own sort algorithm. Something, i might add, I have done more than once in Nisus Macro Language, and is quite doable, but in VB would probably be a nightmare.