REvit Shared Params export with more controls v2.0


Attribute VB_Name = "REVIT_EXPORT_Library" Option Explicit Const VBQT = """" Sub REVIT_Shared_Params_EXPORT() Dim ws As Worksheet Dim objTxtFile As TextStream ''As Integer Dim FilePath As String Dim strN As String Dim nRow As Integer Dim ncol As Integer Dim RowMax As Integer Dim ColMax As Integer Dim RColmax As Integer Dim strLine As String Dim strFmt As String Dim obj Dim CheckGroup As Range Dim GroupID_START As Integer Dim GroupID_END As Integer Dim FSO As FileSystemObject If MsgBox("This exports the ''PARAMS' worksheet, or ACTIVE worksheet in shared parameters format, in the same folder as the EXCEL file." & vbCr & vbCr & "Continue?", vbInformation + vbYesNo) <> vbYes Then MsgBox "Exititng", vbExclamation Exit Sub End If Set FSO = CreateObject("Scripting.FileSystemObject") On Error Resume Next Set ws = ActiveWorkbook.Worksheets("PARAMS") If ws Is Nothing Then Set ws = ActiveWorkbook.ActiveSheet If (MsgBox("Error opening " & VBQT & "PARAMS" & VBQT & " worksheet." & vbCr & vbCr & "OK to use " & ws.Name & " for export?", vbYesNo + vbCritical, "Cannot find 'PARAMS' worksheet") _ <> vbYes) Then Exit Sub End If End If RowMax = Cells.Find("*", [a1], xlFormulas, , xlByRows, xlPrevious).Row ColMax = 9 ''Currently max col until *META is largest from column groups - COLMAX set to 9 by default to start strN = Application.ActiveWorkbook.Name strN = Left(strN, InStr(Len(strN) - 5, strN, ".", vbTextCompare) - 1) ws.Activate Application.DisplayAlerts = False FilePath = ws.Parent.Path & "\" & strN & ".txt" FSO.DeleteFile (FilePath) If FSO.FileExists(FilePath) Then MsgBox "ERROR: File cannot be deleted." & vbCr & vbCr & "Check if it is read only, " & vbCr & vbCr & "-THEN-" & vbCr & vbCr & "Try closing revit and waiting for file to become available to read/write.", vbCritical Exit Sub End If On Error GoTo 0 ''fso text file- the textwrite has issues with uinicode and Kanji characters showing upp in lieu of ASCII '''https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/openobjTxtFile-method Set objTxtFile = FSO.CreateTextFile(FilePath, True, False) For nRow = 1 To RowMax Select Case UCase(ws.Cells(nRow, 1)) Case "*META" ColMax = 3 Case "*GROUP" ColMax = 3 GroupID_START = nRow + 1 Case "*PARAM" ColMax = 9 GroupID_END = Cells.Find("*", ws.Cells(nRow, 2), xlFormulas, , xlByColumns, xlPrevious).Row ''Use the final range to set the check group Set CheckGroup = ws.Range(ws.Cells(GroupID_START, 2), ws.Cells(GroupID_END, 2)) End Select strLine = "" ''For each of the used columns repeat the export For ncol = 1 To ColMax If ncol > ColMax Then Exit For ''for pseudo to truncate junk outside the max col and allow for notes. strFmt = ws.Cells(nRow, ncol).NumberFormat If strFmt Like "General" Then strFmt = "" strLine = strLine & Format(ws.Cells(nRow, ncol).Value, strFmt) & vbTab Next ncol ''this strips off empty tabs from the end of each line. ''This is the cheap and fast way to export a relatively correctly formattted file Do While Right(strLine, 1) = vbTab And strLine <> "" strLine = Left(strLine, (Len(strLine) - 1)) Loop '''Print #objTxtFile, strLine & vbCrLf objTxtFile.Write (strLine & vbCrLf) Next nRow ''Close objTxtFile objTxtFile.Close Set FSO = Nothing If VBA.Dir(ws.Parent.Path & "\" & strN & ".txt") <> "" Then MsgBox ":File Exists- Please verify Success writing: " & vbCr & vbCr & FilePath, vbInformation End Sub '======================================================================== Sub REVIT_EXPORT_CATALOG_1st_row_params() Dim ws As Worksheet Dim objTxtFile As TextStream ''As Integer Dim FilePath As String Dim strN As String Dim nRow As Integer Dim ncol As Integer Dim RowMax As Integer Dim ColMax As Integer Dim strLine As String Dim strFmt As String If MsgBox("This exports the entire catalog from the 'EXPORT' tab using the first row as parameters. Continue?", vbInformation + vbYesNo) <> vbYes Then MsgBox "Exititng", vbExclamation Exit Sub End If Dim FSO As FileSystemObject Set FSO = CreateObject("Scripting.FileSystemObject") Set ws = ActiveWorkbook.Worksheets("EXPORT") ' RowMax = Cells.Find("*", [a1], xlFormulas, , xlByRows, xlPrevious).Row ColMax = Cells.Find("*", [a1], xlFormulas, , xlByColumns, xlPrevious).Column strN = ThisWorkbook.Name strN = Left(strN, InStr(Len(strN) - 5, strN, ".", vbTextCompare) - 1) ws.Activate Application.DisplayAlerts = False FilePath = UCase(ws.Parent.Path & "\" & strN) & ".csv" If VBA.Dir(ws.Parent.Path & "\" & strN & ".csv") <> "" Then FSO.DeleteFile (ws.Parent.Path & "\" & strN & ".csv") ''fso text file- the textwrite has issues with uinicode and Kanji characters showing upp in lieu of ASCII '''https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/openobjTxtFile-method Set objTxtFile = FSO.CreateTextFile(FilePath, True, False) RowMax = ws.Cells(Rows.Count, 1).End(xlUp).Row ColMax = ws.Cells(1, Columns.Count).End(xlToLeft).Column For nRow = 1 To RowMax strLine = "" For ncol = 1 To ColMax If nRow = 1 And ncol = 1 Then ''null for upper left cell always! strLine = strLine & "," Else strFmt = ws.Cells(nRow, ncol).NumberFormat If strFmt Like "General" Then strFmt = "" strLine = strLine & Format(ws.Cells(nRow, ncol).Value, strFmt) & "," End If Next ncol strLine = Left(strLine, Len(strLine) - 1) '''Print #objTxtFile, strLine & vbCrLf objTxtFile.Write (strLine & vbLf) Next nRow ''Close objTxtFile objTxtFile.Close Set FSO = Nothing If VBA.Dir(ws.Parent.Path & "\" & strN & ".csv") <> "" Then MsgBox ":File Exists- Please verify Success writing: " & vbCr & vbCr & FilePath, vbInformation VBA.Shell "C:\WINDOWS\explorer.exe """ & ws.Parent.Path & "", vbNormalFocus Else MsgBox "Error writing", vbCritical End If End Sub Sub REVIT_EXPORT_CATALOG_1strow_mapped_params() Dim ws As Worksheet Dim objTxtFile As TextStream ''As Integer Dim FilePath As String Dim strN As String Dim nRow As Integer Dim ncol As Integer Dim RowMax As Integer Dim ColMax As Integer Dim strLine As String Dim strFmt As String Dim cols As New Collection Dim obj Dim FSO As FileSystemObject If MsgBox("This exports catalogs using the first row of the 'EXPORT' tab." & vbCr & vbCr & _ "Parameters will skip the second row and use the corresponding values from column1 (family name) & marked columns." & vbCr & vbCr & _ "Continue?", vbInformation + vbYesNo) <> vbYes Then MsgBox "Exititng", vbExclamation Exit Sub End If Set FSO = CreateObject("Scripting.FileSystemObject") On Error Resume Next Set ws = ActiveWorkbook.Worksheets("EXPORT") If ws Is Nothing Then MsgBox "Error opening " & VBQT & "EXPORT" & VBQT & " worksheet." & vbCr & "Exiting.", vbCritical Exit Sub End If RowMax = Cells.Find("*", [a1], xlFormulas, , xlByRows, xlPrevious).Row ColMax = Cells.Find("*", [a1], xlFormulas, , xlByColumns, xlPrevious).Column strN = Application.ActiveWorkbook.Name strN = Left(strN, InStr(Len(strN) - 5, strN, ".", vbTextCompare) - 1) ws.Activate Application.DisplayAlerts = False FilePath = UCase(ws.Parent.Path & "\" & strN) & ".csv" FSO.DeleteFile (FilePath) If FSO.FileExists(FilePath) Then MsgBox "ERROR: File cannot be deleted." & vbCr & vbCr & "Check if it is read only, " & vbCr & vbCr & "-THEN-" & vbCr & vbCr & "Try closing revit and waiting for file to become available to read/write.", vbCritical Exit Sub End If On Error GoTo 0 ''fso text file- the textwrite has issues with uinicode and Kanji characters showing upp in lieu of ASCII '''https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/openobjTxtFile-method Set objTxtFile = FSO.CreateTextFile(FilePath, True, False) RowMax = ws.Cells(Rows.Count, 1).End(xlUp).Row ColMax = ws.Cells(1, Columns.Count).End(xlToLeft).Column ''Stop ''Look at first row for non-blank parameters For ncol = 1 To ColMax If ws.Cells(1, ncol).Value > "" Or ncol = 1 Then cols.Add (ncol) ''add first column-assuming family name and any column with data in 1st row Next ncol For nRow = 1 To RowMax If nRow = 2 Then nRow = 3 ''skip row 2 strLine = "" ''For each of the used columns repeat the export For Each obj In cols ncol = obj If nRow = 1 And ncol = 1 Then ''null for upper left cell always! strLine = strLine & "," Else strFmt = ws.Cells(nRow, ncol).NumberFormat If strFmt Like "General" Then strFmt = "" strLine = strLine & Format(ws.Cells(nRow, ncol).Value, strFmt) & "," End If Next obj strLine = Left(strLine, Len(strLine) - 1) '''Print #objTxtFile, strLine & vbCrLf objTxtFile.Write (strLine & vbLf) Next nRow ''Close objTxtFile objTxtFile.Close Set FSO = Nothing If VBA.Dir(ws.Parent.Path & "\" & strN & ".csv") <> "" Then MsgBox ":File Exists- Please verify Success writing: " & vbCr & vbCr & FilePath, vbInformation VBA.Shell "C:\WINDOWS\explorer.exe """ & ws.Parent.Path & "", vbNormalFocus Else MsgBox "Error writing", vbCritical End If End Sub

Comments

Popular posts from this blog

Revit area plans adding new types and references (Gross and rentable)

Powerpoint countdown and current time in slides VBA

Revit Python in Visual Studio Revit Stubs 2022 for Python Revit Intellisense