批量刪除一個(gè)文件夾下所有Word文件的頁眉和頁腳_第1頁
批量刪除一個(gè)文件夾下所有Word文件的頁眉和頁腳_第2頁
批量刪除一個(gè)文件夾下所有Word文件的頁眉和頁腳_第3頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、批量刪除一個(gè)文件夾下所有word文件的頁眉和頁腳* * created by shourouexcelhome 2007-12-26 6:12:17僅測試于system: windows nt word: 11.0 language: 2052 0292the code copyin thisdocument-thisdocument* option explicitsub example() 此代碼功能為列出指定文件夾中所有選取的word文件全路徑名 dim mydialog as filedialog, odoc as document, osec as section dim ofile

2、 as variant, myrange as range on error resume next 定義一個(gè)文件夾選取對話框 set mydialog = application.filedialog(msofiledialogfilepicker) with mydialog .filters.clear 清除所有文件篩選器中的項(xiàng)目 .filters.add 所有 word 文件, *.doc, 1 增加篩選器的項(xiàng)目為所有word文件 .allowmultiselect = true 允許多項(xiàng)選擇 if .show = -1 then 確定 for each ofile in .selec

3、teditems 在所有選取項(xiàng)目中循環(huán) set odoc = word.documents.open(filename:=ofile, visible:=false) for each osec in odoc.sections 文檔的節(jié)中循環(huán) set myrange = osec.headers(wdheaderfooterprimary).range myrange.delete 刪除頁眉中的內(nèi)容 myrange.paragraphformat.borders(wdborderbottom).linestyle = wdlinestylenone 段落下邊框線 set myrange =

4、osec.footers(wdheaderfooterprimary).range myrange.delete 刪除頁腳中的內(nèi)容 next odoc.close true next end if end withend sub- 在vba遍歷文件夾和子文件夾中所有文件,常用兩種方法,一種是使用vba的filesercth對象,另外一種是使用filesystemobject(windows文件管理工具)和遞歸方法。蘭色對代碼進(jìn)行了注解,希望對大家有所幫助 第一種方法:使用filesearch對象 sub mysearch()dim fs, i, arr(1 to 10000)set fs =

5、application.filesearch 設(shè)置一個(gè)搜索對象with fs .lookin = thisworkbook.path & / 設(shè)置搜索路徑 .filename = *.xls 要搜索文件名和類型 .searchsubfolders = true 是否需要搜索子文件夾 if .execute 0 then 如果找不到文件 msgbox there were & .foundfiles.count & _ file(s) found. 顯示文件找不到 for i = 1 to .foundfiles.count 通過循環(huán)把所有搜索到的文件存入到數(shù)組中 arr(i) = .found

6、files(i) next isheets(1).range(a1).resize(.foundfiles.count) = application.transpose(arr) 把數(shù)組內(nèi)的路徑和文件名放在單元格中 else msgbox there were no files found. end ifend withend sub第二種方法:引用filesystemobject對象 注意:要使用filesystemobject對象,需要首先引用一下,具體方法,vbe-工具-引用-找到miscrosoft scription runtime項(xiàng)目并選中 代碼及注釋: dim arrfiles(

7、1 to 10000) 創(chuàng)建一個(gè)數(shù)組空間,用來存放文件名稱dim cntfiles% 文件個(gè)數(shù)public sub listallfiles() dim strpath$ 聲明文件路徑 dim i% set fso = createobject(scripting.filesystemobject) dim fso as new filesystemobject, fd as folder 創(chuàng)建一個(gè)filesystemobject對象和一個(gè)文件夾對象 strpath = thisworkbook.path & 設(shè)置要遍歷的文件夾目錄 cntfiles = 0 set fd = fso.getf

8、older(strpath) 設(shè)置fd文件夾對象 searchfiles fd 調(diào)用子程序查搜索文件 sheets(1).range(a1).resize(cntfiles) = application.transpose(arrfiles) 把數(shù)組內(nèi)的路徑和文件名放在單元格中end subsub searchfiles(byval fd as folder) dim fl as file dim sfd as folder for each fl in fd.files 通過循環(huán)把文件逐個(gè)放在數(shù)組內(nèi) cntfiles = cntfiles + 1 arrfiles(cntfiles) = fl.path next fl if fd.subfolders.count = 0 then exit sub subfolders返回由指定文件

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論