You are on page 1of 3

Option Explicit

' ------------------------------------------------------------------------
' Catia Thread Standard migration for V5R22 and higher
' Migrate XLS thread standards used in V5 R21 and lower
'
' written for Janicki Industries of Sedro-Woolley WA. 98284
' by Steve Roemish ph. 360-814-1822, Feb. 2014
'-------------------------------------------------------------------------
Const FORREADING = 1, FORWRITING = 2, FORAPPENDING = 3

Sub Main()
Dim q As String, r As String, s As String
Dim StdNam As String
Dim objWorkbook As Workbook, objWorksheet As Worksheet
Dim MyResp, i As Integer
Dim FSO, f
Dim str1 As String, str2 As String, MyUOM As String
Dim ThdDia As String, ThdPch As String, NomDia As String, ThdDes As String
Dim MyRow, MyCell
'
==================================================================================
=
On Error Resume Next
'Capture common Chr items for easy reuse
q = Chr(34) 'quote character
r = vbNewLine 'return character
s = Chr(32) 'space character

StartAgain: 'Allow starting over return point


' StdNam = legancey thread standard name from excel
StdNam = InputBox("Paste .xls file path:", "Excel File Path") 'Get xls thread standard from user input
If StdNam = "" Then 'test for empty input
MyResp = MsgBox("You did not enter anything, Do you want to try again?", vbYesNo, "Input Error")
If MyResp = vbYes Then GoTo StartAgain
If MyResp = vbNo Then Exit Sub
End If

Set objWorkbook = Excel.Application.Workbooks.Open(StdNam) 'open thread standard


If Err <> 0 Then 'test for error on open
MyResp = MsgBox("There was something wrong with your filepath."& _
r & "Would you like to try again?", vbYesNo, "Input Error")
If MyResp = vbYes Then GoTo StartAgain
If MyResp = vbNo Then Exit Sub
End If

MyUOM = InputBox("Inch or Metric?" & r & "Enter in OR mm", "Unit Of Measure") 'User Input for units of
measure
If MyUOM = "in" Or MyUOM = "mm" Then 'test input
'do nothing
Else
MyResp = MsgBox("You didn't entee a valid Unit Of Measure" & _
r & "Would you like to try again?", vbYesNo, "Input Error")
If MyResp = vbYes Then GoTo StartAgain
If MyResp = vbNo Then Exit Sub
End If

StdNam = Replace(StdNam, ".xls", ".xml") 'replace .xls with .xml in file name
objWorkbook.Activate 'make thread standard the active workbook
Set objWorksheet = objWorkbook.Worksheets.Item(1) 'get the first worksheet
ThdDia = objWorksheet.Range("A2").Value 'get thread diameter
ThdPch = objWorksheet.Range("B2").Value 'get thread pitch
NomDia = objWorksheet.Range("C2").Value 'get tap diameter
ThdDes = objWorksheet.Range("D2").Value 'get thread description

' Creat XML file for Thread standards output


Set FSO = CreateObject("Scripting.FileSystemObject") 'set file system object
Set f = FSO.OpenTextFile(StdNam, FORWRITING, 8) 'create and open xml file for writing
StdNam = Right(StdNam, Len(StdNam) - InStrRev(StdNam, "\")) 'drop path for only file name to remain

' Let's collect a large amount of header text into one string variable just because
str1 = "<?xml version=" & q & "1.0" & q & " ?>" & r & _
"<std:node name=" & q & StdNam & q & " type="& q & "Thread_standard" & q & _
" xmlns:std=" & q & "http://www.dsweb.com/std"& q & ">" & r & _
r & " <std:typedef name=" & q & "Standard" & q& ">" & r & _
" <std:floatval name=" & q & "ThreadDiameter" & q& " >" & ThdDia & "</std:floatval>" & r& _
" <std:floatval name=" & q & "Pitch" & q & "> " & ThdPch & "</std:floatval>" & r & _
" <std:floatval name=" & q & "NominalDiameter" & q& " >" & NomDia & "</std:floatval>" & r& _
" <std:strval name=" & q & "Description" & q & "> " & ThdDes & "</std:strval>" & r & _
" </std:typedef>" & r & r & _
" <std:node name=" & q & "Key" & q & ">"& r & _
" <std:strval name=" & q & "KeyDrafting" & q & "> THD</std:strval>" & r & _
" <std:strval name=" & q & "KeyDrafting" & q & "> DESC_IN_DRAFTING</std:strval>" & r & _
" </std:node>" & r & r & _
" <std:node name=" & q & "Unit" & q & ">"& r & _
" <std:strval name=" & q & "Unit" & q & " >"& MyUOM & "</std:strval>" & r & _
" </std:node>" & r & r & _
" <std:node name=" & q & "Values" & q & ">"
f.write str1 & r 'Write header text in xml file

objWorksheet.Range("A1:A5000").Find("").Activate 'find the first empty cell for our stop point
Set MyCell = objWorksheet.Application.ActiveCell 'Capture active cell object
MyRow = MyCell.Row - 1 'Get our last row number

For i = 2 To MyRow 'Start loop of xls file here


ThdDia = objWorksheet.Range("A" & i).Value 'get thread diameter
ThdPch = objWorksheet.Range("B" & i).Value 'get thread pitch
NomDia = objWorksheet.Range("C" & i).Value 'get tap diameter
ThdDes = objWorksheet.Range("D" & i).Value 'get thread description
str2 = _
" <std:typeval name=" & q & "Standard" & q & ">" & r & _
" <std:floatval name=" & q & "ThreadDiameter" & q& " >" & ThdDia & "</std:floatval>" & r& _
" <std:floatval name=" & q & "Pitch" & q & "> " & ThdPch & "</std:floatval>" & r & _
" <std:floatval name=" & q & "NominalDiameter" & q& " >" & NomDia & "</std:floatval>" & r& _
" <std:strval name=" & q & "Description" & q & "> " & ThdDes & "</std:strval>" & r & _
" </std:typeval>" & r 'compile xml syntax for string write
f.write str2 & r 'write this standard
Next i 'loop for next standard
f.write " </std:node>"
f.Close 'Close the XML file
objWorkbook.Close 'Close the excel file
End Sub

You might also like