With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(0.8)
.BottomMargin = InchesToPoints(0.8)
.LeftMargin = InchesToPoints(0.8)
.RightMargin = InchesToPoints(0.8)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(8.5)
.PageHeight = InchesToPoints(11)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.GutterPos = wdGutterPosLeft
End With
Selection.Sections(1).Headers(1).PageNumbers.Add PageNumberAlignment:= _
wdAlignPageNumberRight, FirstPage:=True
Dim quote As String
Dim Keyword As String
Dim j As Integer
quote = """"
Dim found_key As Boolean
Dim startSearch As Long
Dim endSearch As Long
Close #1
Open "C:\Users\shuqi\Desktop\新建文本文档.txt" For Input As #1
Set myRange = ActiveDocument.Content
j = 0
Do While Not EOF(1) ' Loop until end of file.
Set myRange = ActiveDocument.Content
endSearch = myRange.End
Input #1, Keyword
j = 0
With myRange.Find
.Text = Keyword
.Forward = True
.MatchWholeWord = True
.MatchCase = False
End With
While myRange.Find.Execute
myRange.Collapse wdCollapseEnd
Set myIndexEntry = myRange.Fields.Add(myRange, Type:=wdFieldIndexEntry, _
Text:=quote & Keyword & quote)
startSearch = myRange.End
startSearch = startSearch + 7
Set myRange = ActiveDocument.Content
myRange.Start = startSearch
If startSearch = endSearch - 1 Then
GoTo skip_while
End If
With myRange.Find
.Text = Keyword
.Forward = True
.MatchWholeWord = True
.MatchCase = False
End With
' this code is because I had a loop here
j = j + 1
If j = 300 Then
myRange.Bold = True
Exit Do
End If
Wend
skip_while:
Loop
Close #1 ' Close file.
myRange.Start = 0
myRange.End = 0
With ActiveDocument
.Indexes.Add Range:=myRange, HeadingSeparator:= _
wdHeadingSeparatorNone, Type:=wdIndexIndent, RightAlignPageNumbers:= _
True, NumberOfColumns:=1, IndexLanguage:=wdEnglishUS
.Indexes(1).TabLeader = wdTabLeaderDots
End With
End Sub
如果你的英语老师死得早,你也可以请教下英文词典。
开头几句给你翻译下,别的去请教你的英语老师:
With ActiveDocument.PageSetup 活动文档的页面设置
.LineNumbering.Active = False 行号的不要
.Orientation = wdOrientPortrait 纸张纵向
.TopMargin = InchesToPoints(0.8) 顶端边距
.BottomMargin = InchesToPoints(0.8) 底端边距
.LeftMargin = InchesToPoints(0.8) 左边距
.RightMargin = InchesToPoints(0.8) 右边距
.Gutter = InchesToPoints(0) 装订线
.HeaderDistance = InchesToPoints(0.5) 页眉距离
.FooterDistance = InchesToPoints(0.5) 页脚距离
.PageWidth = InchesToPoints(8.5) 纸张宽度
.PageHeight = InchesToPoints(11) 纸张高度
.FirstPageTray = wdPrinterDefaultBin 首页装纸盘
.OtherPagesTray = wdPrinterDefaultBin 其它页装纸盘(这里解释下,有时候我们封面比如说用牛皮纸,其他页用打印纸,那么这两个设置就很有用)
.SectionStart = wdSectionNewPage 选区开始
.OddAndEvenPagesHeaderFooter = False 单页和双页区分页眉页脚
。。。
呵呵,你要是经常和代码打交道的话,英语一般般,这些属性基本上一看就知道个大概。
要是想逐一弄懂都是什么意思,可以安装下面步骤:
录制一段宏(专录页面设置部分),随便设置几项内容,结束宏后查看代码然后逐一对应看是什么意思,
有不懂的属性直接在vba窗口内打开对象浏览器然后搜索相关的属性,看看是如何使用的。
比如:
Sub Macro1()
'
' Macro1 Macro
' 宏在 2015/1/9 由 MicroDDD 录制
'
With ActiveDocument.Styles(wdStyleNormal).Font
If .NameFarEast = .NameAscii Then
.NameAscii = ""
End If
.NameFarEast = ""
End With
With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientLandscape
.TopMargin = CentimetersToPoints(3.17)
.BottomMargin = CentimetersToPoints(3.17)
.LeftMargin = CentimetersToPoints(2.54)
.RightMargin = CentimetersToPoints(2.54)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.5)
.FooterDistance = CentimetersToPoints(1.75)
.PageWidth = CentimetersToPoints(29.7)
.PageHeight = CentimetersToPoints(21)
.FirstPageTray = wdPrinterAutomaticSheetFeed
.OtherPagesTray = wdPrinterAutomaticSheetFeed
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
.LayoutMode = wdLayoutModeLineGrid
End With
End Sub
呵呵,我的word技巧还有不少是我的英语老师教我的。