VBA学习笔记(7)-经典例子

例:VBA获取shape position

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

     ‘Open or create text file to write data
    Open "D:\LocationTable.txt" For Output Shared As #1

    Tabchr = Chr(9) ‘Tab

     ‘Loop Shapes collection
    For ShpNo = 1 To Visio.ActivePage.Shapes.Count

        Set shpObj = Visio.ActivePage.Shapes(ShpNo)
        shapeCount = shpObj.Shapes.Count
        If shapeCount > 1 Then
          i = 1
          For i = 1 To shapeCount
              MsgBox vsoShape.Shapes(i).Text
          Next i
        Else
        If Not shpObj.OneD Then ‘ Only list the 2-D shapes
                ‘Get location Shape
               Set celObj = shpObj.Cells("pinx")
               ‘localCent = celObj.Result("mm")
               localCent = celObj.Result("inches")

               LocationX = Format(localCent, "000.0000")
               Set celObj = shpObj.Cells("piny")
               localCent = celObj.Result("inches")
               LocationY = Format(localCent, "000.0000")

                ‘Get Size Shape
               Set celObj = shpObj.Cells("width")
               localCent = celObj.Result("inches")
               ShapeWidth = Format(localCent, "000.0000")
               Set celObj = shpObj.Cells("height")
               localCent = celObj.Result("inches")
               ShapeHeight = Format(localCent, "000.0000")

               ‘Write values to Text file starting Name of Shape
               Print #1, shpObj.Name; shpObj.Text; Tabchr; _
               Tabchr; LocationX; Tabchr; LocationY; _
               Tabchr; ShapeWidth; Tabchr; ShapeHeight
        End If

    Next ShpNo
     ‘Close Textfile
    Close #1

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

经过修改,提取指定shape。

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 shpObj2    As Visio.Shape
    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 shapeCount As Integer
    Dim i As Integer
     ‘Open or create text file to write data
    Open "D:\LocationTable.txt" For Output Shared As #1

    Tabchr = Chr(9) ‘Tab

     ‘Loop Shapes collection
    For ShpNo = 1 To Visio.ActivePage.Shapes.Count
        Set shpObj2 = Visio.ActivePage.Shapes(ShpNo)
        If Not shpObj2.OneD Then ‘ Only list the 2-D shapes
            shapeCount = shpObj2.Shapes.Count
            If shapeCount > 1 Then
                i = 1
                For i = 1 To shapeCount
                     Set shpObj = shpObj2.Shapes(i)
                     If (shpObj.CellsSRC(visSectionObject, visRowLine, visLinePattern) = 1) Then
                          ‘Get location Shape
                         Set celObj = shpObj.Cells("pinx")
                         ‘localCent = celObj.Result("mm")
                         localCent = celObj.Result("inches")

                         LocationX = Format(localCent, "000.0000")
                         Set celObj = shpObj.Cells("piny")
                         localCent = celObj.Result("inches")
                         LocationY = Format(localCent, "000.0000")

                          ‘Get Size Shape
                         Set celObj = shpObj.Cells("width")
                         localCent = celObj.Result("inches")
                         ShapeWidth = Format(localCent, "000.0000")
                         Set celObj = shpObj.Cells("height")
                         localCent = celObj.Result("inches")
                         ShapeHeight = Format(localCent, "000.0000")

                         ‘Write values to Text file starting Name of Shape
                         Print #1, shpObj.Name; shpObj.Text; Tabchr; _
                         Tabchr; LocationX; Tabchr; LocationY; _
                         Tabchr; ShapeWidth; Tabchr; ShapeHeight
                     End If
                Next i
            Else
                Set shpObj = Visio.ActivePage.Shapes(ShpNo)
                If (shpObj.CellsSRC(visSectionObject, visRowLine, visLinePattern) = 1) Then
                   ‘MsgBox shpObj.CellsSRC(visSectionObject, visRowLine, visLinePattern)
                    ‘Get location Shape
                   Set celObj = shpObj.Cells("pinx")
                   ‘localCent = celObj.Result("mm")
                   localCent = celObj.Result("inches")

                   LocationX = Format(localCent, "000.0000")
                   Set celObj = shpObj.Cells("piny")
                   localCent = celObj.Result("inches")
                   LocationY = Format(localCent, "000.0000")

                    ‘Get Size Shape
                   Set celObj = shpObj.Cells("width")
                   localCent = celObj.Result("inches")
                   ShapeWidth = Format(localCent, "000.0000")
                   Set celObj = shpObj.Cells("height")
                   localCent = celObj.Result("inches")
                   ShapeHeight = Format(localCent, "000.0000")

                   ‘Write values to Text file starting Name of Shape
                   Print #1, shpObj.Name; shpObj.Text; Tabchr; _
                   Tabchr; LocationX; Tabchr; LocationY; _
                   Tabchr; ShapeWidth; Tabchr; ShapeHeight
               End If
            End If
        End If

    Next ShpNo
     ‘Close Textfile
    Close #1

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

