WORD批量修改类型(50分)

  • 主题发起人 主题发起人 LUJIAYU110
  • 开始时间 开始时间
L

LUJIAYU110

Unregistered / Unconfirmed
GUEST, unregistred user!
有大量WORD文档,想把它保存为MHT格式,单个文件好办,打开-->另存为网页-->单个文件就行,但批量修改怎么办,本人不会编写宏,请教可行的代码
 
The first and easiest is to save the document as HTML. If Doc is a
connected TWordDocument:
FileName := 'D:/Docs/Word/TestHTML.htm';
Fmt := wdFormatHTML;
Doc.SaveAs(FileName, Fmt);
You can then use the saved file and delete it when you've finished.

If you really don't want to do that, though, you could use the
clipboard instead - just copy the whole document and then use
Clipboard.GetAsHandle(CF_HTML) to get at the data. Using the
clipboard like that is a bit antisocial, however, so I'll move on to
method three, which is similar but uses the Word document's
IDataObject interface:

var
CF_HTML: Word;

var
Doc: WordDocument;
DataObj: IDataObject;
FormatEtc: TFormatEtc;
SM: TstgMedium;
TextPtr: PChar;
..
Doc := WordApp.ActiveDocument;
DataObj := Doc as IDataObject;
Assert(DataObj <> nil);

FormatEtc.cfFormat := CF_HTML;
FormatEtc.tymed := TYMED_HGLOBAL;
FormatEtc.ptd := nil;
FormatEtc.dwAspect := DVASPECT_CONTENT;
FormatEtc.lindex := -1;

OleCheck(DataObj.GetData(FormatEtc, SM));
TextPtr := GlobalLock(SM.hGlobal);
Memo1.Lines.Text := StrPas(TextPtr);
GlobalUnlock(SM.hGlobal);
ReleaseStgMedium(SM);

initialization
CF_HTML := RegisterClipboardFormat('HTML Format');
 
下面的代码是测试成功的(注意引用这几个单元 : OleServer, WordXP :主要是wdFormatHTML 这个常量定义在WordXP 中,当然如果不引用也可以:我查到了wdFormatHTML=8 ,直接用8也可以):
procedure TForm1.Button1Click(Sender: TObject);
var
wordapp:Variant;
doc:Variant;
begin


OpenDialog1.Filter:='*.doc';
OpenDialog1.Execute ;
SaveDialog1.Filter:='*.html';
SaveDialog1.Execute;
try
wordapp := CreateOleObject('Word.Application');
doc :=wordapp.Documents.open(FileName:=OpenDialog1.FileName,ReadOnly:=False,
AddToRecentFiles:=False);

doc.SaveAs(FileName:=savedialog1.FileName,FileFormat:= wdFormatHTML );
doc.close;
wordapp.quit;
except
MessageDlg('对不起,您没有安装Word 软件!', mtInformation,
[mbOk], 0);
FreeAndNil(doc);
FreeAndNil(wordapp );

end;


end;
 
测试通过,请abin30接分.谢谢。
另外我再问一下,这样保存为HTM格式,如果要MHT格式,怎么改?
 
后退
顶部