注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

widebright的个人空间

// 编程和生活

 
 
 

日志

 
 

VBscript 控制word的例子  

2008-11-21 12:22:57|  分类: 程序设计 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

原文地址:http://www.activexperts.com/activmonitor/windowsmanagement/scripts/msoffice/word/

Microsoft Word Scripts

Add Formatted Text to a Word Document
Add a Formatted Table to a Word Document
Apply a Style to a Table in a Word Document
Add a Table to a Word Document
Append Text to a Word Document
Create a New Word Document
Create and Save a Word Document
Display Service Information in a Word Document
List Microsoft Word Properties
Modify Bookmark Text in a Word Document
Open and Print a Word Document
Read a Bookmark in a Word Document
Use Word to Search for Files


You can use any of the VBScript programs below in ActiveXperts Network Monitor. Click here for an explanation about how to include scripts in ActiveXperts Network Monitor.

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 = TrueSet objDoc = objWord.Documents.Add()Set objSelection = objWord.SelectionobjSelection.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 = TrueSet objDoc = objWord.Documents.Add()Set objRange = objDoc.Range()objDoc.Tables.Add objRange,1,3Set objTable = objDoc.Tables(1)x=1strComputer = "."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 + 1Next 

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 = TrueSet objDoc = objWord.Documents.Add()Set objRange = objDoc.Range()objDoc.Tables.Add objRange,1,3Set objTable = objDoc.Tables(1)objTable.Range.Font.Size = 10objTable.Range.Style = "Table Contemporary"x=2objTable.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 + 1Next 

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 = TrueSet objDoc = objWord.Documents.Add()Set objRange = objDoc.Range()objDoc.Tables.Add objRange,1,3Set objTable = objDoc.Tables(1)x=1strComputer = "."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 + 1Next 

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 = 6Const MOVE_SELECTION = 0Set objWord = CreateObject("Word.Application")objWord.Visible = TrueSet objDoc = objWord.Documents.Open("c:\scripts\word\testdoc.doc")Set objSelection = objWord.SelectionobjSelection.EndKey END_OF_STORY, MOVE_SELECTIONobjSelection.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 = TrueSet 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 = TrueSet objDoc = objWord.Documents.Add()Set objSelection = objWord.SelectionobjSelection.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()NextobjDoc.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 = TrueSet objDoc = objWord.Documents.Add()Set objSelection = objWord.SelectionobjSelection.TypeText "Services Report"objSelection.TypeParagraph()objSelection.TypeText "" & NowobjSelection.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 NextSet objWord = CreateObject("Word.Application")Wscript.Echo "Active Printer:", objWord.ActivePrinterFor Each objAddIn in objWord.AddIns    Wscript.Echo "AddIn: ", objAddInNextWscript.Echo "Application:", objWord.ApplicationWscript.Echo "Assistant:", objWord.AssistantFor Each objCaption in objWord.AutoCaptions    Wscript.Echo "AutoCaptions:", objCaptionNextWscript.Echo "Automation Security:", objWord.AutomationSecurityWscript.Echo "Background Printing Status:", objWord.BackgroundPrintingStatusWscript.Echo "Background Saving Status:", objWord.BackgroundSavingStatusWscript.Echo "Browse Extra File Type:", objWord.BrowseExtraFileTypesWscript.Echo "Build:", objWord.BuildWscript.Echo "Caps Lock:", objWord.CapsLockWscript.Echo "Caption:", objWord.CaptionFor Each objLabel in objWord.CaptionLabels    Wscript.Echo "Caption Label:", objLabelNextWscript.Echo "Check Language:", objWord.CheckLanguageFor Each objAddIn in objWord.COMAddIns    Wscript.Echo "COM AddIn:", objAddInNextWscript.Echo "Creator:", objWord.CreatorFor Each objDictionary in objWord.CustomDictionaries    Wscript.Echo "Custom Dictionary:", objDictionaryNextWscript.Echo "Customization Context:", objWord.CustomizationContextWscript.Echo "Default Legal Blackline:", objWord.DefaultLegalBlacklineWscript.Echo "Default Save Format:", objWord.DefaultSaveFormatWscript.Echo "Default Table Separator:", objWord.DefaultTableSeparatorFor Each objDialog in objWord.Dialogs    Wscript.Echo "Dialog:", objDialogNextWscript.Echo "Display Alerts:", objWord.DisplayAlertsWscript.Echo "Display Recent Files:", objWord.DisplayRecentFilesWscript.Echo "Display Screen Tips:", objWord.DisplayScreenTipsWscript.Echo "Display Scroll Bars:", objWord.DisplayScrollBarsFor Each objDocument in objWord.Documents    Wscript.Echo "Document:", objDocumentNextWscript.Echo "Email Template:", objWord.EmailTemplateWscript.Echo "Enable Cancel Key:", objWord.EnableCancelKeyWscript.Echo "Feature Install:", objWord.FeatureInstallFor Each objConverter in objWord.FileConverters    Wscript.Echo "File Converter:", objConverterNextWscript.Echo "Focus In MailHeader:", objWord.FocusInMailHeaderFor Each objFont in objWord.FontNames    Wscript.Echo "Font Name:", objFontNextWscript.Echo "Height", objWord.HeightFor Each objBinding in objWord.KeyBindings    Wscript.Echo "Key Binding:", objBindingNextFor Each objFont in objWord.LandscapeFontNames    Wscript.Echo "Landscape Font Name:", objFontNextWscript.Echo "Language", objWord.LanguageFor Each objLanguage in objWord.Languages    Wscript.Echo "Language:", objLanguageNextWscript.Echo "Left", objWord.LeftWscript.Echo "Mail System:", objWord.MailSystemWscript.Echo "MAPI Available:", objWord.MAPIAvailableWscript.Echo "Math Coprocessor Available:", objWord.MathCoprocessorAvailableWscript.Echo "Mouse Available:", objWord.MouseAvailableWscript.Echo "Name:", objWord.NameWscript.Echo "Normal Template:", objWord.NormalTemplateWscript.Echo "Num Lock:", objWord.NumLockWscript.Echo "Parent:", objWord.ParentWscript.Echo "Path:", objWord.PathWscript.Echo "Path Separator:", objWord.PathSeparatorWscript.Echo "Print Preview:", objWord.PrintPreviewFor Each objFile in objWord.RecentFiles    Wscript.Echo "Recent File:", objFileNextWscript.Echo "Screen Updating:", objWord.ScreenUpdatingWscript.Echo "Show Visual Basic Editor:", objWord.ShowVisualBasicEditorWscript.Echo "Special Mode:", objWord.SpecialModeWscript.Echo "Startup Path:", objWord.StartupPathFor Each objTask in objWord.Tasks    Wscript.Echo "Task:", objTaskNextFor Each objTemplate in objWord.Templates    Wscript.Echo "Template:", objTemplateNextWscript.Echo "Top:", objWord.TopWscript.Echo "Usable Height:", objWord.UsableHeightWscript.Echo "Usable Width:", objWord.UsableWidthWscript.Echo "User Address:", objWord.UserAddressWscript.Echo "User Control:", objWord.UserControlWscript.Echo "User Initials:", objWord.UserInitialsWscript.Echo "User Name:", objWord.UserNameWscript.Echo "Version:", objWord.VersionWscript.Echo "Visible:", objWord.VisibleWscript.Echo "Width:", objWord.WidthFor Each objWindow in objWord.Windows    Wscript.Echo "Window:", objWindowNextWscript.Echo "Window State:", objWord.WindowStateobjWord.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 = TrueSet objDoc = objWord.Documents.Open("c:\scripts\word\bookmarkdoc.doc")Set objRange = objDoc.Bookmarks("NameBookmark").RangeobjRange.Text = "Bob"Set objRange = objDoc.Bookmarks("AddressBookmark").RangeobjRange.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").RangeWscript.Echo objRange.Text Set objRange = objDoc.Bookmarks("AddressBookmark").RangeWscript.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 = TrueobjWord.FileSearch.ExecuteFor Each objFile in objWord.FileSearch.FoundFiles    Wscript.Echo objFileNextobjWord.Quit
  评论这张
 
阅读(1152)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017