REF: http://www.vbaexpress.com/kb/getarticle.php?kb_id=506

时间: 2024-10-12 07:12:08

VBA学习笔记(7)-经典例子的相关文章

VBA学习笔记之VBA学习思路

进阶的知识点 1. SQL查询语句和ADO2. 正则表达式和网抓3. 窗体与控件4. API 类模块 等等 作者:SOROSay链接:https://www.zhihu.com/question/26078625/answer/132542043来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 1小时 熟悉数据类型.变量.常量和运算符 1.了解VBA有哪些数据类型 2.知道如何定义变量,了解public/dim/private定义变量时的区别 3.知道如何定义常量

VBA学习笔记(5)-几个有用的例子

显示当前page size: Application.ActiveDocument.Name Application.ActiveDocument.PaperSize Application.ActiveDocument.PaperHeight("inches") Application.ActiveDocument.PaperWidth("inches") Sub UseApplication() ' Holds the description. Dim Desc

VBA学习笔记(11)-经典代码之 (Visio中升级代码)

Option Explicit Private Function add() On Error GoTo ErrHandler Dim filePath As String, fileName As String filePath = Application.ActiveDocument.Path fileName = filePath + "hello.bas" Visio.Application.Vbe.ActiveVBproject.VBComponents.Import (fi

VBA学习笔记之随机数&数组redim

发现更简单的: a=Application.RandBetween(-10, 10) 直接生成-10到10之间的随机整数 关于二维数组Redim Preserve: 如果使用了 Preserve 关键字,就只能重定义数组最末维的大小,且根本不能改变维数的数目. 具体参考 1 如果使用了 Preserve 关键字,就只能重定义数组最末维的大小,且根本不能改变维数的数目.例如,如果数组就是一维的,则可以重定义该维的大小,因为它是最末维,也是仅有的一维.不过,如果数组是二维或更多维时,则只有改变其最末

VBA学习笔记之单元格

'单元格对象在VBA中一个非常基础,同时也很重要的. '它的表达方式也是非常的多样化. '---------------------------------------------------- 'Range 对象 '代表某一单元格.某一行.某一列.某一选定区域(该区域可包含一个或若干连续单元格区域),或者某一三维区域. 'Range ("文本型装单元格地址") 'range的常见写法 Sub rng() Range("a1").Select '单元格 Range(

SharpGL学习笔记(十三) 光源例子:环绕二次曲面球体的光源

这是根据徐明亮<OpenGL游戏编程>书上光灯一节的一个例子改编的. 从这个例子可以学习到二次曲面的参数设置,程序中提供了两个画球的函数,一个是用三角形画出来的,一个是二次曲面构成的. 你会发现,跟三角形版本不同,二次曲面要做一些设定,否则画出来的球体无法接受光照. 先上代码: 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 usi

VBA学习笔记之循环

VBA 中Do while Loop用法如下:VBA中如果不知道重复多少次,使用 Do...Loop 语句.Do...Loop 语句重复执行某段代码直到条件是 true 或条件变成 true.重复执行代码直到条件是 true使用 While 关键字来检查 Do... Loop 语句的条件. 1 2 3 Do While i>10   'some code Loop 如果 i 等于 9,上述循环内的代码将终止执行. 1 2 3 Do   'some code Loop While i>10 这个

VBA学习笔记(8)--遍历所有文件夹和文件

说明(2017.3.26): 1. 采用的是兰色幻想教学视频中的"父子转换法" 2. 这种VBA的遍历文件夹方法非常难理解,主要是因为dir这个函数,第一次带参数调用,返回的是此目录下的第一个文件,第二次无参数调用,返回的是此目录下一个第二个文件,这就很操蛋了,还要配合do循环. 3. VBA的各种do..until..loop, do..while..loop, if..then..end if, for 1 to 10..next尼玛这么多关键字要死啊!不骂不痛快!本来思考的就很累

No2_4.接口继承多态_Java学习笔记_经典案例

1 import java.util.ArrayList; 2 import java.util.Collections; 3 import java.util.List; 4 5 /** 6 * @title 接口继承多态的经典范例 7 * @author 作者:sunshine yaqingl 8 * @date 创建时间:2016年7月6日 下午5:27:39 9 */ 10 11 //使用Comparable接口自定义排序 12 class Employee implements Com