Solved

WordDocument, tables and Range

Posted on 2004-09-18
6
1,165 Views
Last Modified: 2011-08-18
hi,
i'm trying to do the following:

 for I := 0 to something.count -1 do
    begin
      WordDocument.Tables.Add(WordDocument.Range, 1, 2, EmptyParam, EmptyParam);
        WordDocument.Tables.Item(1).Cell(1, 1).Range.Document.Shapes.AddPicture(stuff here);
        WordDocument.Tables.Item(1).Cell(1, 2).Range.InsertAfter(IntToStr(i + 1));
    end;

(just a rough sketch)

 it works fine for the first table .. but the second one (i = 1) gives me the following error:
"the range cannot be deleted"

 i tried to use WordDocument.Range.Sentences.Last in Tables.Add, but it inserts the second table inside the second cell of the first table.
any ideas how i can fix this, which range should i use to add the sencod (and third an so on..) table AFTER the other tables?

thanks in advance
0
Comment
Question by:bipbip86
  • 3
  • 3
6 Comments
 
LVL 3

Accepted Solution

by:
dlengel earned 125 total points
ID: 12093269
From the Word Visual Basic Help: "The range where you want the table to appear. The table replaces the range, if the range isn't collapsed".  You don't want to use WordDocument.Range, because even if it did work you would delete any old tables every time you inserted a new one.  In the following code, I am assuming you want a blank carriage return between each table.  

var DocumentRange: Range;

for I := 0 to something.count-1 do begin
     DocumentRange := WordDocument.Sections.First.Range;
    //go to the very end of the section and insert a carriage return
    //if the two tables are added side by side, they merge together into one
    DocumentRange.Expand(wdStory);
    DocumentRange.Collapse(wdCollapseEnd);
    DocumentRange.InsertParagraph;
    //once again go to the very end, this time add the table
    DocumentRange.Expand(wdStory);
    DocumentRange.Collapse(wdCollapseEnd);
    WordDocument.Tables.Add(myrange, 1, 2, EmptyParam, EmptyParam);
    //now you can insert your code for manipulating table cells here
end;

I haven't actually run this in Delphi, but it is a direct translation from VBA code that I tested.   There may be a more effidient way to find the end of the range, but I haven't been able to find it yet.  

Daniel Engel
0
 

Author Comment

by:bipbip86
ID: 12093805
that .. with some modifications.. has done it, thank you very much. Frankly, i couldn't find any documentation for it so it wasn't easy.
now i have a couple of more .. minor, issues:

inserting a picture,

WordDocument.Tables.Item(i+1).Cell(1, 1).Range.Document.Shapes.AddPicture
      (S, v1, v2, v3, v4, v5, v6, emptyparam);

inserts it as a *floating* image, how can i change the default setting so it is added as an inline picture..along with the text..

and second .. as you can see from the AddPicture above, i'm using separate variables for the numbers, boolean vars  and all, and then inserting them there.
if i write them directly in the function i get this error,

"Types of actual and formal var parameters must be identical"

and i can't seem to get around it yet. its not that big of a problem its just annoying.

thanks again.
0
 
LVL 3

Expert Comment

by:dlengel
ID: 12094270
The parameters to each of the functions must be OLEVariants, and they must be passed by reference, not value.  I have always done Word functions by declaring separate OLEVariants, copying the values of my parameters into those variables, and then calling the function.  It's more lines of code but is required to fulfill the declarations the way Word likes them.

The best documentation I have ever found for Microsoft Word is to load Word, record a macro that resembles the steps you want to execute, and then use the macro as a guideline for your programming.  By trial and error, I found the following code should work to initialize your image.  

  var v1, v2, v3, v4: OLEVariant;
begin
    //many lines of code.....
    v1 := 'filename'; {Image FIleName}
    v2 := False; {LinkToFile}
    v3 := True;  {SaveWithDocument}
    v4 := WordDocument.Tables.Item(i+1).Cell(1, 1).Range; {Range To Insert Into}
    WordDocument.InlineShapes.AddPicture(v1, v2, v3, v4);
    //more lines of code.....

If that doesn't work, let me know.

Daniel Engel
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:bipbip86
ID: 12095685
..mm that's what i do,

      v1 := False;
      v2 := True;
      v3 := 0;
      v4 := 0;
      v5 := 320;
      v6 := 240;
      WordDocument.Tables.Item(i+1).Cell(1, 1).Range.Document.Shapes.AddPicture
      (ImageFileName, V1, v2, v3, v4, v5, v6, emptyparam);

     but the image isn't getting insterted into the table cell, its.. *in front of the text* (the Text Wrapping proerty is set to "In front of text", in need it to be "In line with text" .
 i think the "anchor" parameter in Document.Shpaes.AddPicture has something to do with it .. but -- no documentation :).
could you check it please?
0
 
LVL 3

Expert Comment

by:dlengel
ID: 12095975
Refer back to the code in my previous post.  Use the WordDocument.InlineShapes.AddPicture procedure with the arguments that I gave in my example.  

Daniel Engel
0
 

Author Comment

by:bipbip86
ID: 12096196
excellent. thank you very much. I think i m going to write a little tutorial on all the things i've found out about manipulating ms Word, after i finish working on this applicaiton.

thanks again.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
proper way to parse text with delphi 7 99
Help on project with Soap 10 47
Firemonkey webbrowser scrollbars ? 1 29
update joined tables 2 26
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now