word 宏 实例
這個宏的基本功能如下:對一個文件中的每行都進行轉換。
轉換前:1?? ?1001??? aaa ?? aaa ?? 0
轉換后:1,1001,"aaa","aaa",0
Sub convert()
??? Selection.find.ClearFormatting
??? Selection.find.Replacement.ClearFormatting
??? With Selection.find
??????? .Text = "^t"
??????? .Replacement.Text = ","
??????? .Forward = True
??????? .Wrap = wdFindContinue
??????? .Format = False
??????? .MatchCase = False
??????? .MatchWholeWord = False
??????? .MatchByte = True
??????? .MatchWildcards = False
??????? .MatchSoundsLike = False
??????? .MatchAllWordForms = False
??? End With
??? Selection.find.Execute replace:=wdReplaceAll
??? Selection.HomeKey Unit:=wdStory
??? For i = 1 To ActiveDocument.Paragraphs.Count
??????? Selection.HomeKey Unit:=wdLine
??????? Selection.find.ClearFormatting
??????? Selection.find.Replacement.ClearFormatting
??????? With Selection.find
??????????? .Text = ","
??????????? .Replacement.Text = ""
??????????? .Forward = True
??????????? .Wrap = wdFindContinue
??????? End With
??????? Selection.find.Execute
??????? Selection.find.Execute
??????? With Selection.find
??????????? .Text = ","
??????????? .Replacement.Text = ","""
??????????? .Forward = True
??????????? .Wrap = wdFindContinue
??????? End With
??????? With Selection
??????? If .find.Forward = True Then
??????????? .Collapse Direction:=wdCollapseStart
??????? Else
??????????? .Collapse Direction:=wdCollapseEnd
??????? End If
??????? .find.Execute replace:=wdReplaceOne
??????? If .find.Forward = True Then
??????????? .Collapse Direction:=wdCollapseEnd
??????? Else
??????????? .Collapse Direction:=wdCollapseStart
??????? End If
??????? .find.Execute
??????? End With
??????? With Selection.find
??????????? .Text = ","
??????????? .Replacement.Text = ""","""
??????????? .Forward = True
??????????? .Wrap = wdFindContinue
??????? End With
??????? With Selection
??????? If .find.Forward = True Then
??????????? .Collapse Direction:=wdCollapseStart
??????? Else
??????????? .Collapse Direction:=wdCollapseEnd
??????? End If
??????? .find.Execute replace:=wdReplaceOne
??????? If .find.Forward = True Then
??????????? .Collapse Direction:=wdCollapseEnd
??????? Else
??????????? .Collapse Direction:=wdCollapseStart
??????? End If
??????? .find.Execute
??????? End With
??????? With Selection.find
??????????? .Text = ","
??????????? .Replacement.Text = ""","
??????????? .Forward = True
??????????? .Wrap = wdFindContinue
??????? End With
??????? With Selection
??????? If .find.Forward = True Then
??????????? .Collapse Direction:=wdCollapseStart
??????? Else
??????????? .Collapse Direction:=wdCollapseEnd
??????? End If
??????? .find.Execute replace:=wdReplaceOne
??????? If .find.Forward = True Then
??????????? .Collapse Direction:=wdCollapseEnd
??????? Else
??????????? .Collapse Direction:=wdCollapseStart
??????? End If
??????? .find.Execute
??????? End With
??? Next i
End Sub
參考了以下文檔,以下轉載:
Application對象
- - - - - - - - - - - - - - - - - - - -
(301) Application.ActivePrinter ‘獲取當前打印機
(302) Application.Height '當前應用程序文檔的高度
(303) Application.Width ‘當前應用程序文檔的寬度
(304) Application.Build ‘獲取Word版本號和編譯序號
(305) Application.Caption ‘當前應用程序名
(306) Application.DefaultSaveFormat '返回空字符串,表示Word文檔
(307) Application.DisplayRecentFiles '返回是否顯示最近使用的文檔的狀態
(308) Application.Documents.Count '返回當前打開的文檔數
(309) Application.FontNames.Count ‘返回當前可用的字體數
(310) Application.Left ‘返回當前文檔的水平位置
(311) Application.MacroContainer.FullName '返回當前文檔名,包括所在路徑
(312) Application.NormalTemplate.FullName '返回文檔標準模板名稱及所在位置
(313) Application.RecentFiles.Count '返回最近打開的文檔數目
(314) Application.System.CountryRegion '返回應用程序所在的地區代碼
(315) Application.System.FreeDiskSpace ‘返回應用程序所在磁盤可用空間
(316) Application.System.HorizontalResolution '返回顯示器的水平分辨率
(317) Application.System.VerticalResolution '返回顯示器的垂直分辨率
(318) Application.System.LanguageDesignation '返回系統所使用的語言
(319) Application.System.MathCoprocessorInstalled ‘返回系統是否安裝了數學協處理器
(320) Application.System.OperatingSystem ‘返回當前操作系統名
(321) Application.System.ProcessorType '返回計算機處理器名
(322) Application.System.Version ‘返回操作系統的版本號
(323) Application.Templates.Count '返回應用程序所使用的模板數
(324) Application.UserName '返回應用程序用戶名
(325) Application.Version ‘返回應用程序的版本號
- - - - - - - - - - - - - - - - - - - - - -
Documents/Document對象
- - - - - - - - - - - - - - - - - - - - - -
(326) ActiveDocument.AttachedTemplate.FullName '返回當前文檔采用的模板名及模板所在位置
(327) ActiveDocument.Bookmarks.Count '返回當前文檔中的書簽數
(328) ActiveDocument.Characters.Count '返回當前文檔的字符數
(329) ActiveDocument.CodeName ‘返回當前文檔的代碼名稱
(330) ActiveDocument.Comments.Count ‘返回當前文檔中的評論數
(331) ActiveDocument.Endnotes.Count '返回當前文檔中的尾注數
(332) ActiveDocument.Fields.Count '返回當前文檔中的域數目
(333) ActiveDocument.Footnotes.Count ‘返回當前文檔中的腳注數
(334) ActiveDocument.FullName '返回當前文檔的全名及所在位置
(335) ActiveDocument.HasPassword '當前文檔是否有密碼保護
(336) ActiveDocument.Hyperlinks.Count '返回當前文檔中的鏈接數
(337) ActiveDocument.Indexes.Count '返回當前文檔中的索引數
(338) ActiveDocument.ListParagraphs.Count '返回當前文檔中項目編號或項目符號數
(339) ActiveDocument.ListTemplates.Count '返回當前文檔中使用的列表模板數
(340) ActiveDocument.Paragraphs.Count '返回當前文檔中的段落數
(341) ActiveDocument.Password=XXX '設置打開文件使用的密碼
(342) ActiveDocument.ReadOnly '獲取當前文檔是否為只讀屬性
(343) ActiveDocument.Saved '當前文檔是否被保存
(344) ActiveDocument.Sections.Count '當前文檔中的節數
(345) ActiveDocument.Sentences.Count ‘當前文檔中的語句數
(346) ActiveDocument.Shapes.Count '當前文檔中的形狀數
(347) ActiveDocument.Styles.Count '當前文檔中的樣式數
(348) ActiveDocument.Tables.Count ‘當前文檔中的表格數
(349) ActiveDocument.TablesOfAuthorities.Count ‘返回當前文檔中的引文目錄數
(350) ActiveDocument.TablesOfAuthoritiesCategories.Count ‘返回當前文檔中引文目錄類別數
(351) ActiveDocument.TablesOfContents.Count ‘返回當前文檔中的目錄數
(352) ActiveDocument.TablesOfFigures.Count '返回當前文檔中的圖表目錄數
- - - - - - - - - - - - - - - - - - - - - -
Paragraphs/Paragraph對象
- - - - - - - - - - - - - - - - - - - - - -
(353) Selection.Paragraphs.Count '返回所選區域的段落數
(354) Selection.Paragraphs.First '返回所選區域中的第一段
(355) ActiveDocument.Paragraphs(1).LeftIndent '返回當前文檔中第一段的左縮進值
(356) ActiveDocument.Paragraphs(1).LineSpacing '返回當前文檔中第一段的行距
(357) ActiveDocument.Paragraphs(1).OutlineLevel? ‘返回或設置當前文檔中第一段的大綱級別
(358) ActiveDocument.Paragraphs(1).RightIndent ‘返回當前文檔中第一段的右縮進量
(359) ActiveDocument.Paragraphs(1).SpaceBefore '返回當前文檔中第一段的段前間距
(360) ActiveDocument.Paragraphs(1).SpaceAfter ‘返回當前文檔中第一段的段后間距
(361) ActiveDocument.Paragraphs(1).Range.Text '返回當前文檔中第一段的內容
(362) ActiveDocument.Paragraphs(1).Range.Style.NameLocal '返回當前文檔中第一段應用的樣式名
(363) ActiveDocument.Paragraphs(1).Range.Style.Descrīption '返回當前文檔中第一段所應用樣式的詳細描述
(364) ActiveDocument.Paragraphs(1).Range.Style.Font.Name '返回當前文檔中第一段所應用樣式的字體名
(365) ActiveDocument.Paragraphs(1).Range.Style.Font.NameFarEast '返回或設置一種東亞字體名
(366) ActiveDocument.Paragraphs(1).Range.Style.Font.Size '返回或設置當前文檔中第一段所應用樣式的字體大小
(367) ActiveDocument.Paragraphs(1).Range.Style.Font.Spacing '返回或設置字符間距
(368) Selection.Words.Count '所選區域的字數
- - - - - - - - - - - - -
Sentences對象
- - - - - - - - - - - - -
(369) Selection.Sentences.Item(1) '所選區域中的第一句的內容
- - - - - - - - - -
Words對象
- - - - - - - - - -
(371) ActiveDocument.Words(1).Select '選擇當前文檔中的第一個詞
(372) ActiveDocument.Range.Words(1).InsertAfter "我愛你!" '在當前文檔中的第一個詞后插入“我愛你”
- - - - - - - - - - - - -
Characters對象
- - - - - - - - - - - - -
(373) Selection.Characters.Count '當前文檔中所選區域的字符數
(374) ActiveDocument.Paragraphs(1).Range.InsertParagraphAfter '在當前文檔的第一段之后插入一個新段落
- - - - - - - - - - - - - - - - - -
Sections/Section對象
- - - - - - - - - - - - - - - - - -
(375) ActiveDocument.Sections.First '當前文檔的第一節
(376) ActiveDocument.Sections.First.PageSetup.BottomMargin '當前文檔第一節所在頁的底邊距
(377) ActiveDocument.Sections.First.PageSetup.LeftMargin '當前文檔第一節所在頁的左邊距
(378) ActiveDocument.Sections.First.PageSetup.RightMargin '當前文檔第一節所在頁的右邊距
(379) ActiveDocument.Sections.First.PageSetup.TopMargin '當前文檔第一節所在頁的頂邊距
(380) ActiveDocument.Sections.First.PageSetup.PaperSize '返回或設置當前文檔第一節所在頁的大小
(381) ActiveDocument.Sections.First.PageSetup.PageHeight '返回或設置當前文檔第一節所在頁的高度
(382) ActiveDocument.Sections.First.PageSetup.PageWidth '返回或設置當前文檔第一節所在頁的寬度
(383) ActiveDocument.Sections.Add Range:=myRange '在當前文檔中添加新節
(384) ActiveDocument.Sections.Item(2) '當前文檔中的第二節
(385) ActiveDocument.Sections.Last.Range.InsertAfter "文檔結束!" '在當前文檔中最后一節的結尾添加文字“文檔結束!”
- - - - - - - - - -
Range對象
- - - - - - - - - -
(386) ActiveDocument.Range(Start:=0, End:=10) '表示當前文檔前10個字符所組成的一個Range對象
(387) Set myRange = ActiveDocument.Range( _
??? Start:=ActiveDocument.Paragraphs(2).Range.Start, _
End:=ActiveDocument.Paragraphs(4).Range.End) '將當前文檔第2段至第4段設置為一個Range對象
(388) ActiveDocument.Paragraphs(1).Range.Copy? '復制當前文檔中的第一段
(389) Selection.Copy
Documents.Add.Content.Paste '復制所選內容到新文檔中
(390) ActiveDocument.Bookmarks("Book1").Copy Name:="Book2" '將Book2書簽復制Book1書簽標記的位置
(391) Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=4 '將所選內容移至文檔中的第4行
(392) Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext '將所選內容移至下一個表格的第1個單元格
(393) Selection.Range.AutoFormat '為所選內容套用格式
(394) ActiveDocument.Content.Font.Name = "Arial" '將當前文檔的字體設置為斜體
(395) ActiveDocument.Content.Select
Selection.Delete '將當前文檔中的內容刪除
- - - - -
其它
- - - - -
(396) Documents.Add '添加一個新文檔
(397) Set myTable = ActiveDocument.Tables.Add(Selection.Range, 2, 2) '在當前文檔所選區域添加一個2行2列的表格
(398) Open "C:"my.txt" For Input As #1 '打開一個用于輸入的文件并令其編號為1
(399) Line Input #1, TextLine '讀取被打開用于輸入且編號為1的文件
(400) Close #1? '關閉編號為1的文件
?
?
轉載于:https://www.cnblogs.com/strinkbug/archive/2009/03/26/1422412.html
總結
- 上一篇: HP6531s安装windows2003
- 下一篇: SQL Server 2005学习笔记