1.科普
网上有一段老外的VBA代码
先给自己科普一下VBA
Visual Basic for application—是Microsoft Office软件的应用程序视觉化脚本,这个在上三节中我已经深刻体会到了
2.为了看懂下面的代码
数据类型编辑基本数据类型即Primary Type Data,下述列表的括号内为字节数: [2]Byte (1):无符号数类型,取值范围0-255Boolean (2)Integer(2)Long (4)Single (4)Double (8)Currency (8)Decimal (14)Date (8)StringObject (4)Variant (根据分配确定) [2]自定义的数据类型相当于C语言的struct,例如: [2]Type 自定义类型名 元素名 As 类型 … [元素名 As 类型] End Type数组Option Base 0 :数组索引值从0开始 [2]Option Base 1 :数组索引值从1开始Dim MyArray(10) :声明一个数组变量,10是最大的可用的数组索引值MyArray(5) = 101 :给数组的元素赋值Dim Data(10,5) :声明一个二维数组变量Data(1,1) = "A001" :给数组元素赋值Dim cArr(-11 To 20, 1 To 3) As String :声明一个数组,定义数组索引值的上下界Dim dArr() As String :声明动态数组ReDim dArr(0 To 5, 1 To 2) :改变动态数组的尺寸默认把原数据清除。如果保留原来的数据,必须加上参数Preserve:使用Preserve参数时只能改变最后一位的大小If UBound(vTemp) = -1 Then:判断数组变量vTemp是否为空数组End If Erase MyArrar, Data Erase语句清除数组元素,释放变量占用的空间
3.VBA代码
Option Explicit
Public Sub LocationTable()
‘This routine will create a text file of the location and size of all 2-d shapes
‘ on the current page
Dim shpObj As Visio.Shape, celObj As Visio.Cell
Dim ShpNo As Integer, Tabchr As String, localCent As Double
Dim LocationX As String, LocationY As String
Dim ShapeWidth As String, ShapeHeight As String
Dim unit As String
unit = “mm”
‘Open or create text file to write data
Open “C:\temp\LocationTable.xml” For Output Shared As #1
Tabchr = Chr(9) ‘Tab
Print #1, “<?xml version=””1.0”” encoding=””gb2312”” ?>”
Print #1, “
Print #1, “
‘Loop Shapes collection
For ShpNo = 1 To Visio.ActivePage.Shapes.Count
Set shpObj = Visio.ActivePage.Shapes(ShpNo)
If Not shpObj.OneD Then ‘ Only list the 2-D shapes
‘Get location Shape
Set celObj = shpObj.Cells(“pinx”)
localCent = celObj.Result(unit)
LocationX = localCent ‘ Format(localCent, “000.0000”)
Set celObj = shpObj.Cells(“piny”)
localCent = celObj.Result(unit)
LocationY = Format(localCent, “000.0000”)
‘Get Size Shape
Set celObj = shpObj.Cells(“width”)
localCent = celObj.Result(unit)
ShapeWidth = Format(localCent, “000.0000”)
Set celObj = shpObj.Cells(“height”)
localCent = celObj.Result(unit)
ShapeHeight = Format(localCent, “0.0000”)
‘Write values to Text file starting Name of Shape
Print #1, “
End If
Next ShpNo
Print #1, “
Print #1, “
‘Close Textfile
Close #1
‘Clean Up
Set celObj = Nothing
Set shpObj = Nothing
End Sub
4.C#代码
private void SaveShapeXML(string filePath,Page page){//生成KML文件,注意大小写FileStream fs = new FileStream(filePath, FileMode.Create);XmlTextWriter w = new XmlTextWriter(fs, Encoding.UTF8);string unit = "mm";// 开始文档w.Formatting = System.Xml.Formatting.Indented;w.WriteStartDocument();w.WriteStartElement("document");//向先前创建的元素中添加一个属性w.WriteAttributeString("path", filePath);w.WriteAttributeString("name", axDrawingControl1.Document.Name);w.WriteStartElement("shapes");w.WriteAttributeString("unit", unit);//声明一些变量Cell celObj;int shpNo;double localCent;string locationX;string locationY;string shapeWidth;string shapeHeight;foreach (Shape shpObj in page.Shapes){//Get location ShapecelObj = shpObj.Cells["pinx"];localCent = celObj.Result[unit];locationX = string.Format(localCent.ToString(), "000.000");celObj = shpObj.Cells["piny"];localCent = celObj.Result[unit];locationY = string.Format(localCent.ToString(), "000.000");//Get Size ShapecelObj = shpObj.Cells["width"];localCent = celObj.Result[unit];shapeWidth = string.Format(localCent.ToString(), "000.000");celObj = shpObj.Cells["height"];localCent = celObj.Result[unit];shapeHeight = string.Format(localCent.ToString(), "000.000");string bound = string.Format("{0},{1},{2},{3}", locationX, locationY, shapeWidth, shapeHeight);w.WriteStartElement("shape");w.WriteAttributeString("name", shpObj.Name);w.WriteAttributeString("type", shpObj.Type.ToString());w.WriteAttributeString("text", shpObj.Text);w.WriteAttributeString("bounds", bound);w.WriteEndElement();//结束shape}w.WriteEndElement();//关闭shapesw.WriteEndElement();//关闭documentw.WriteEndDocument();//关闭流w.Close();fs.Close();}
