老问题,关于在word中插入图片。 我提供一部分代码,希望高手帮我补补(200分)

J

jifeng

Unregistered / Unconfirmed
GUEST, unregistred user!
希望进来的朋友耐心看完,只有耐心看完下面的文字你才能了解我的问题所在。并且你即使不能回答我的问题,你也会在我的贴子里学到点东西,谢谢了,朋友

我要完成的功能是:在当前光标的位置插入一张图片

我现在的思路是:在要插入图片的地方放一个标签,然后通过程序让光标走到标签那个地方,取到光标的座标值,然后通过插入图片的方法,插入一张图片。根据之前得到的光标的座标,设置刚刚插入的图片的位置。

现在的问题是:我想在第二页插入一张图片。我把标签放在第二页的一个地方,然后插入一张图片,这时图片是在第一页的,然后我把图片设置为标签的座标上时,图片没有到第二页,而是在第一页的相对座标值上。因为是在取得光标座标的时候,取到的是光标当前页的相对座标。 而图片插入在了第一页,所以图片就出现在了第一页的相对座标上。

我该如果让图片出现在第二页的那个座标上?

目前代码如下(这是我写的一个类,相关用到的方法我已经给了注释,其它方法留下来的目的是希望能给有需要的朋友一点帮助):
unit MyWordDocument;

interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, jpeg, ExtCtrls, Word2000, OleServer, DBCtrls, DB,
DBTables, ComObj;

type
TMyWordDocument = Class
private
wordApp:Variant;
wordDoc:Variant;

procedure setWordApp(wa:Variant);
procedure setWordDoc(wd:Variant);
public
constructor create();
procedure openWord();
procedure newDocument(templateName:String);
procedure openDocument(fileName:String);
procedure saveAs(saveToFileName:String);
procedure closeWord();
procedure ReplaceAll(findText, replaceWith: String);
procedure ReplaceNext(findText, replaceWith: String);
//让光标移动到标签处
procedure goToMark(bookmarkName:String);
procedure gotoPageNumber(PageNum:Integer);
//插入图片
procedure addPic(picFileName:String; picTop, picLeft, picWidth, picHeight:Integer; wrapType:Integer);
function getWordApp:variant;
function getWordDoc:variant;
//取光标横座标值
function getCursorHorizontalPosition:Integer;
//取光标纵座标值
function getCursorVerticalPosition:Integer;

function GetPageCount: integer;
procedure shwoNextPage;
End;
implementation

{ TWordDocument }

procedure TMyWordDocument.shwoNextPage;
begin
self.WordApp.Browser.Next;
end;

function TMyWordDocument.GetPageCount: integer;
begin
result := self.wordDoc.BuiltInDocumentProperties[wdPropertyPages].Value;
end;

procedure TMyWordDocument.gotoPageNumber(PageNum:Integer);
var
what,which,Count,PageNumber:OleVariant;
begin
what:=wdGoToPage;
which:=wdGoToNext;
Count:=self.wordApp.Selection.Information[wdNumberOfPagesInDocument];
PageNumber := OleVariant(PageNum);
self.wordApp.Selection.GoTo(what,which,Count,PageNumber);
end;

procedure TMyWordDocument.addPic(picFileName: String; picTop, picLeft, picWidth, picHeight: Integer; wrapType:Integer);
var
linkToFile, saveWithDocument, myPic : OleVariant;
begin
linkToFile := false;
saveWithDocument := true;
myPic := self.wordApp.ActiveDocument.AddPicture(picFileName, linkToFile, saveWithDocument);

//self.wordApp.ActiveDocument.Shapes.Item(1).Select(1);
//self.wordApp.Selection.cut;
//self.gotoPageNumber(pageNum);
//self.wordApp.Selection.Paste;

//设置图片位置及大小
myPic.width := picWidth;
myPic.height := picHeight;
myPic.top := picTop-0;
myPic.left := picLeft-0;

//设置图片环绕方式
//书上抄的,经试验,格式不准
case wrapType of
0:begin //嵌入式
//myPic.WrapFormat.type := wdWrapNone;
end;
1:begin //四周式
myPic.WrapFormat.type := wdWrapSquare;
end;
2:begin //紧密式
myPic.WrapFormat.type := wdWrapTight;
end;
3:begin //衬于文字上方
myPic.WrapFormat.type := wdWrapTopBottom;
end;
4:begin //衬于文字下面
myPic.WrapFormat.Type := wdWrapThrough;
myPic.WrapFormat.Type := wdWrapNone;
end;
end;

{//图片环绕类型常量定义 Word2000.pas
wdWrapSquare = $00000000;
wdWrapTight = $00000001;
wdWrapThrough = $00000002;
wdWrapNone = $00000003;
wdWrapTopBottom = $00000004;

wdWrapBoth = $00000000;
wdWrapLeft = $00000001;
wdWrapRight = $00000002;
wdWrapLargest = $00000003;
}


end;

procedure TMyWordDocument.closeWord;
var
SaveChanges, OriginalFormat, RouteDocument: OleVariant;
begin
SaveChanges := WdDoNotSaveChanges;
OriginalFormat := UnAssigned;
RouteDocument := UnAssigned;
try
self.wordApp.documents.close;
self.wordApp.Quit(SaveChanges, OriginalFormat, RouteDocument);
//self.wordApp.Disconnect;
except
on E: Exception do
begin
Showmessage(E.Message);
//self.wordApp.Disconnect;
end;
end;
end;

constructor TMyWordDocument.create;
begin
end;

function TMyWordDocument.getCursorHorizontalPosition: Integer;
var
rlt, plt:Integer;
begin
plt := self.wordDoc.PageSetup.LeftMargin;
rlt := self.wordApp.Selection.Information[wdHorizontalPositionRelativeToPage];
result := rlt - plt;
end;

