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

显示当前page size:

Application.ActiveDocument.Name
Application.ActiveDocument.PaperSize
Application.ActiveDocument.PaperHeight("inches")

Application.ActiveDocument.PaperWidth("inches")
Sub UseApplication()
    ‘ Holds the description.
    Dim Description As String

    ‘ Get the document description.
    With Application.ActiveDocument
        Description = _
            "Name: " + .Name + vbCrLf + _
            "Description: " + .Description + vbCrLf + _
            "Paper Height: " + _
            CStr(.PaperHeight("inches")) + vbCrLf + _
            "Paper Width: " + _
            CStr(.PaperWidth("inches")) + vbCrLf + _
            "Paper Size: "

        ‘ The paper size requires special handling.
        Select Case .PaperSize
            Case VisPaperSizes.visPaperSizeA3
                Description = Description + "A3"
            Case VisPaperSizes.visPaperSizeA4
                Description = Description + "A4"
            Case VisPaperSizes.visPaperSizeA5
                Description = Description + "A5"
            Case VisPaperSizes.visPaperSizeB4
                Description = Description + "B4"
            Case VisPaperSizes.visPaperSizeB5
                Description = Description + "B5"
            Case VisPaperSizes.visPaperSizeC
                Description = Description + "C"
            Case VisPaperSizes.visPaperSizeD
                Description = Description + "D"
            Case VisPaperSizes.visPaperSizeE
                Description = Description + "E"
            Case VisPaperSizes.visPaperSizeFolio
                Description = Description + "Folio"
            Case VisPaperSizes.visPaperSizeLegal
                Description = Description + "Legal"
            Case VisPaperSizes.visPaperSizeLetter
                Description = Description + "Letter"
            Case VisPaperSizes.visPaperSizeNote
                Description = Description + "Note"
            Case VisPaperSizes.visPaperSizeUnknown
                Description = Description + "Unknown"
        End Select
    End With

    ‘ Get the active page description.
    With Application.ActivePage
        Description = Description + vbCrLf + _
            "Page Width: " + _
            CStr(.Shapes("thePage").Cells("PageWidth")) + _
            vbCrLf + "Page Height: " + _
            CStr(.Shapes("thePage").Cells("PageHeight"))

    End With

    ‘ Display the description on screen.
    MsgBox Description, _
        vbInformation Or vbOKOnly, _
        "Document and Page Description"
End Sub

显示当前所有pages.

ActiveDocument.Pages
Option Explicit

Sub ListPages()
    ‘ Holds the list of pages.
    Dim ThePages As Pages

    ‘ Holds the page information.
    Dim PageNames As String
    Dim ThisPage As Page

    ‘ Get the list of pages.
    Set ThePages = ActiveDocument.Pages

    ‘ Obtain the page information.
    For Each ThisPage In ThePages

        ‘ Check for a drawing page.
        PageNames = PageNames + ThisPage.Name + vbCrLf
    Next

    ‘ Display the results.
    MsgBox PageNames, vbInformation Or vbOKOnly, "Drawing Pages in Document"
End Sub

显示当前page所有shapes.

ActivePage.Shapes
Sub ListShapes()
    ‘ Holds the list of shapes for a page.
    Dim TheShapes As Shapes

    ‘ Obtain the list of shapes.
    Set TheShapes = ActivePage.Shapes

    ‘ Holds individual shape data.
    Dim ThisShape As Shape
    Dim ShapeNames As String

    ‘ Obtain each shape and add it to the list.
    For Each ThisShape In TheShapes
        ShapeNames = ShapeNames + ThisShape.Name + vbCrLf
    Next

    ‘ Display the results on screen.
    MsgBox ShapeNames, _
           vbInformation Or vbOKOnly, _
           "Shapes on Current Page"
End Sub

显示对应shapesheet的cells.

ActivePage.Shapes("xxx").RowCount
ActivePage.Shapes("xxx").CellsSRC
Sub ListCells()
    ‘ Holds the current shape.
    Dim TheShape As Shape

    ‘ Loop counter variables.
    Dim RowCount As Integer
    Dim CellCount As Integer

    ‘ Holds the current cell information.
    Dim TheCell As Cell
    Dim CellName As String

    ‘ Obtain a selected shape.    ‘ shapename,refer before papers
    Set TheShape = ActivePage.Shapes("xxx")

    ‘ Open the file that will contain the cell names.
    Open ThisDocument.Path + "\CellNames.txt" For Output As #1

    ‘ Process each of the cell rows.
    For RowCount = 0 To TheShape.RowCount(visSectionProp) - 1

        ‘ Process each cell in the row.
        For CellCount = 0 To TheShape.RowsCellCount(visSectionProp, RowCount) - 1

            ‘ Obtain the specific cell.
            Set TheCell = TheShape.CellsSRC(visSectionProp, RowCount, CellCount)

            ‘ Save the name of the Cell.
            CellName = TheCell.Name + vbCrLf

            ‘ Output the data.
            Write #1, CellName
        Next
    Next

    ‘ Close the file.
    Close #1

End Sub

两个有用的链接:

REF:https://msdn.microsoft.com/en-us/library/aa201749

https://msdn.microsoft.com/en-us/library/aa730963(v=office.12).aspx

时间: 2024-08-29 04:36:36

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

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学习笔记之随机数&数组redim

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

VBA学习笔记之单元格

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

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学习笔记(1)-Error处理

1. On Error GoTo line line 参数可以是任何标签或行号,且该line必须在该过程中: 2. On Error Resume Next 当程序运行时发生错误时,转到错误语句下一行执行 3. On Error Goto 0 禁止错误程序处理 从程序来讲,其实方法一可取,而且Err.Description 很好用,返回有用的提示信息.

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尼玛这么多关键字要死啊!不骂不痛快!本来思考的就很累

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

VBA学习笔记(8)-Application Object Members

Application Object Members Represents an instance of Microsoft Office Visio. An external program typically creates or retrieves an Application object before it can retrieve other Visio objects from that instance. Use the Microsoft Visual Basic Create

VBA学习笔记之工作簿

Workbook工作簿的常用功能: 1. 新建工作簿 Dim wb As Workbook Application.SheetsInNewWorkbook = 1     '设置初始工作簿中的工作表数 Set wb = Application.WorkBooks.Add wb.Worksheets(1).name = "表1"                '给第一个工作表设置名称 Application.SheetsInNewWorkbook = 3 2.用Excel对话框打开Exc