{"id":1315,"date":"2024-07-23T08:00:00","date_gmt":"2024-07-23T00:00:00","guid":{"rendered":"http:\/\/www.wyrcad.com\/?p=1315"},"modified":"2024-07-23T08:43:35","modified_gmt":"2024-07-23T00:43:35","slug":"word-%e5%87%bd%e6%95%b0%e7%b3%bb%e5%88%97%e4%b9%8b%e4%b8%80","status":"publish","type":"post","link":"https:\/\/www.wyrcad.com\/?p=1315","title":{"rendered":"Word \u51fd\u6570\u7cfb\u5217\u4e4b\u4e00"},"content":{"rendered":"\n<p>Public Sub ReplaceWord(ByVal vWord As Variant, ByVal sOld As String, ByVal sNew As String)<br>Const wdReplaceAll = 2<br>Const wdFindStop = 0<br>Dim oRange As Object<br>Set oRange = vWord.Selection.Range<br>&#8216;\u5148\u5224\u65ad\u662f\u5426\u6709\u9009\u4e2d\u533a\u57df\uff0c\u6ca1\u6709\u9009\u4e2d\u5219\u8868\u793a\u6574\u4e2a\u6587\u6863<br>If oRange.Start = oRange.End Then<br>Set oRange = vWord.ActiveDocument.Content<br>End If<br>With oRange.Find<br>&#8216;\u6279\u91cf\u67e5\u627e\u66ff\u6362 sOld \u4e3a sNew<br>bRet = .Execute(FindText:=sOld, replacewith:=sNew, Replace:=wdReplaceAll)<br>&#8216;Debug.Print bRet<br>&#8216;Debug.Print oRange.Words.Count<br>End With<br>End Sub<\/p>\n\n\n\n<p>Public Sub InsPageNumber(ByVal vWord As Variant) &#8216;\u5176\u5b9e\u662f vDocument<br>On Error GoTo ERR_PAGENUMBER<br>&#8216;\u8bbe\u7f6eWord\u6587\u6863\u7b2c\u4e00\u9875\u9875\u7801<br>Dim oRange As Object<br>Set oRange = vWord.ActiveDocument.Sections(1).Footers(1).Range &#8216;wdHeaderFooterPrimary = 1<br>With oRange<br>.InsertAfter &#8220;\u7b2c&#8221;<br>&#8216;.Font.Size = 14<br>.Collapse Direction:=0 &#8216;wdCollapseEnd = 0<br>&#8216;\u63d2\u5165\u9875\u7801\u57df<br>.Fields.Add Range:=oRange, Type:=-1, Text:=&#8221;PAGE * Arabic &#8220;, PreserveFormatting:=True &#8216;wdFieldEmpty = -1<br>.Expand Unit:=2 &#8216;wdWord = 2<br>.InsertAfter &#8220;\u9875&#8221;<br>.InsertAfter &#8220;\u5171&#8221;<br>.Collapse Direction:=0 &#8216;wdCollapseEnd = 0<br>&#8216;\u63d2\u5165\u9875\u6570\u57df<br>.Fields.Add Range:=oRange, Type:=-1, Text:=&#8221;NUMPAGES * Arabic &#8220;, PreserveFormatting:=True &#8216;wdFieldEmpty = -1<br>.Expand Unit:=2 &#8216;wdWord = 2<br>.InsertAfter &#8220;\u9875&#8221;<br>&#8216;.InsertAfter &#8220;YagerSoft&#8221;<br>.ParagraphFormat.Alignment = 2 &#8216;wdAlignParagraphRight = 2 &#8216;\u53f3\u5bf9\u9f50<br>End With<br>&#8216;\u9690\u85cf\u9875\u7709\u7684\u6a2a\u7ebf<br>vWord.ActiveDocument.Sections(1).Headers(1).Range.Borders(-3).Visible = False &#8216;wdBorderBottom = -3<br>&#8216;\u53d6\u5f97\u9875\u7709\u7684\u5185\u5bb9<br>&#8216;Debug.Print vWord.ActiveDocument.Sections(1).Headers(2).Range.Text &#8216;wdHeaderFooterFirstPage = 2<br>Set oRange = Nothing<br>On Error GoTo 0<br>Exit Sub<br>ERR_PAGENUMBER:<br>On Error GoTo 0<br>End Sub<\/p>\n\n\n\n<p>Public Sub InsPageBreak(ByVal vWord As Variant)<br>On Error GoTo ERR_BREAK<br>&#8216;wdPropertyTitle = 1\u6807\u9898<br>&#8216;wdPropertySubject = 2\u4e3b\u9898<br>&#8216;wdPropertyAuthor = 3\u4f5c\u8005<br>&#8216;wdPropertyKeywords = 4\u5173\u952e\u8bcd<br>&#8216;wdPropertyComments = 5\u6279\u6ce8<br>&#8216;wdPropertyTemplate = 6\u6a21\u677f<br>&#8216;wdPropertyLastAuthor = 7\u4e0a\u4e00\u4e2a\u4f5c\u8005<br>&#8216;wdPropertyRevision = 8\u4fee\u8ba2\u6b21\u6570<br>&#8216;wdPropertyAppName = 9\u5e94\u7528\u7a0b\u5e8f\u540d<br>&#8216;wdPropertyTimeLastPrinted = 10\u4e0a\u6b21\u6253\u5370\u65f6\u95f4<br>&#8216;wdPropertyTimeCreated = 11\u521b\u5efa\u65f6\u95f4<br>&#8216;wdPropertyTimeLastSaved = 12\u4e0a\u6b21\u4fdd\u5b58\u65f6\u95f4<br>&#8216;wdPropertyVBATotalEdit = 13\u7f16\u8f91\u65f6\u95f4\u603b\u8ba1<br>&#8216;wdPropertyPages = 14\u9875\u6570<br>&#8216;wdPropertyWords = 15\u5b57\u6570<br>&#8216;wdPropertyCharacters = 16\u5b57\u7b26\u6570<br>&#8216;wdPropertySecurity = 17\u5b89\u5168\u6027<br>&#8216;wdPropertyCategory = 18\u7c7b\u522b<br>&#8216;wdPropertyFormat = 19<br>&#8216;wdPropertyManager = 20\u7ecf\u7406<br>&#8216;wdPropertyCompany = 21\u516c\u53f8<br>&#8216;wdPropertyBytes = 22\u5b57\u8282\u6570<br>&#8216;wdPropertyLines = 23\u884c\u6570<br>&#8216;wdPropertyParas = 24\u6bb5\u6570(\u7a7a\u767d\u6587\u6863 BuiltInDocumentProperties(wdPropertyParas) = 0\uff0c\u800c vWord.ActiveDocument.Paragraphs.Count = 1)<br>&#8216;wdPropertySlides = 25<br>&#8216;wdPropertyNotes = 26\u6ce8\u91ca<br>&#8216;wdPropertyHiddenSlides = 27<br>&#8216;wdPropertyMMClips = 28<br>&#8216;wdPropertyHyperlinkBase = 29<br>&#8216;wdPropertyCharsWSpaces = 30\u5b57\u7b26\u6570\uff08\u8ba1\u7a7a\u683c\uff09<br>If vWord.ActiveDocument.BuiltInDocumentProperties(15) = 0 Then Exit Sub &#8216;wdPropertyWords = 15<br>vWord.Selection.EndKey Unit:=6 &#8216;wdStory = 6 &#8216;\u5c06\u5149\u6807\u79fb\u5230\u6700\u540e<br>vWord.Selection.InsertBreak Type:=7 &#8216;wdPageBreak = 7 &#8216;\u63d2\u5165\u5206\u9875\u7b26<br>On Error GoTo 0<br>Exit Sub<br>ERR_BREAK:<br>On Error GoTo 0<br>End Sub<\/p>\n\n\n\n<p>Public Function FindWord(ByVal vWord As Variant, ByVal sData As String) As Boolean<br>Dim oSelection As Object<br>Set oSelection = vWord.ActiveDocument.Content<br>&#8216;\u5229\u7528FIND\u67e5\u627e sData \uff0c\u4ece\u5149\u6807\u4e4b\u5904\u5f00\u59cb\u67e5\u627e\uff0c\u67e5\u627e\u5230\u540e\u9009\u4e2d\u3002<br>With oSelection.Find<br>&#8216;\u67e5\u627e\u7684\u65b9\u5411\u5411\u4e0b<br>.Forward = True<br>&#8216;\u53d6\u6d88\u5728\u67e5\u627e\u6216\u66ff\u6362\u64cd\u4f5c\u4e2d\u6240\u6307\u5b9a\u6587\u672c\u7684\u6587\u672c\u683c\u5f0f\u548c\u6bb5\u843d\u683c\u5f0f<br>.ClearFormatting<br>&#8216;\u67e5\u627e\u64cd\u4f5c\u67e5\u627e\u4ec5\u5b8c\u6574\u5355\u8bcd\uff0c\u800c\u4e0d\u662f\u8f83\u957f\u5355\u8bcd\u7684\u4e00\u90e8\u5206\u7684\u6587\u672c<br>.MatchWholeWord = True<br>&#8216;\u67e5\u627e\u65f6\u4e0d\u533a\u5206\u5927\u5c0f\u5199<br>.MatchCase = False<br>&#8216;\u5230\u8fbe\u6587\u6863\u672b\u5c3e\u65f6\uff0c\u7ee7\u7eed\u4ece\u6587\u6863\u5f00\u5934\u8fdb\u884c\u641c\u7d22<br>.Wrap = 1 &#8216;wdFindContinue<br>&#8216;\u8fd0\u884c\u6307\u5b9a\u7684\u67e5\u627e\u64cd\u4f5c\u3002\u5982\u679c\u67e5\u627e\u6210\u529f\uff0c\u5219\u8fd4\u56de True<br>.Execute FindText:=sData<br>End With<br>FindWord = oSelection.Find.Execute<br>End Function<\/p>\n\n\n\n<p>Public Function GetTextSite(ByVal vWord As Variant, ByVal sText As String) As Integer &#8216;\u8fd4\u56de sText \u5728 vWord \u4e2d\u9996\u6b21\u51fa\u73b0\u7684\u6bb5\u53f7<br>&#8216;\u8017\u65f6\u8fc7\u957f\u4e0d\u5b9c\u7528<br>Dim I As Integer, J As Integer<br>GetTextSite = 0<br>If vWord Is Nothing Then Exit Function<br>If vWord.Documents.Count = 0 Then Exit Function<br>If sText = &#8220;&#8221; Then Exit Function<br>For I = 1 To vWord.ActiveDocument.Paragraphs.Count<br>DoEvents<br>&#8216;If vWord.ActiveDocument.Paragraphs(I).Range.Text = sText Then<br>If InStr(1, vWord.ActiveDocument.Paragraphs(I).Range.Text, sText, vbTextCompare) &gt; 0 Then<br>GetTextSite = I<br>Exit For<br>End If<br>Next I<br>End Function<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Public Sub ReplaceWord(ByVal vWord As Variant, ByVal sO &hellip; <a href=\"https:\/\/www.wyrcad.com\/?p=1315\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cWord \u51fd\u6570\u7cfb\u5217\u4e4b\u4e00\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[24,17],"class_list":["post-1315","post","type-post","status-publish","format-standard","hentry","category-bianchengyuandi","tag-vb","tag-biancheng"],"_links":{"self":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts\/1315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1315"}],"version-history":[{"count":3,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts\/1315\/revisions"}],"predecessor-version":[{"id":1399,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts\/1315\/revisions\/1399"}],"wp:attachment":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}