function TMyWordDocument.getCursorVerticalPosition: Integer;
var
rtp, ptp:Integer;
begin
ptp := self.wordDoc.PageSetup.TopMargin;
rtp := self.wordApp.Selection.Information[wdVerticalPositionRelativeToPage];
result := rtp - ptp;
end;

function TMyWordDocument.getWordApp: variant;
begin
result := self.wordApp;
end;

function TMyWordDocument.getWordDoc: variant;
begin
result := self.wordDoc;
end;

procedure TMyWordDocument.goToMark(bookmarkName: String);
var
what,b,c,bmrkName: OleVariant;
begin
what := wdGoToBookmark;
bmrkName := bookMarkName;
self.wordApp.Selection.GoTo(what,b,c,bmrkName);
self.wordApp.ActiveDocument.Bookmarks.DefaultSorting := wdSortByName;
self.wordApp.ActiveDocument.Bookmarks.ShowHidden := False
end;

procedure TMyWordDocument.newDocument(templateName: String);
var
Template, NewTemplate, documentType, visible: OleVariant;
begin
//打开新文档
if(templateName = '') then
begin
template := EmptyParam;
end
else
begin
template := templateName;
end;

NewTemplate := false;
visible := true;
self.wordDoc := self.wordApp.Documents.add(template, newTemplate, documentType, visible);
self.wordDoc.Select;
self.wordApp.Options.CheckSpellingAsYouType := False;
self.wordApp.Options.CheckGrammarAsYouType := False;
end;

procedure TMyWordDocument.openDocument(fileName: String);
begin
self.wordApp.Documents.open(fileName);
end;

procedure TMyWordDocument.openWord;
begin
try
self.wordApp := CreateOleObject('Word.Application');
self.wordApp.Visible:=true;
//self.wordApp.Connect;
except
MessageDlg('Word may not be installed', mtError, [mbOk], 0);
Abort;
end;
self.wordApp.Visible := True;

end;

procedure TMyWordDocument.ReplaceAll(findText, replaceWith: String);
var
tmpFindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike,
MatchAllWordForms, Forward, Wrap, Format, tmpReplaceWith, Replace, a, b ,c , d: OleVariant;
begin
tmpFindText := findText;
MatchCase := False;
MatchWholeWord := True;
MatchWildcards := False;
MatchSoundsLike := False;
MatchAllWordForms := False;
Forward := True;
Wrap := wdFindContinue;
Format := False;
tmpReplaceWith := replaceWith;
Replace := wdReplaceAll;
self.wordDoc.Range.Find.Execute(FindText, MatchCase, MatchWholeWord,
MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward,
Wrap, Format, ReplaceWith, Replace, a, b, c, d );
end;

procedure TMyWordDocument.ReplaceNext(findText, replaceWith: String);
var
tmpFindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike,
MatchAllWordForms, Forward, Wrap, Format, tmpReplaceWith, Replace, a, b ,c , d: OleVariant;
begin
tmpFindText := findText;
MatchCase := False;
MatchWholeWord := True;
MatchWildcards := False;
MatchSoundsLike := False;
MatchAllWordForms := False;
Forward := True;
Wrap := wdFindContinue;
Format := False;
tmpReplaceWith := replaceWith;
Replace := True;
self.wordDoc.Range.Find.Execute(FindText, MatchCase, MatchWholeWord,
MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward,
Wrap, Format, ReplaceWith, Replace, a, b, c, d );
end;

procedure TMyWordDocument.saveAs(saveToFileName: String);
begin
self.wordDoc.saveAs(saveToFileName);
end;

procedure TMyWordDocument.setWordApp(wa: Variant);
begin
self.wordApp := wa;
end;

procedure TMyWordDocument.setWordDoc(wd: Variant);
begin
self.wordDoc := wd;
end;
end.

这是调用的代码
var
myDoc : TMyWordDocument;
begin
myDoc := TMyWordDocument.create;
myDoc.openWord;
myDoc.newDocument('D:/Project/DelphiProject/Word控制/test.dot');
myDoc.goToMark('bookmark_cachet');
myDoc.addPic('D:/Project/DelphiProject//Resource/jordan.jpg',myDoc.getCursorVerticalPosition,myDoc.getCursorHorizontalPosition,50,50,4);

帮帮忙,谢谢了。 我现在好像只能加200,如果解决,我愿意再出300分。 决不食言
 
你只需要关注这几个方法和调用代码就行

//让光标移动到标签处
procedure goToMark(bookmarkName:String);
//插入图片
procedure addPic(picFileName:String; picTop, picLeft, picWidth, picHeight:Integer; wrapType:Integer);
//取光标横座标值
function getCursorHorizontalPosition:Integer;
//取光标纵座标值
function getCursorVerticalPosition:Integer;
 
代码太长
 
我不是说了吗

你只需要关注这几个方法和调用代码就行

//让光标移动到标签处
procedure goToMark(bookmarkName:String);
//插入图片
procedure addPic(picFileName:String; picTop, picLeft, picWidth, picHeight:Integer; wrapType:Integer);
//取光标横座标值
function getCursorHorizontalPosition:Integer;
//取光标纵座标值
function getCursorVerticalPosition:Integer;
 
顶!!!
 
现在这个问题我已经通过一种变通的方式解决了。 只是我还不知道‘正道’是什么。为什么我问的问题来回贴的人这么少。 郁闷 ing
 
帮顶!

╭=========================================╮

80G海量源代码,控件,书籍全免费狂下不停!

http://www.source520.com

╰=========================================╯
 
我说老大,你是怎么解决的,能不能邦一把,我的需求跟你完全一样
能把你的代码发一份给我嘛
QQ:68827171
E_Mail: cszgdzx@163.com
 
顶部