标题: [转贴] VBS对Office软件Word、Excel等的操作实例 [打印本页]
作者: HAT 时间: 2009-9-22 23:55 标题: VBS对Office软件Word、Excel等的操作实例
【Word】
VBS把多个Word文档合并成一个
http://bbs.bathome.net/thread-13081-1-1.html- Add Formatted Text to a Word Document
-
- Demonstration script that displays formatted data in a Microsoft Word document.
- Set objWord = CreateObject("Word.Application")
-
- objWord.Visible = True
- Set objDoc = objWord.Documents.Add()
- Set objSelection = objWord.Selection
-
- objSelection.Font.Name = "Arial"
- objSelection.Font.Size = "18"
- objSelection.TypeText "Network Adapter Report"
- objSelection.TypeParagraph()
-
- objSelection.Font.Size = "14"
- objSelection.TypeText "" & Date()
- objSelection.TypeParagraph()
-
- Add a Formatted Table to a Word Document
-
- Demonstration script that retrieves service data from a computer and then displays that data in a formatted table in Microsoft Word.
- Set objWord = CreateObject("Word.Application")
- objWord.Visible = True
- Set objDoc = objWord.Documents.Add()
-
- Set objRange = objDoc.Range()
- objDoc.Tables.Add objRange,1,3
- Set objTable = objDoc.Tables(1)
-
- x=1
-
- strComputer = "."
- Set objWMIService = _
- GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
- Set colItems = objWMIService.ExecQuery("Select * from Win32_Service")
-
- For Each objItem in colItems
- If x > 1 Then
- objTable.Rows.Add()
- End If
- objTable.Cell(x, 1).Range.Font.Bold = True
- objTable.Cell(x, 1).Range.Text = objItem.Name
- objTable.Cell(x, 2).Range.text = objItem.DisplayName
- objTable.Cell(x, 3).Range.text = objItem.State
- x = x + 1
- Next
-
- Apply a Style to a Table in a Word Document
-
- Demonstration script that retrieves service data from a computer, displays that data in a table in Microsoft Word, then formats the data by using a predefined Microsoft Word style
- Set objWord = CreateObject("Word.Application")
- objWord.Visible = True
- Set objDoc = objWord.Documents.Add()
-
- Set objRange = objDoc.Range()
- objDoc.Tables.Add objRange,1,3
- Set objTable = objDoc.Tables(1)
- objTable.Range.Font.Size = 10
- objTable.Range.Style = "Table Contemporary"
-
- x=2
-
- objTable.Cell(x, 1).Range.Text = "Service Name"
- objTable.Cell(x, 2).Range.text = "Display Name"
- objTable.Cell(x, 3).Range.text = "State"
-
- strComputer = "."
- Set objWMIService = _
- GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
- Set colItems = objWMIService.ExecQuery("Select * from Win32_Service")
-
- For Each objItem in colItems
- If x > 1 Then
- objTable.Rows.Add()
- End If
- objTable.Cell(x, 1).Range.Text = objItem.Name
- objTable.Cell(x, 2).Range.text = objItem.DisplayName
- objTable.Cell(x, 3).Range.text = objItem.State
- x = x + 1
- Next
-
- Add a Table to a Word Document
-
- Demonstration script that retrieves service information from a computer and then displays that information in tabular format in Microsoft Word.
- Set objWord = CreateObject("Word.Application")
- objWord.Visible = True
- Set objDoc = objWord.Documents.Add()
-
- Set objRange = objDoc.Range()
- objDoc.Tables.Add objRange,1,3
- Set objTable = objDoc.Tables(1)
-
- x=1
-
- strComputer = "."
- Set objWMIService = _
- GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
- Set colItems = objWMIService.ExecQuery("Select * from Win32_Service")
-
- For Each objItem in colItems
- If x > 1 Then
- objTable.Rows.Add()
- End If
- objTable.Cell(x, 1).Range.Text = objItem.Name
- objTable.Cell(x, 2).Range.text = objItem.DisplayName
- objTable.Cell(x, 3).Range.text = objItem.State
- x = x + 1
- Next
-
- Append Text to a Word Document
-
- Demonstration script that appends the current date to the existing Microsoft Word document C:\Scripts\Word\Testdoc.doc.
- Const END_OF_STORY = 6
- Const MOVE_SELECTION = 0
-
- Set objWord = CreateObject("Word.Application")
- objWord.Visible = True
-
- Set objDoc = objWord.Documents.Open("c:\scripts\word\testdoc.doc")
- Set objSelection = objWord.Selection
- objSelection.EndKey END_OF_STORY, MOVE_SELECTION
-
- objSelection.TypeParagraph()
- objSelection.TypeParagraph()
-
- objSelection.Font.Size = "14"
- objSelection.TypeText "" & Date()
- objSelection.TypeParagraph()
- objSelection.TypeParagraph()
-
- objSelection.Font.Size = "10"
-
- Create a New Word Document
-
- Demonstration script that creates and displays a new Microsoft Word document.
- Set objWord = CreateObject("Word.Application")
- objWord.Visible = True
- Set objDoc = objWord.Documents.Add()
-
- Create and Save a Word Document
-
- Demonstration script that retrieves network adapter data from a computer, displays that data in a Microsoft Word document, and then saves the document as C:\Scripts\Word\Testdoc.doc.
- Set objWord = CreateObject("Word.Application")
- objWord.Caption = "Test Caption"
- objWord.Visible = True
-
- Set objDoc = objWord.Documents.Add()
- Set objSelection = objWord.Selection
-
- objSelection.Font.Name = "Arial"
- objSelection.Font.Size = "18"
- objSelection.TypeText "Network Adapter Report"
- objSelection.TypeParagraph()
-
- objSelection.Font.Size = "14"
- objSelection.TypeText "" & Date()
- objSelection.TypeParagraph()
- objSelection.TypeParagraph()
-
- objSelection.Font.Size = "10"
-
- strComputer = "."
- Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
- Set colItems = objWMIService.ExecQuery _
- ("Select * from Win32_NetworkAdapterConfiguration")
-
- For Each objItem in colItems
-
- objSelection.Font.Bold = True
- objSelection.TypeText "ARP Always Source Route: "
- objSelection.Font.Bold = False
- objSelection.TypeText "" & objItem.ArpAlwaysSourceRoute
- objSelection.TypeParagraph()
-
- objSelection.Font.Bold = True
- objSelection.TypeText "ARP Use EtherSNAP: "
- objSelection.Font.Bold = False
- objSelection.TypeText "" & objItem.ArpUseEtherSNAP
- objSelection.TypeParagraph()
-
- objSelection.Font.Bold = True
- objSelection.TypeText "Caption: "
- objSelection.Font.Bold = False
- objSelection.TypeText "" & objItem.Caption
- objSelection.TypeParagraph()
-
- objSelection.Font.Bold = True
- objSelection.TypeText "Database Path: "
- objSelection.Font.Bold = False
- objSelection.TypeText "" & objItem.DatabasePath
- objSelection.TypeParagraph()
-
- objSelection.Font.Bold = True
- objSelection.TypeText "Dead GW Detection Enabled: "
- objSelection.Font.Bold = False
- objSelection.TypeText "" & objItem.DeadGWDetectEnabled
- objSelection.TypeParagraph()
-
- objSelection.Font.Bold = True
- objSelection.TypeText "Default IP Gateway: "
- objSelection.Font.Bold = False
- objSelection.TypeText "" & objItem.DefaultIPGateway
- objSelection.TypeParagraph()
-
- objSelection.Font.Bold = True
- objSelection.TypeText "Default TOS: "
- objSelection.Font.Bold = False
- objSelection.TypeText "" & objItem.DefaultTOS
- objSelection.TypeParagraph()
-
- objSelection.Font.Bold = True
- objSelection.TypeText "Default TTL: "
- objSelection.Font.Bold = False
- objSelection.TypeText "" & objItem.DefaultTTL
- objSelection.TypeParagraph()
-
- objSelection.Font.Bold = True
- objSelection.TypeText "Description: "
- objSelection.Font.Bold = True
- objSelection.Font.Bold = False
- objSelection.TypeText "" & objItem.Description
- objSelection.TypeParagraph()
-
- objSelection.TypeParagraph()
-
- Next
-
- objDoc.SaveAs("C:\Scripts\Word\testdoc.doc")
- objWord.Quit
-
- Display Service Information in a Word Document
-
- Demonstration script that retrieves service information from a computer and then displays that data in a Microsoft Word document.
- Set objWord = CreateObject("Word.Application")
- objWord.Visible = True
-
- Set objDoc = objWord.Documents.Add()
- Set objSelection = objWord.Selection
-
- objSelection.TypeText "Services Report"
- objSelection.TypeParagraph()
- objSelection.TypeText "" & Now
- objSelection.TypeParagraph()
- objSelection.TypeParagraph()
-
- strComputer = "."
- Set objWMIService = _
- GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
- Set colItems = objWMIService.ExecQuery("Select * from Win32_Service")
-
- For Each objItem in colItems
- objSelection.TypeText objItem.DisplayName & " -- " & objItem.State
- objSelection.TypeParagraph()
- Next
-
- List Microsoft Word Properties
-
- Demonstration script that lists Microsoft Word configuration settings.
- On Error Resume Next
-
- Set objWord = CreateObject("Word.Application")
- Wscript.Echo "Active Printer:", objWord.ActivePrinter
-
- For Each objAddIn in objWord.AddIns
- Wscript.Echo "AddIn: ", objAddIn
- Next
-
- Wscript.Echo "Application:", objWord.Application
- Wscript.Echo "Assistant:", objWord.Assistant
-
- For Each objCaption in objWord.AutoCaptions
- Wscript.Echo "AutoCaptions:", objCaption
- Next
- Wscript.Echo "Automation Security:", objWord.AutomationSecurity
- Wscript.Echo "Background Printing Status:", objWord.BackgroundPrintingStatus
- Wscript.Echo "Background Saving Status:", objWord.BackgroundSavingStatus
- Wscript.Echo "Browse Extra File Type:", objWord.BrowseExtraFileTypes
- Wscript.Echo "Build:", objWord.Build
- Wscript.Echo "Caps Lock:", objWord.CapsLock
- Wscript.Echo "Caption:", objWord.Caption
-
- For Each objLabel in objWord.CaptionLabels
- Wscript.Echo "Caption Label:", objLabel
- Next
-
- Wscript.Echo "Check Language:", objWord.CheckLanguage
-
- For Each objAddIn in objWord.COMAddIns
- Wscript.Echo "COM AddIn:", objAddIn
- Next
-
- Wscript.Echo "Creator:", objWord.Creator
-
- For Each objDictionary in objWord.CustomDictionaries
- Wscript.Echo "Custom Dictionary:", objDictionary
- Next
-
- Wscript.Echo "Customization Context:", objWord.CustomizationContext
- Wscript.Echo "Default Legal Blackline:", objWord.DefaultLegalBlackline
- Wscript.Echo "Default Save Format:", objWord.DefaultSaveFormat
- Wscript.Echo "Default Table Separator:", objWord.DefaultTableSeparator
-
- For Each objDialog in objWord.Dialogs
- Wscript.Echo "Dialog:", objDialog
- Next
-
- Wscript.Echo "Display Alerts:", objWord.DisplayAlerts
- Wscript.Echo "Display Recent Files:", objWord.DisplayRecentFiles
- Wscript.Echo "Display Screen Tips:", objWord.DisplayScreenTips
- Wscript.Echo "Display Scroll Bars:", objWord.DisplayScrollBars
-
- For Each objDocument in objWord.Documents
- Wscript.Echo "Document:", objDocument
- Next
-
- Wscript.Echo "Email Template:", objWord.EmailTemplate
- Wscript.Echo "Enable Cancel Key:", objWord.EnableCancelKey
- Wscript.Echo "Feature Install:", objWord.FeatureInstall
-
- For Each objConverter in objWord.FileConverters
- Wscript.Echo "File Converter:", objConverter
- Next
-
- Wscript.Echo "Focus In MailHeader:", objWord.FocusInMailHeader
-
- For Each objFont in objWord.FontNames
- Wscript.Echo "Font Name:", objFont
- Next
-
- Wscript.Echo "Height", objWord.Height
-
- For Each objBinding in objWord.KeyBindings
- Wscript.Echo "Key Binding:", objBinding
- Next
-
- For Each objFont in objWord.LandscapeFontNames
- Wscript.Echo "Landscape Font Name:", objFont
- Next
-
- Wscript.Echo "Language", objWord.Language
-
- For Each objLanguage in objWord.Languages
- Wscript.Echo "Language:", objLanguage
- Next
-
- Wscript.Echo "Left", objWord.Left
- Wscript.Echo "Mail System:", objWord.MailSystem
- Wscript.Echo "MAPI Available:", objWord.MAPIAvailable
- Wscript.Echo "Math Coprocessor Available:", objWord.MathCoprocessorAvailable
- Wscript.Echo "Mouse Available:", objWord.MouseAvailable
- Wscript.Echo "Name:", objWord.Name
- Wscript.Echo "Normal Template:", objWord.NormalTemplate
- Wscript.Echo "Num Lock:", objWord.NumLock
- Wscript.Echo "Parent:", objWord.Parent
- Wscript.Echo "Path:", objWord.Path
- Wscript.Echo "Path Separator:", objWord.PathSeparator
- Wscript.Echo "Print Preview:", objWord.PrintPreview
-
- For Each objFile in objWord.RecentFiles
- Wscript.Echo "Recent File:", objFile
- Next
-
- Wscript.Echo "Screen Updating:", objWord.ScreenUpdating
- Wscript.Echo "Show Visual Basic Editor:", objWord.ShowVisualBasicEditor
- Wscript.Echo "Special Mode:", objWord.SpecialMode
- Wscript.Echo "Startup Path:", objWord.StartupPath
-
- For Each objTask in objWord.Tasks
- Wscript.Echo "Task:", objTask
- Next
-
- For Each objTemplate in objWord.Templates
- Wscript.Echo "Template:", objTemplate
- Next
-
- Wscript.Echo "Top:", objWord.Top
- Wscript.Echo "Usable Height:", objWord.UsableHeight
- Wscript.Echo "Usable Width:", objWord.UsableWidth
- Wscript.Echo "User Address:", objWord.UserAddress
- Wscript.Echo "User Control:", objWord.UserControl
- Wscript.Echo "User Initials:", objWord.UserInitials
- Wscript.Echo "User Name:", objWord.UserName
- Wscript.Echo "Version:", objWord.Version
- Wscript.Echo "Visible:", objWord.Visible
- Wscript.Echo "Width:", objWord.Width
-
- For Each objWindow in objWord.Windows
- Wscript.Echo "Window:", objWindow
- Next
-
- Wscript.Echo "Window State:", objWord.WindowState
- objWord.Quit
-
- Modify Bookmark Text in a Word Document
-
- Demonstration script that changes the text of two different bookmarks in an existing Microsoft Word document.
- Set objWord = CreateObject("Word.Application")
- objWord.Caption = "Test Caption"
- objWord.Visible = True
-
- Set objDoc = objWord.Documents.Open("c:\scripts\word\bookmarkdoc.doc")
-
- Set objRange = objDoc.Bookmarks("NameBookmark").Range
- objRange.Text = "Bob"
-
- Set objRange = objDoc.Bookmarks("AddressBookmark").Range
- objRange.Text = "999"
-
- Open and Print a Word Document
-
- Demonstration script that opens and prints and existing Microsoft Word document.
- Set objWord = CreateObject("Word.Application")
- Set objDoc = objWord.Documents.Open("c:\scripts\inventory.doc")
-
- objDoc.PrintOut()
- objWord.Quit
-
- Read a Bookmark in a Word Document
-
- Demonstration script that retrieves the values of two different Microsoft Word bookmarks.
- Set objWord = CreateObject("Word.Application")
- Set objDoc = objWord.Documents.Open("c:\scripts\word\bookmarkdoc.doc")
- Set objRange = objDoc.Bookmarks("NameBookmark").Range
-
- Wscript.Echo objRange.Text
-
- Set objRange = objDoc.Bookmarks("AddressBookmark").Range
- Wscript.Echo objRange.Text
-
- objWord.Quit
-
- Use Word to Search for Files
-
- Demonstration script that uses Microsoft Word to locate all the .mp3 files stored on drive C of the local computer.
- Set objWord = CreateObject("Word.Application")
- Set objDoc = objWord.Documents.Add()
-
- objWord.FileSearch.FileName = "*.mp3"
- objWord.FileSearch.LookIn = "C:\"
- objWord.FileSearch.SearchSubfolders = True
- objWord.FileSearch.Execute
-
- For Each objFile in objWord.FileSearch.FoundFiles
- Wscript.Echo objFile
- Next
-
- objWord.Quit
复制代码
转自:http://www.activexperts.com/acti ... ipts/msoffice/word/
作者: HAT 时间: 2009-9-22 23:56
【Excel】- Add Data to a Spreadsheet Cell
-
- Demonstration script that adds the words "Test Value" to cell 1,1 in a new spreadsheet.
-
- Set objExcel = CreateObject("Excel.Application")
-
- objExcel.Visible = True
- objExcel.Workbooks.Add
- objExcel.Cells(1, 1).Value = "Test value"
-
- Add Formatted Data to a Spreadsheet
-
- Demonstration script that adds the words "test value" to a new spreadsheet, then formats the cell containing the value.
-
- Set objExcel = CreateObject("Excel.Application")
-
- objExcel.Visible = True
- objExcel.Workbooks.Add
- objExcel.Cells(1, 1).Value = "Test value"
- objExcel.Cells(1, 1).Font.Bold = TRUE
- objExcel.Cells(1, 1).Font.Size = 24
- objExcel.Cells(1, 1).Font.ColorIndex = 3
-
- Create User Accounts Based on Information in a Spreadsheet
-
- Demonstration script that creates new Active Directory user accounts based on information stored in an Excel spreadsheet.
-
- Set objExcel = CreateObject("Excel.Application")
- Set objWorkbook = objExcel.Workbooks.Open _
- ("C:\Scripts\New_users.xls")
-
- intRow = 2
-
- Do Until objExcel.Cells(intRow,1).Value = ""
- Set objOU = GetObject("ou=Finance, dc=fabrikam, dc=com")
- Set objUser = objOU.Create _
- ("User", "cn=" & objExcel.Cells(intRow, 1).Value)
- objUser.sAMAccountName = objExcel.Cells(intRow, 2).Value
- objUser.GivenName = objExcel.Cells(intRow, 3).Value
- objUser.SN = objExcel.Cells(intRow, 4).Value
- objUser.AccountDisabled = FALSE
- objUser.SetInfo
- intRow = intRow + 1
- Loop
-
- objExcel.Quit
-
- Format a Range of Cells
-
- Demonstration script that adds data to four different cells in a spreadsheet, then uses the Range object to format multiple cells at the same time.
-
- Set objExcel = CreateObject("Excel.Application")
-
- objExcel.Visible = True
- objExcel.Workbooks.Add
-
- objExcel.Cells(1, 1).Value = "Name"
- objExcel.Cells(1, 1).Font.Bold = TRUE
- objExcel.Cells(1, 1).Interior.ColorIndex = 30
- objExcel.Cells(1, 1).Font.ColorIndex = 2
- objExcel.Cells(2, 1).Value = "Test value 1"
- objExcel.Cells(3, 1).Value = "Test value 2"
- objExcel.Cells(4, 1).Value = "Tets value 3"
- objExcel.Cells(5, 1).Value = "Test value 4"
-
- Set objRange = objExcel.Range("A1","A5")
- objRange.Font.Size = 14
-
- Set objRange = objExcel.Range("A2","A5")
- objRange.Interior.ColorIndex = 36
-
- Set objRange = objExcel.ActiveCell.EntireColumn
- objRange.AutoFit()
-
- List Active Directory Data in a Spreadsheet
-
- Demonstration script that retrieves data from Active Directory and then displays that data in an Excel spreadsheet.
-
- Const ADS_SCOPE_SUBTREE = 2
-
- Set objExcel = CreateObject("Excel.Application")
-
- objExcel.Visible = True
- objExcel.Workbooks.Add
-
- objExcel.Cells(1, 1).Value = "Last name"
- objExcel.Cells(1, 2).Value = "First name"
- objExcel.Cells(1, 3).Value = "Department"
- objExcel.Cells(1, 4).Value = "Phone number"
-
- Set objConnection = CreateObject("ADODB.Connection")
- Set objCommand = CreateObject("ADODB.Command")
- objConnection.Provider = "ADsDSOObject"
- objConnection.Open "Active Directory Provider"
-
- Set objCommand.ActiveConnection = objConnection
- objCommand.Properties("Page Size") = 100
- objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
- objCommand.CommandText = _
- "SELECT givenName, SN, department, telephoneNumber FROM " _
- & "'LDAP://dc=fabrikam,dc=microsoft,dc=com' WHERE " _
- & "objectCategory='user'"
- Set objRecordSet = objCommand.Execute
- objRecordSet.MoveFirst
- x = 2
-
- Do Until objRecordSet.EOF
- objExcel.Cells(x, 1).Value = _
- objRecordSet.Fields("SN").Value
- objExcel.Cells(x, 2).Value = _
- objRecordSet.Fields("givenName").Value
- objExcel.Cells(x, 3).Value = _
- objRecordSet.Fields("department").Value
- objExcel.Cells(x, 4).Value = _
- objRecordSet.Fields("telephoneNumber").Value
- x = x + 1
- objRecordSet.MoveNext
- Loop
-
- Set objRange = objExcel.Range("A1")
- objRange.Activate
-
- Set objRange = objExcel.ActiveCell.EntireColumn
- objRange.Autofit()
-
- Set objRange = objExcel.Range("B1")
- objRange.Activate
- Set objRange = objExcel.ActiveCell.EntireColumn
- objRange.Autofit()
-
- Set objRange = objExcel.Range("C1")
- objRange.Activate
-
- Set objRange = objExcel.ActiveCell.EntireColumn
- objRange.Autofit()
-
- Set objRange = objExcel.Range("D1")
- objRange.Activate
-
- Set objRange = objExcel.ActiveCell.EntireColumn
- objRange.Autofit()
-
- Set objRange = objExcel.Range("A1").SpecialCells(11)
- Set objRange2 = objExcel.Range("C1")
- Set objRange3 = objExcel.Range("A1")
-
- List Excel Color Values
-
- Demonstration script that displays the various colors -- and their related color index -- available when programmatically controlling Microsoft Excel.
-
- Set objExcel = CreateObject("Excel.Application")
-
- objExcel.Visible = True
- objExcel.Workbooks.Add
-
- For i = 1 to 56
- objExcel.Cells(i, 1).Value = i
- objExcel.Cells(i, 1).Interior.ColorIndex = i
- Next
-
- List Service Data in a Spreadsheet
-
- Demonstration script that retrieves information about each service running on a computer, and then displays that data in an Excel spreadsheet.
-
- Set objExcel = CreateObject("Excel.Application")
- objExcel.Visible = True
- objExcel.Workbooks.Add
-
- x = 1
- strComputer = "."
- Set objWMIService = GetObject _
- ("winmgmts:\\" & strComputer & "\root\cimv2")
- Set colServices = objWMIService.ExecQuery _
- ("Select * From Win32_Service")
-
- For Each objService in colServices
- objExcel.Cells(x, 1) = objService.Name
- objExcel.Cells(x, 2) = objService.State
- x = x + 1
- Next
-
- Open an Excel Spreadsheet
-
- Demonstration script that opens an existing Excel spreadsheet named C:\Scripts\New_users.xls.
-
- Set objExcel = CreateObject("Excel.Application")
- Set objWorkbook = objExcel.Workbooks.Open("C:\Scripts\New_users.xls")
-
- Read an Excel Spreadsheet
-
- Demonstration script that reads the values stored in a spreadsheet named C:\Scripts\New_users.xls.
-
- Set objExcel = CreateObject("Excel.Application")
- Set objWorkbook = objExcel.Workbooks.Open _
- ("C:\Scripts\New_users.xls")
-
- intRow = 2
-
- Do Until objExcel.Cells(intRow,1).Value = ""
- Wscript.Echo "CN: " & objExcel.Cells(intRow, 1).Value
- Wscript.Echo "sAMAccountName: " & objExcel.Cells(intRow, 2).Value
- Wscript.Echo "GivenName: " & objExcel.Cells(intRow, 3).Value
- Wscript.Echo "LastName: " & objExcel.Cells(intRow, 4).Value
- intRow = intRow + 1
- Loop
-
- objExcel.Quit
-
- VBS控制Excel的一些常见方法:
- (一) 使用动态创建的方法
- 首先创建 Excel 对象,使用ComObj:
- oExcel = CreateObject( "Excel.Application” )
-
- 1) 显示当前窗口:
- oExcel.Visible = True
-
- 2) 更改 Excel 标题栏:
- oExcel.Caption = "应用程序调用 Microsoft Excel”
-
- 3) 添加新工作簿:
- oExcel.WorkBooks.Add
-
- 4) 打开已存在的工作簿:
- oExcel.WorkBooks.Open( "C:\Excel\Demo.xls” )
-
- 5) 设置第2个工作表为活动工作表:
- oExcel.WorkSheets(2).Activate
- 或
- oExcel.WorksSheets( "Sheet2" ).Activate
-
- 6) 给单元格赋值:
- oExcel.Cells(1,4).Value = "第一行第四列”
-
- 7) 设置指定列的宽度(单位:字符个数),以第一列为例:
- oExcel.ActiveSheet.Columns(1).ColumnsWidth = 5
-
- 8)设置指定行的高度(单位:磅)(1磅=0.035厘米),以第二行为例:
- oExcel.ActiveSheet.Rows(2).RowHeight = 1/0.035 ‘ 1厘米
-
- 9) 在第8行之前插入分页符:
- oExcel.WorkSheets(1).Rows(8).PageBreak = 1
-
- 10) 在第8列之前删除分页符:
- oExcel.ActiveSheet.Columns(4).PageBreak = 0
-
- 11) 指定边框线宽度:
- oExcel.ActiveSheet.Range( "B3:D4" ).Borders(2).Weight = 3
- 1-左 2-右 3-顶 4-底 5-斜( \ ) 6-斜( / )
-
- 12) 清除第一行第四列单元格公式:
- oExcel.ActiveSheet.Cells(1,4).ClearContents
-
- 13) 设置第一行字体属性:
- oExcel.ActiveSheet.Rows(1).Font.Name = "隶书”
- oExcel.ActiveSheet.Rows(1).Font.Color = clBlue
- oExcel.ActiveSheet.Rows(1).Font.Bold = True
- oExcel.ActiveSheet.Rows(1).Font.UnderLine = True
-
- 14) 进行页面设置:
- a.页眉:
- oExcel.ActiveSheet.PageSetup.CenterHeader = "报表演示”
- b.页脚:
- oExcel.ActiveSheet.PageSetup.CenterFooter = "第&P页”
- c.页眉到顶端边距2cm:
- oExcel.ActiveSheet.PageSetup.HeaderMargin = 2/0.035
- d.页脚到底端边距3cm:
- oExcel.ActiveSheet.PageSetup.HeaderMargin = 3/0.035
- e.顶边距2cm:
- oExcel.ActiveSheet.PageSetup.TopMargin = 2/0.035
- f.底边距2cm:
- oExcel.ActiveSheet.PageSetup.BottomMargin = 2/0.035
- g.左边距2cm:
- oExcel.ActiveSheet.PageSetup.LeftMargin = 2/0.035
- h.右边距2cm:
- oExcel.ActiveSheet.PageSetup.RightMargin = 2/0.035
- i.页面水平居中:
- oExcel.ActiveSheet.PageSetup.CenterHorizontally = 2/0.035
- j.页面垂直居中:
- oExcel.ActiveSheet.PageSetup.CenterVertically = 2/0.035
- k.打印单元格网线:
- oExcel.ActiveSheet.PageSetup.PrintGridLines = True
-
- 15) 拷贝操作:
-
- a.拷贝整个工作表:
- oExcel.ActiveSheet.Used.Range.Copy
-
- b.拷贝指定区域:
- oExcel.ActiveSheet.Range( "A1:E2" ).Copy
-
- c.从A1位置开始粘贴:
- oExcel.ActiveSheet.Range.( "A1" ).PasteSpecial
-
- d.从文件尾部开始粘贴:
- oExcel.ActiveSheet.Range.PasteSpecial
-
- 16) 插入一行或一列:
- a. oExcel.ActiveSheet.Rows(2).Insert
- b. oExcel.ActiveSheet.Columns(1).Insert
-
- 17) 删除一行或一列:
- a. oExcel.ActiveSheet.Rows(2).Delete
- b. oExcel.ActiveSheet.Columns(1).Delete
-
- 18) 打印预览工作表:
- oExcel.ActiveSheet.PrintPreview
-
- 19) 打印输出工作表:
- oExcel.ActiveSheet.PrintOut
-
- 20) 工作表保存:
- if not oExcel.ActiveWorkBook.Saved then
- oExcel.ActiveSheet.PrintPreview
-
- 21) 工作表另存为:
- oExcel.SaveAs( "C:\Excel\Demo1.xls” )
-
- 22) 放弃存盘:
- oExcel.ActiveWorkBook.Saved = True
-
- 23) 关闭工作簿:
- oExcel.WorkBooks.Close
-
- 24) 退出 Excel:
- oExcel.Quit
-
- (二) 使用VBS 控制Excle二维图
-
- 1)选择当第一个工作薄第一个工作表
- set oSheet=oExcel.Workbooks(1).Worksheets(1)
-
- 2)增加一个二维图
- achart=oSheet.chartobjects.add(100,100,200,200)
-
- 3)选择二维图的形态
- achart.chart.charttype=4
-
- 4)给二维图赋值
- set series=achart.chart.seriescollection
- range=”sheet1!r2c3:r3c9"
- series.add range,true
-
- 5)加上二维图的标题
- achart.Chart.HasTitle=True
- achart.Chart.ChartTitle.Characters.Text=” Excle二维图”
-
- 6)改变二维图的标题字体大小
- achart.Chart.ChartTitle.Font.size=18
-
- 7)给二维图加下标说明
- achart.Chart.Axes(xlCategory, xlPrimary).HasTitle = True
- achart.Chart.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "下标说明”
-
- 8)给二维图加左标说明
- achart.Chart.Axes(xlValue, xlPrimary).HasTitle = True
- achart.Chart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "左标说明”
-
- 9)给二维图加右标说明
- achart.Chart.Axes(xlValue, xlSecondary).HasTitle = True
- achart.Chart.Axes(xlValue, xlSecondary).AxisTitle.Characters.Text = "右标说明”
-
- 10)改变二维图的显示区大小
- achart.Chart.PlotArea.Left = 5
- achart.Chart.PlotArea.Width = 223
- achart.Chart.PlotArea.Height = 108
复制代码
转自:http://www.activexperts.com/acti ... pts/msoffice/excel/
作者: HAT 时间: 2009-9-22 23:56
【PowerPoint】
转自:。。。。。。
作者: HAT 时间: 2009-9-22 23:58
【Access】
转自:。。。。。。
作者: 5566ljlj 时间: 2010-1-9 21:46
对我很有帮助,谢谢!
作者: oneuu 时间: 2010-1-16 10:02
还好。。。。呵呵。。。。
作者: lm180180 时间: 2011-3-2 10:32
谢谢提供........!
作者: zhanglei1371 时间: 2014-5-11 11:53
好繁琐的东西!
作者: yangd2014 时间: 2014-12-25 16:10
学习了~~~~~~~~
作者: txljb 时间: 2015-4-10 15:31
学习了~~~~~~~~
作者: zrc20d 时间: 2015-12-6 09:44
好东西,多谢分享!
作者: 峰回路转 时间: 2017-6-19 01:44
我就看看,反正我也看不懂
作者: 霖僡 时间: 2018-5-28 22:35
谢谢 学习了一大把
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |