Solved

WordDocument, tables and Range

Posted on 2004-09-18
6
1,176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

734 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