Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

WordDocument, tables and Range

Posted on 2004-09-18
6
Medium Priority
?
1,203 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 375 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

704 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