-
當前位置:首頁 > 創(chuàng)意學院 > 營銷推廣 > 專題列表 > 正文
word vba編程實例(word vba編程基礎知識)
大家好!今天讓創(chuàng)意嶺的小編來大家介紹下關于word vba編程實例的問題,以下是小編對此問題的歸納整理,讓我們一起來看看吧。
開始之前先推薦一個非常厲害的Ai人工智能工具,一鍵生成原創(chuàng)文章、方案、文案、工作計劃、工作報告、論文、代碼、作文、做題和對話答疑等等
只需要輸入關鍵詞,就能返回你想要的內容,有小程序、在線網頁版、PC客戶端和批量生成器
問友Ai官網:https://ai.de1919.com。
本文目錄:
如何利用VBA代碼或者腳本代碼批量修改word文檔的正文標題
操作步驟。第一,首先將需要批量替換的多個Word文檔放在同一文件夾下面。
第二,新建一空白Word文檔,右擊空白工具欄,單擊“控件工具箱”,就可以看到屏幕上調出的控件工具箱。
第三,在控件工具箱上單擊“命令按鈕”,文檔中就放置了一個按鈕了。
第四,雙擊該按鈕,進入VB代碼編寫模式,將以下代碼復制進去。
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
Dim myPas As String, myPath As String, i As Integer, myDoc As Document
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "選擇目標文件夾"
If .Show = -1 Then
myPath = .SelectedItems(1)
Else
Exit Sub
End If
End With
myPas = InputBox("請輸入打開密碼:")
With Application.FileSearch
.LookIn = myPath
.FileType = msoFileTypeWordDocuments
If .Execute > 0 Then
For i = 1 To .FoundFiles.Count
Set myDoc = Documents.Open(FileName:=.FoundFiles(i), Passworddocument:=myPas)
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "大家好"
.Replacement.Text = "你好"
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
myDoc.Save
myDoc.Close
Set myDoc = Nothing
Next
End If
End With
Application.ScreenUpdating = True
End Sub
第五,保存上面代碼,退出VB編輯模式,返回Word文檔界面。
第六,單擊選中該按鈕,再單擊控件工具箱的第一個按鈕“退出設計模式”。
第七,進行測試:點擊按鈕,選擇要放置多個WORD文檔所在的文件夾,確定后即可完成!注意如果WORD文檔沒有加密的話,密碼項就不填,直接確認。就會發(fā)現(xiàn)該文件夾下面的所有WORD文檔中“大家好”已被替換為“你好”了。
怎樣利用vba自動生成word報告
以下是自行編寫的代碼實例片段,供參考:
'==================='生成分析報告
'===================
Private Sub btnDtyp_Click()
sPathName = ThisWorkbook.Path & "模板" & "動態(tài)研判分析.doc"
Set WordApp = CreateObject("Word.Application") '生成WORD對象
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Add(sPathName)
With Sheet6
sText = "{ksrq}" '開始日期
sReplace = Format(Range("D1").Value, "YYYY年M月DD日")
WordDoc.Range.Find.Execute sText, False, True, False, False, False, True, 0, False, sReplace, 2
sText = "{jsrq}" '結束日期
sReplace = Format(Range("F1").Value, "YYYY年M月DD日")
WordDoc.Range.Find.Execute sText, False, True, False, False, False, True, 0, False, sReplace, 2
sText = "{zj}" '總計人數(shù)
sReplace = .Range("E48").Value
WordDoc.Range.Find.Execute sText, False, True, False, False, False, True, 0, False, sReplace, 2
sText = "{nan}" '男性人數(shù)
sReplace = .Range("B33").Value
WordDoc.Range.Find.Execute sText, False, True, False, False, False, True, 0, False, sReplace, 2
sText = "{nv}" '女性人數(shù)
sReplace = .Range("B34").Value
WordDoc.Range.Find.Execute sText, False, True, False, False, False, True, 0, False, sReplace, 2
sText = "{jyrs}" '就業(yè)人數(shù)
sReplace = .Range("B84").Value + .Range("B85").Value
WordDoc.Range.Find.Execute sText, False, True, False, False, False, True, 0, False, sReplace, 2
sText = "{jxrs}" '就學人數(shù)
sReplace = .Range("B86").Value
WordDoc.Range.Find.Execute sText, False, True, False, False, False, True, 0, False, sReplace, 2
End With
sFileName = "分析記錄(" & Range("D1").Value & "至" & Range("F1").Value & ").doc"
WordApp.ChangeFileOpenDirectory "E:分析報告"
WordDoc.SaveAs Filename:=sFileName, FileFormat:=wdFormatDocument
End Sub
以上就是關于word vba編程實例相關問題的回答。希望能幫到你,如有更多相關問題,您也可以聯(lián)系我們的客服進行咨詢,客服也會為您講解更多精彩的知識和內容。
推薦閱讀:
wordpress城市分站插件(wordpress站點地圖插件)