第一范文网 - 专业文章范例文档资料分享平台

Word VBA(2) (3)

来源:用户分享 时间:2020-06-09 本文由雾涣风月 分享 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

Use the DefaultFilePath property to determine the location of user or workgroup templates (that is, the folder where you want to store these templates). The following example displays the user template folder from the File Locations tab in the Options dialog box (Tools menu).

MsgBox Options.DefaultFilePath(wdUserTemplatePath)

Templates collection (Application object)

属性:Count, Parent, Application, Creater 方法:Item

Template object

属性:

Name,Path,FullName, (FullName= Path & “\\” & Name) Type,Saved,

BuiltInDocumentProperties(DocumentProperty), CustomaerDocumentProperties(DocumentProperty), FarEastLineBreakLanguage,FarEastLineLevel, KerningByAlgorithm, JustificationMode, NoLineBreakBefore/After, ListTemplates, VBProject,

(Document object也有以上属性) AutoTextEntries(AutoTextEntry),

LanguageID,LanguageIDFarEast,NoProofing, Parent, Application, Creater 方法:OpenAsDocument,Save

NoProofing属性: True if the spelling and grammar checker ignores documents based on this template. Read/write Long.

Bookmarks

Bookmarks Collection

A collection of Bookmark objects that represent the bookmarks in the specified selection, range, or document.

属性:Count,ShowHidden,DefaultSorting, Creater,Parent,Application 方法:Item,Exists,Add

Use the Bookmarks property of selection/range/ documentto return the Bookmarks collection. Use the Bookmarks(index) to return a single Bookmark object: Object.Bookmarks(index)

where index is the bookmark name or index number. You must exactly match the spelling (but not necessarily the capitalization) of the bookmark name. The index number represents the

position of the bookmark in the Selection or Range object. For the Document object, the index number represents the position of the bookmark in the alphabetic list of bookmarks in the Bookmarks dialog box (click Name to sort the list of bookmarks alphabetically).

MsgBox ActiveDocument.Bookmarks(2).Name

MsgBox ActiveDocument.Bookmarks(“Bookmark1”).Range.Text

The following example ensures that the bookmark named \before selecting the bookmark.

