你的位置:首页 > 操作系统

[操作系统]MicroStation VBA基础


实习笔记1

2016年8月1日

14:12

Option Explicit

缺省情况下,如果使用一个没有声明的变量,它将继承“Variant”类型。在模块、窗体和类的通用声明区使用“OptionExplicit”能强制我们必须声明变量后才能使用变量

Sample:

Option ExplicitSub test()X = 5End Sub

 

在通用声明区声明了“Option Explicit”。当试着去运行上述test宏时将得到一个错误

本章回顾

1.在过程、函数或用户窗体事件中写代码

2.在过程和函数中利用必须的和可选的参数

3.函数可返回值、数组、类型和对象

4.在过程和函数中,如果参数声明为“ByRef”,则可以改变作为参数传递进来的变量。把参数声明为“ByVal”将保持变量不变

4.能在过程、函数和事件中或者在通用声明区内声明变量。这些变量的范围依赖于它们在何处被声明以及声明中所用的关键字

第六章 变量

Option ExplicitSub tst()TTT = 5MsgBox TTTEnd Sub'整型Dim PageNumber As IntegerPageNumber = 84'长整型Dim MySalary As LongMySalary = 123456'双精度型Dim HourToLearnVBA As DoubleHourToLearnVBA = 36.25Sub VariableTestC()Dim N As DoubleN = 1.23456789012346N = 12345678901234.6End Sub'布尔类型Dim ICanLearnThis As BooleanICanLearnThis = True'日期型Dim XMReleaseDate As DateXMReleaseDate = "5/19/2006 8:00:00 AM"'字符型Dim MYLevelName As StringMYLevelName = "utilElectricity"'对象型Dim MyExcelApp As ObjectSet MyExcelApp = GetObject(, "Excel.Application")'VariantDim PointArray As Variant'MicroStation特有的变量类型'Application(应用)Dim MSAPP As ApplicationSet MSAPP = Application'ActiveDesignFile当前设计文件'ActiveModelReference当前模型参考'ActiveSettings 当前设置'VBProject object & attributes 对象以及属性'UserName用户名'取得左、顶、宽和高属性'DesignFile设计文件Dim MyDGN As DesignFileSet MyDGN = Application.ActiveDesignFile'Author 作者,Client客户,Comments注释,Company公司,KeyWords关键字,Manager管理者,Subject主题,Title标题'FormatMajorVersion 格式主版本 FormatMinorVersion格式次版本'Levels层, Models模型, Name 名称,Path 路径'ModelReference 模型参考Dim MyModel As ModelReferenceSet MyModel = Application.ActiveModelReference'Level 层Dim MyLevel As LevelSet MyLevel = Application.ActiveDesignFile.Levels(1)'Description 描述, ElementColor元素颜色,ElementLineStyle元素线性,ElementLineWeight元素线宽,IsActive是否激活,IsDisplayed是否显示'IsFrozen是否冻结,IsLocked是否锁定,Name名字,Number层号,Plot光栅绘图'LineElement线元素Dim MyLine As ApplicationSet MyLine = Application.CreateLineElement2(Nothing, Point3dFromXYZ(0, 0, 0), Point3dFromXYZ(4, 5, 6))Application.ActiveModelReference.AddElement MyLine'EllipseElement椭圆元素Dim MyCircle As EllipseElementDim RotMatrix As Matrix3dSet MyCircle = CreateEllipseElement2(Nothing, Point3dFromXYZ(0, 0, 0), 1.5, 1.5, RotMatrix)Application.ActiveModelReference.AddElement MyCircle'ArcElement 弧元素Dim MyArc As ArcElementDim RotMatrix As Matrix3dSet MyArc = CreateArcElement2(Nothing, Point3dFromXYZ(0, 0, 0), 1.75, 1.75, RotMatrix, Radians(45), Radiands(90))Application.ActiveModelReference.AddElement MyArc'TextElement文本元素Dim MyText As TextElementDim RotMatrix As Matrix3dSet MyText = CreateTextElement1(Nothing, "MicroStation VBA", Point3dFromXYZ(0, 0, 0), RotMatrix)Application.ActiveModelReference.AddElement MyText'赋值和设置对象Dim LevelName As StringLevelName = "Element"Dim EasementLevel As LevelSet EasementLevel = ActiveDesignFile.AddNewLevel(LevelName)Dim StartPoint(0 To 2) As DoubleStartPoint(0) = 4.5StartPoint(1) = 5.6StartPoint(2) = 6.7Sub ArrayTestA()Dim MyVerticies(0 To 1) As Point3dDim MyLine As LineElementMyVerticies(0).X = 1MyVerticies(0).Y = 2MyVerticies(1).X = 3MyVerticies(1).Y = 4MyVerticies(2).X = 5MyVerticies(2).Y = 6Set MyLine = CreateLineElement1(Nothing, MyVerticies)ActiveModelReference.AddElement MyLineEnd SubSub VariableTestD()Dim MySalary As DoubleDim MySalary As DoubleMySalary = 1234567MyHourly = MySalary / 52 / 40MsgBox "My Hourly Rate is " & FormatCurrency(MyHourly, 2, vbFalse, vbFalse, vbTrue)End Sub

 

