首页 > 代码库 > VBA学习笔记(5)-几个有用的例子

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 ExplicitSub 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

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