1.科普

网上有一段老外的VBA代码
先给自己科普一下VBA
Visual Basic for application—是Microsoft Office软件的应用程序视觉化脚本,这个在上三节中我已经深刻体会到了

2.为了看懂下面的代码

  1. 数据类型
  2. 编辑
  3. 基本数据类型
  4. Primary Type Data,下述列表的括号内为字节数: [2]
  5. Byte (1):无符号数类型,取值范围0-255
  6. Boolean (2)
  7. Integer(2)
  8. Long (4)
  9. Single (4)
  10. Double (8)
  11. Currency (8)
  12. Decimal (14)
  13. Date (8)
  14. String
  15. Object (4)
  16. Variant (根据分配确定) [2]
  17. 自定义的数据类型
  18. 相当于C语言的struct,例如: [2]
  19. Type 自定义类型名 元素名 As 类型 [元素名 As 类型] End Type
  20. 数组
  21. Option Base 0 :数组索引值从0开始 [2]
  22. Option Base 1 :数组索引值从1开始
  23. Dim MyArray(10) :声明一个数组变量,10是最大的可用的数组索引值
  24. MyArray(5) = 101 :给数组的元素赋值
  25. Dim Data(10,5) :声明一个二维数组变量
  26. Data(1,1) = "A001" :给数组元素赋值
  27. Dim cArr(-11 To 20, 1 To 3) As String :声明一个数组,定义数组索引值的上下界
  28. Dim dArr() As String :声明动态数组
  29. ReDim dArr(0 To 5, 1 To 2) :改变动态数组的尺寸默认把原数据清除。如果保留原来的数据,必须加上参数
  30. Preserve:使用Preserve参数时只能改变最后一位的大小
  31. If UBound(vTemp) = -1 Then:判断数组变量vTemp是否为空数组
  32. 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, “ LocationX; ","; LocationY; ","; ShapeWidth; ","; ShapeHeight; """ />“
End If

Next ShpNo

Print #1, “

Print #1, “

‘Close Textfile
Close #1

‘Clean Up
Set celObj = Nothing
Set shpObj = Nothing
End Sub


4.C#代码

  1. private void SaveShapeXML(string filePath,Page page)
  2. {
  3. //生成KML文件,注意大小写
  4. FileStream fs = new FileStream(filePath, FileMode.Create);
  5. XmlTextWriter w = new XmlTextWriter(fs, Encoding.UTF8);
  6. string unit = "mm";
  7. // 开始文档
  8. w.Formatting = System.Xml.Formatting.Indented;
  9. w.WriteStartDocument();
  10. w.WriteStartElement("document");
  11. //向先前创建的元素中添加一个属性
  12. w.WriteAttributeString("path", filePath);
  13. w.WriteAttributeString("name", axDrawingControl1.Document.Name);
  14. w.WriteStartElement("shapes");
  15. w.WriteAttributeString("unit", unit);
  16. //声明一些变量
  17. Cell celObj;
  18. int shpNo;
  19. double localCent;
  20. string locationX;
  21. string locationY;
  22. string shapeWidth;
  23. string shapeHeight;
  24. foreach (Shape shpObj in page.Shapes)
  25. {
  26. //Get location Shape
  27. celObj = shpObj.Cells["pinx"];
  28. localCent = celObj.Result[unit];
  29. locationX = string.Format(localCent.ToString(), "000.000");
  30. celObj = shpObj.Cells["piny"];
  31. localCent = celObj.Result[unit];
  32. locationY = string.Format(localCent.ToString(), "000.000");
  33. //Get Size Shape
  34. celObj = shpObj.Cells["width"];
  35. localCent = celObj.Result[unit];
  36. shapeWidth = string.Format(localCent.ToString(), "000.000");
  37. celObj = shpObj.Cells["height"];
  38. localCent = celObj.Result[unit];
  39. shapeHeight = string.Format(localCent.ToString(), "000.000");
  40. string bound = string.Format("{0},{1},{2},{3}", locationX, locationY, shapeWidth, shapeHeight);
  41. w.WriteStartElement("shape");
  42. w.WriteAttributeString("name", shpObj.Name);
  43. w.WriteAttributeString("type", shpObj.Type.ToString());
  44. w.WriteAttributeString("text", shpObj.Text);
  45. w.WriteAttributeString("bounds", bound);
  46. w.WriteEndElement();//结束shape
  47. }
  48. w.WriteEndElement();//关闭shapes
  49. w.WriteEndElement();//关闭document
  50. w.WriteEndDocument();
  51. //关闭流
  52. w.Close();
  53. fs.Close();
  54. }