Solved

WordDocument, tables and Range

Posted on 2004-09-18
6
1,159 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

18 Experts available now in Live!

Get 1:1 Help Now