Solved

WordDocument, tables and Range

Posted on 2004-09-18
6
1,167 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

775 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