㈠ delphi讀取word文檔每一頁的內容
uses ComObj,WordXp;
var wordapp, WordDoc, PageRange: Variant;
sContext: string;
i, nPageCounts, nStart, nEnd : Integer;
begin
wordapp := CreateOleObject('Word.Application');
try
wordapp.Visible := True;
if dlgOpen1.Execute = False then Exit;
WordDoc := wordapp.Documents.Open(dlgOPen1.FileName);
//文檔總頁數
nPageCounts := wordapp.Selection.Information[wdNumberOfPagesInDocument];
//如果只有一頁 那麼全選就OK了
if nPageCounts = 1 then
begin
wordapp.Selection.WholeStory;
mmo1.Lines.Add('=============第'+IntToStr(nPageCounts)+'頁內容:===================');
mmo1.Lines.Add(wordapp.Selection.Text);
Exit;
end;
nStart := -1;
nEnd := -1;
//循環獲取文檔頁中的內容
for i := 1 to nPageCounts do
begin
//定位到第i頁
PageRange := wordapp.Selection.GoTo(wdGoToPage, wdGoToNext, IntToStr(i));
//如果第i頁是最後一頁 那麼直接將游標移動到最後 並輸出內容
if i = nPageCounts then
begin
wordapp.Selection.EndKey(wdStory,wdExtend);
sContext := WordApp.Selection.Range.Text;
mmo1.Lines.Add('=============第'+IntToStr(i)+'頁內容:===================');
mmo1.Lines.Add(sContext);
Exit;
end;
//取第i頁的頁首位置作為開始位置
nStart := wordapp.Selection.Start;
//定位到i+1頁
PageRange := wordapp.Selection.GoTo(wdGoToPage, wdGoToNext, IntToStr(i+1));
//取第i+1頁的頁首位置作為結束位置
nEnd := wordapp.Selection.Start;
//根據開始位置和結束位置確定文檔選中的內容(第i頁的內容)
WordDoc.Range(nStart,nEnd).Select;
sContext := WordDoc.Range.Text;
//輸出內容
mmo1.Lines.Add('=============第'+IntToStr(i)+'頁內容:===================');
mmo1.Lines.Add(sContext);
nStart := -1;
nEnd := -1;
end;
finally
wordapp.Quit;
end;
end;
昨天沒有測試好 這個應該沒有問題了吧 試一下吧
㈡ delphi中,如何讀取一個目錄中的所有文件在線等……
procere FileSearch(PathName:string);
var
F : TSearchRec;
Found : Boolean;
begin
ChDir(PathName);
Found := (FindFirst('*.*', faAnyFile, F) = 0);
while Found do
begin
if (F.Name = '.') or (F.Name = '..') then
begin
Found := (FindNext(F) = 0);
Continue;
end;
if (F.Attr and faDirectory)>0 then
begin
Application.ProcessMessages;
FileSearch(F.Name);
end;
//插入你的代碼,F.Name就是文件名,GetCurrentDir可以得到當前目錄
Found := (FindNext(F) = 0);
end;
FindClose(F);
ChDir('..\');
end;
轉換目錄可以用MoveFile,查一下幫助
㈢ delphi 中查找已知路徑下某類型文件的個數
變數沒有初始化,如下:
-----------------------------------------------------------------
procere TForm1.Button2Click(Sender: TObject);
var
Search:TSearchRec; //搜索到的文件信息的一個數據結構
ret,FileNums:integer; //findFirst文件搜索方法的返回值,0表示成功,不為0表示失敗
key:string;
FileName,FilePath:string;
begin
FileNums :=0; //*>>>>>>>>>>>>>>>>>>>這里加上初始化>>>>>>>>>>>>>>
FilePath := edit1.Text;
key := FilePath+ '*.txt';
ret := SysUtils.FindFirst(key,faanyfile,search);
㈣ delphi里怎麼把指定文件夾內的所有文件名輸出出來
1、使用函數和FindNext函數就可以查找出文件夾內所有的文件名,編寫如下一個函數:
function searchfile(path:string):TStringList;
var SearchRec:TSearchRec;
found:integer;
list:TStringList;
begin
list:=TStringList.Create;
found:=FindFirst(path+'*.*',faAnyFile,SearchRec);
while found=0 do
begin
if (SearchRec.Name<>'.') and (SearchRec.Name<>'..') and (SearchRec.Attr<>faDirectory)
then List.Add(SearchRec.Name);
found:=FindNext(SearchRec);
end;
FindClose(SearchRec);
searchfile:=list;
end;
2、使用searchfile(要查找的路徑),就可以得到所有文件名,如下示例:
procere TForm1.btn1Click(Sender: TObject);
begin
mmo1.Lines.AddStrings(searchfile('G:BaiDu\_codeigimg'));
end;
效果如下:
㈤ delphi如何獲得一個文件夾下所有文件的信息
方法來1:先調用FindFirst啟動列表,再自循環調用FindNext獲取文件名存入aa數組,最後調用FindClose關閉列表
方法2:直接使用TFileListBox控制項,設置好Drive、Directory、FileType屬性,然後訪問其Items數組就能得到所有文件/子目錄列表了,很簡單