本章回顾:

变量是保存值或指向对象的名字。在函数、过程或事件内声明的变量是局部的,仅能在声明它的函数中使用,不能在函数外使用。在窗体或代码模块通用声明区中声明的变量能在声明它们的窗体或代码模块内任何地方使用。在代码模块中声明为“Public”的变量能在VBA工程的任何位置使用,在类模块中声明为“public”的变量成为类模块的读/写属性

如果没有变量,一切将是静态的什么也不能代表——线总是从固定的一点画到固定的另一点,文本总是插入在同一点且内容总是相同的。

第七章 操作文本

UCase大写转换Function Ucase(String)LCase小写转换Function Lcase(String)StrConv字符串转换Function StrConv(String, Conversion As VbStrConv, [LocaleID AS Long])WeekDayName星期名 WeekDay日期转星期Function WeekDayName(Weekday As Long ,[Abbreviate As Boolean = False],[FirstDayOffWeek As VbDayOfWeek = vbUseSystemDayOfWeek]) As StringMonthNAme月名Function MonthName(Month As Long, [Abbreviate As Boolean = False]) As StringLTrim左截取 RTrim右截取 Trim两端截取Function LTrim(String)Function RTrim(String)Function Trim(String)StrComp字符串比较Function StrComp(String1, String2, [Compare As VbCompareMethod = vbBinaryCompare]Left左取字符串Function Left(String ,Length As Long)Right右取字符串Function Right(String, Length As Long)Mid 取中串Function Mid(String, Start As Long, [Length])Sub TextWork12()Dim BookTitle As StringBookTitle = "Learning MicroStation VBA"Debug.Print Mid(BookTitle, 3, 6)Debug.Print Mid(BookTitle, 6)Debug.Print Mid(BookTitle, InStr(1, BookTitle, " ") + 1)End Sub

 

'注意上面的InStr里面的" "里面是有一个空格

运行结果:

arning

ing MicroStation VBA

MicroStation VBA

第一个是从第三个字符开始并返回六个字符

第二个是从第六个字符开始并返回其后的每个字符

第三个是以Instr函数在变量BookTitle中查找第一个空格的位置并加上“1”作为起始,这样实际上是从第一个空格后开始的。如果没有指定长度,将得到空格后的所有内容

Replace 替换字符Function Replace(Expression As String, Find As String, Replace As String, [Start As Long = 1],[Count As Long = -1], [Compare As VbCompareMethod = vbBinaryCompare])AS StringSub TextWork13()Dim FilePath As StringDim FilePath2 As StringFilePath = Application.ActiveDesignFile.FullNameFilePath2 = Replace(FilePath, "\", "//")MsgBox FilePath & "turns into" & vbCr & FilePath2End SubInStr 子串位置Function InStr([Start],[String1],[String2],[Compare As VbCompareMethod = vbBinaryCompare])InStrRev反向子串位置Function InStrRev(StringCheck As String, StringMatch As String, [Start As Long = -1],[Compare As VbCompareMethod = vbBinaryCompare]) As LongSplit 分割和 Join联结Function Split(Expression As String,[Delimiter],[Limit As Long = -1],[Compare As VbCompareMethod = vbBinaryCompare])Function Join(SourceArray,[Delimiter]) As StringAsc 字符到ASCII码 和Chr ASCII码到字符Function Asc(Stirng As String) As IntegerFunction Chr(CharCode As Long)FormatCurrency格式化货币Function FormatCurrency(Expression, [NumDigitaAfterDecimal As Long = -1],[IncludeLeadingDigit As VbTriState = vbUseDefault],[GroupDigits As VbTriState = vbUseDefault])As StringFormatNumber格式化数字Function FormatNumber(Expression,[NumDigitsAfterDecimal As Long =-1],[IncludeLeadingDigit As VbTriState = vbUseDefault],[UseParensForNegativenumbers As VbTriState = vbUseDefault],[GroupDigits As VbTriState = vbUseDefault]) As StringFormatDateTime格式化日期Funcion FormatDateTime(Expression,[NamedFormat As VbDateTimeFormat = vbGeneralDate]) As StringFormat格式化Function Format(Expression, [Format],[FirstDayOfWeek As VbDayOfWeek = vbSunday], [FirstWeekOfYear As VbFirstWeekOfYear As VbFirstWeekOfYear = vbSunday],[FirstWeekOfYear As VbFirstWeekOfYear = vbFirstJan1])

 

&连接符

用来连接字符串

VbCr回车符<Enter>键

VbTab制表符<Tab>键

本章回顾:

字符串是指由文本、字母、数字和其他字符组合的单个文本块。本章集中讲述了如何操作这些字符串,例如大写转化,小写转化。连接字符串,格式化字符串,分割字符串等操作

<F8>可以单步

第8章 操作数字

加、减、乘、除、平方与指数(^2 或者^n)、平方根Sqr