If ActiveDocument.Bookmarks.Exists(\ ActiveDocument.Bookmarks(\End If

Use the Add method to set a bookmark for a range in a document. The following example marks the selection by adding a bookmark named \

ActiveDocument.Bookmarks.Add Name:=\

Remarks

The ShowHidden property effects the number of elements in the Bookmarks collection. The Hidden bookmarks are included in the Bookmarks collection only if ShowHidden is True.

Bookmark Object

Use the BookmarkID property with a range or selection object to return the index number of the Bookmark object in the Bookmarks collection. The following example displays the index number of the bookmark named \

MsgBox ActiveDocument.Bookmarks(\

You can use predefined bookmarks with the Bookmarks property. The following example sets the bookmark named \

ActiveDocument.Bookmarks(\

Predefined Bookmarks

Word sets and automatically updates a number of reserved bookmarks. You can use these

predefined bookmarks just as you use the ones that you place in documents, except that you don't have to set them and they are not listed on the Go To tab in the Find and Replace dialog box (Edit menu).

The following table describes the predefined bookmarks available in Word. BOOKMARK \\Sel \\PrevSel1 DESCRIPTION Current selection or the insertion point. Most recent selection where editing occurred; going to this bookmark is equivalent to running the GoBack method once. Second most recent selection where editing occurred; going to this bookmark is equivalent to running the GoBack method twice. Start of the current selection. End of the current selection. Current line or the first line of the current selection. If the insertion point is at the end of a line that is not the last line in the paragraph, \\PrevSel2 \\StartOfSel \\EndOfSel \\Line the bookmark includes the entire next line. \\Char Current character, which is the character following the insertion point if there is no selection, or the first character of the selection. Current paragraph, which is the paragraph containing the insertion point or, if more than one paragraph is selected, the first paragraph of the selection. Note that if the insertion point or selection is in the last paragraph of the document, the \paragraph mark. Current section, including the break at the end of the section, if any. The current section contains the insertion point or selection. If the selection contains more than one section, the \first section in the selection. Entire contents of the active document, with the exception of the final paragraph mark. Current page, including the break at the end of the page, if any. The current page contains the insertion point. If the current selection contains more than one page, the \selection. Note that if the insertion point or selection is in the last page of the document, the \paragraph mark. Beginning of the document. End of the document. Current cell in a table, which is the cell containing the insertion point. If one or more cells of a table are included in the current selection, the \ Current table, which is the table containing the insertion point or selection. If the selection includes more than one table, the \bookmark is the entire first table of the selection, even if the entire table is not selected. \\Para \\Section \\Doc \\Page \\StartOfDoc \\EndOfDoc \\Cell \\Table \\HeadingLevel The heading that contains the insertion point or selection, plus any subordinate headings and text. If the current selection is body text, the \and text subordinate to that heading.

Bookmark Object

属性:Name,Range,Empty,Column,Start,End,StoryType, Creator,Parent,Application 方法:Select, Cope, Delete

Start Property (Selection, Range, and Bookmark objects)

Returns or sets the starting character position of a selection, range, or bookmark. Read/write Long.

Note If this property is set to a value larger than that of the End property, the End property is set to the same value as that of Start property.

Selection, Range, and Bookmark objects have starting and ending character positions. The starting position refers to the character position closest to the beginning of the story. This property returns the starting character position relative to the beginning of the story. The main text story (wdMainTextStory) begins with character position 0 (zero). You can change the size of a selection, range, or bookmark by setting this property.

End Property (Selection, Range, and Bookmark objects) Read/write Long.

This property returns the ending character position relative to the beginning of the story.

You can change the size of a selection, range, or bookmark by setting Start and End property.

StoryType Property (Selection, Range, and Bookmark objects)

Read-only Long. 该Range,Selection或Bookmark 所在的Story的类型。Can be one of the following WdStoryType constants:

wdCommentsStory, wdEndnotesStory, wdEvenPagesFooterStory, wdEvenPagesHeaderStory, wdFirstPageFooterStory, wdFirstPageHeaderStory, wdFootnotesStory, wdMainTextStory, wdPrimaryFooterStory, wdPrimaryHeaderStory, or wdTextFrameStory.

StoryLength Property (Selection and Range objects)

Read-only Long. 该range or selection 所在的Story(如wdMainTextStory)的长度(字符数)

Column Property (Bookmark and Cell objects)

Bookmark object: Read-only Boolean.

True if the specified bookmark is a table column. Cell object: Read-only, object.

Returns a Column object that represents the table column containing the specified cell.

Empty Property ( Bookmark objects )

Read-only Boolean.

True if the specified bookmark is empty. An empty bookmark marks a location (a collapsed selection); it doesn't mark any text.

Note An error occurs if the specified bookmark doesn't exist. Use the Exists method of Bookmarks collection to determine whether a given bookmark exists:

Range Property ( for many objects)

Read-only.

Returns a Range object that represents the portion of a document that's contained in the specified bookmark object.

If ActiveDocument.Bookmarks.Exists(\…

自注:更新Bookmark下的Text

如果Bookmark是一插入点,则以下语句是在此插入 “Smith”,其后text右移,该Bookmark的位置保持不变:

ActiveDocument.Bookmarks(\“Smith”

但如果Bookmark是一范围,上述语句更新原范围中的text后,该Bookmark也被Deleted了。例如 执行该语句,把EmpName bookmark下的Text换为“Smith”(实际是先删后插,不管原Text有多长。即Range的新范围(End位置)按新Text长度确定。对任何Range,写其Text属性总是这样),但是Bookmark “EmpName” 也删去了。为保持原Bookmark ,只能再加上:

Dim r1 As Range

Set r1 = ActiveDocument.Bookmarks(\ ?保留Range object

r1.Text = CmbEmpID.Value ? 更新text,r1范围大小可能变了 ActiveDocument.Bookmarks.Add Name:=\ ?恢复Bookmark代表新范围

当然,可用Bookmarks(\方法插入text,此时Bookmark不会被删,但原来的text仍在。可以在插入后删去原Text:

Dim r As Range, N As Integer, i As Integer

If ActiveDocument.Bookmarks.Exists(\ ActiveDocument.Bookmarks(\Select

‘Options.ReplaceSelection = False 不影响collapse后插入 N = Selection.Characters.Count

Selection.Collapse wdCollapseStart Selection.TypeText \

Selection.Delete wdCharacter, N End If

Tables

Tables Collection Object

A collection of Table objects that represent the tables in a selection, range, or document.

属性:Count,NestingLevel, Creater,Parent,Application 方法:Item, Add

Use the Tables property to return the Tables collection. The following example applies a border around each of the tables in the active document.

For Each aTable In ActiveDocument.Tables

aTable.Borders.OutsideLineStyle = wdLineStyleSingle aTable.Borders.OutsideLineWidth = wdLineWidth025pt aTable.Borders.InsideLineStyle = wdLineStyleNone Next aTable

Use the Add method to add a table at the specified range. The following example adds a 3x4 table at the beginning of the active document.

Set myRange = ActiveDocument.Range(Start:=0, End:=0)

ActiveDocument.Tables.Add Range:=myRange, NumRows:=3, NumColumns:=4

Use Tables(index), where index is the index number(Table无Name), to return a single Table object. The index number represents the position of the table in the selection, range, or document. The following example converts the first table in the active document to text.

ActiveDocument.Tables(1).ConvertToText Separator:=wdSeparateByTabs

Remarks

The Count property for this collection in a document returns the number of items in the main story only. To count items in other stories use the collection with the Range object.

搜索“diyifanwen.net”或“第一范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,第一范文网,提供最新IT计算机Word VBA(2) (3)全文阅读和word下载服务。

Word VBA(2) (3).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/wenku/1078297.html(转载请注明文章来源)
热门推荐
Copyright © 2018-2022 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top