- You can use a function the same way that you use
a sub. In this case a function is used because it can return a value
to the calling statement.
- ByRef passes the address of the argument
instead of the value, which lets you change the variables actual
value.
- oTbl As Inventor.Titleblock is the titleblock
object which represents an instance of a title block on a sheet.
face=Arial size=2>
- ParseXML is a function that I've got from
somebody on the net, that returns the value of an XML string.
The following code returns the value for a prompted
property of a title block on the first sheet of a drawing document. Let me
know if you need more information on how to extract the file property values of
the title block.
'start
code========================================================================
Option Explicit
Const xs = "<"
Const xe = ">"
Const
xend = "</"
Sub test()
Dim oDoc As
DrawingDocument
Dim oTbl As
TitleBlock
Set oDoc =
ThisApplication.ActiveDocument
Set oTbl =
oDoc.Sheets(1).TitleBlock
'Name of
prompted property that you extract the value for
Dim sPropName As String
sPropName
= "<replace this with your property name>"
Dim sPropValue As String
sPropValue
= IvGetPromptedValue(oTbl, sPropName)
MsgBox "Prompted Property" & vbCrLf & vbCrLf
& sPropName & " = " & sPropValue, vbOKOnly
End Sub
Public Function IvGetPromptedValue(ByRef oTbl As Inventor.TitleBlock,
_
sPromptName As String) As String
'get value of specified prompted property
from titleblock and return it
'with Function. If property can't be found
return "-1"
'
'references:
'mXML.ParseXML
Dim oTextBoxes As
Inventor.TextBoxes
Dim lBox As Long
'get textboxes of titleblock
Set
oTextBoxes = oTbl.Definition.Sketch.TextBoxes
'iterate through text boxes and find
value
For lBox = oTextBoxes.Count To 1 Step
-1
If
Left(GetFormattedText(oTextBoxes(lBox)), 8) = "<Prompt>"
Then
Dim
sValue As
String
sValue = ParseXML(oTextBoxes(lBox).FormattedText,
"Prompt")
If
UCase(sValue) = UCase(sPromptName)
Then
IvGetPromptedValue =
oTbl.GetResultText(oTextBoxes(lBox))
Exit
Function
End If
End
If
Next
IvGetPromptedValue = "-1"
End Function
Private Function GetFormattedText(ByRef oTextBox As Inventor.TextBox) As
String
'get value of formatted text property. If error return error
string
On Error Resume Next
GetFormattedText =
oTextBox.FormattedText
If Err
Then
Err.Clear
GetFormattedText =
"<Application-defined or object-defined error>"
End
If
End Function
Private Function ParseXML(ByVal XMLSource As String,
_
ByVal XMLName As String, _
Optional Instance As Integer = 1, _
Optional Default As
Variant = "") As String
Dim x As Integer, y As Integer, XMLStart As Integer,
XMLTag As String, c As String
Dim XMLTagEnd As
String
Dim XMLMatch As Integer, XMLEnd As Integer,
XMLLength As Integer
XMLLength =
Len(XMLSource)
XMLTag = xs + XMLName +
xe
XMLTagEnd = xend + XMLName + xe
'*** Find the start of the requested
intstance...
XMLStart = 1
For x = 1
To Instance
y = InStr(XMLStart,
XMLSource, XMLTag)
If y >=
XMLStart
Then
XMLStart = y + Len(XMLTag)
Else
ParseXML =
Default
Exit Function
End
If
Next
'***
Find the end of the instance...
XMLEnd =
XMLStart
XMLMatch = 1
Do Until
XMLMatch = 0
c = Mid(XMLSource,
XMLEnd, Len(XMLTagEnd))
If c =
XMLTagEnd
Then
XMLMatch = XMLMatch - 1
ElseIf
Left(c, 1) = xs
Then
XMLMatch = XMLMatch + 1
End
If
XMLEnd = XMLEnd +
1
If XMLEnd = XMLLength
Then
ParseXML =
Default
Exit Function
End
If
Loop
ParseXML = Mid(XMLSource, XMLStart, XMLEnd - XMLStart - 1)
End Function
'end
code========================================================================
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Thanks very much for the help, but as I'm a bit new to VBA
I am very frustrated with myself as I cannot understand
everything in the code. I'm used to using subs, and am a bit unsure about functions.
I'm not sure what the 'ByRef oTbl As Inventor.titleblock'
is refering to in the arguments for the first function.
And is the 'ParseXML' important, as I can't find any
reference to it in my VBA help. How do the two functions
relate to each other, ie which on do I call.
And Im having trouble calling the functions, I'm not sure
what to put in the argument list.
(I'm worried I put the wrong thing in my original question,
I want to read the values from the titleblock, and just
use them elsewhere. As long as I can get a set of text
strings containing the values in the title block, i'll be
happy)
Thanks for the previous help