-
February 25th, 2008, 05:00 AM
#1
Word automation inserting tables etc ??
Hai,
via my applicaiton, I wanted to create an MS word document displaying some images and some text....
My search led me to the concept called word automation. i browsed thru many sites describing me to add typelib etc
I tried many of them without any success
untill finally i came to the link
http://www.codeguru.com/cpp/com-tech...le.php/c14519/
Here the author has created a wrapper class... that uses something like
IDispatch* and invoke commands
I am currently using this wrapper class.
A code snippet to insert the text using the wrapper class would lok something like this
Please note that the functions described here are funcitons of my wrapper class.....
/////////// Code begins....
IDispatch* m_pdispWordApp; // have also initialized it and opened a word document...
VARIANTARG varg1,varg2;
if (!WordInvoke(m_pdispWordApp, L"Selection", &varg1, DISPATCH_PROPERTYGET, 0))
return FALSE;
ClearAllArgs();
AddArgumentCString(L"Text", 0, _T("Heading one..."));
if (!WordInvoke(varg1.pdispVal, L"TypeText",&varg2 , DISPATCH_METHOD, 0))
return FALSE;
///////////// Code ends..
The above code works fine....
Indirectly this would run the macro
Selection.TypeText Text:="Screen ID no - "Heading one..."
and to insert an image we need to run the macro
Selection.InlineShapes.AddPicture FileName:= "C:\Template.bmp"
, LinkToFile:=False, SaveWithDocument:=True
So the code would look like this
///////// code begins
if (!WordInvoke(m_pdispWordApp, L"Selection", &varg1, DISPATCH_PROPERTYGET, 0))
return FALSE;
ClearAllArgs();
if (!WordInvoke(varg1.pdispVal, L"InlineShapes", &varg2, DISPATCH_PROPERTYGET, 0))
return FALSE;
ClearAllArgs();
if(!AddArgumentCString(L"FileName", 0, szFileName))
return FALSE;
if(!AddArgumentBool(L"LinkToFile", 0, FALSE))
return FALSE;
if(!AddArgumentBool(L"SaveWithDocument", 0, TRUE))
return FALSE;
if (!WordInvoke(varg2.pdispVal, L"AddPicture", NULL, DISPATCH_METHOD, 0))
return FALSE;
/////////// Code ends.....
The above code works fine....and inserts an image on the page...
Now based on the working code above , I want to set the style of the heading .. the Script would be something like this ....
/// Script Begins
Selection.Style = ActiveDocument.Styles("Heading 2") // How to do this ?
Selection.TypeText Text:="This is a Heading..." // this is done in my code....
/// Script Ends
Also i need to create a table but am stuck
the macro would be something like this
/// Script Begins
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=3, NumColumns:= 2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= wdAutoFitFixed
/// Script Ends
I am new to automation and hope you would explain with respect to the wrapper class...
Using visual studio 2005
Office 2003
wrapper class from the lnk found in codeguru
http://www.codeguru.com/cpp/com-tech...le.php/c14519/
Thanks in anticipation
Softy
-
February 25th, 2008, 05:21 AM
#2
Re: Word automation inserting tables etc ??
the pseudo-class is just a copy from :
http://support.microsoft.com/kb/216686/en-us
You can do everything with autowrap()...
-
February 25th, 2008, 08:14 AM
#3
Re: Word automation inserting tables etc ??
Thank you fred,
But I couldnt find how to use the link, i read it but couldnt folow it.
The best help you can do is that to describe things with respect to the wrapper class that i have mentioned......
Thanks in anticipation
softy
-
February 25th, 2008, 10:52 PM
#4
Re: Word automation inserting tables etc ??
hai gode curu members....
I finally managed to fix the issue.... Thank you all once agian...
Solution : just in case if it would help someone...
To run a macro based on my wrapper class
// To Run the following macro...
Selection.Style = ActiveDocument.Styles("Heading 2")
this is same as
Selection.Style = -3
as Heading 2 is a constant having value -3
The code would be
// Code begins ...
int myHeadingStyle = -3; // Ms Word Contant for Heading Level 2
ClearAllArgs();
if (!WordInvoke(m_pdispWordApp, L"Selection", &varg1, DISPATCH_PROPERTYGET, 0))
return FALSE;
ClearAllArgs();
if (!AddArgumentInt2(NULL, 0, myHeadingStyle))
return FALSE;
if (!WordInvoke(varg1.pdispVal, L"Style",&varg2 , DISPATCH_PROPERTYPUT, 0))
return FALSE;
/// Similarly adding a table
int wdWord9TableBehavior = 1; // ms word constant
int wdAutoFitFixed = 0; // ms word constant
if (!WordInvoke(m_pdispWordApp, L"Selection", &varg1, DISPATCH_PROPERTYGET, 0))
return FALSE;
ClearAllArgs();
if (!WordInvoke(varg1.pdispVal, L"Range", &varg2, DISPATCH_PROPERTYGET, 0))
return FALSE;
ClearAllArgs();
VARIANTARG vargTableAdd,vargRange;
if (!WordInvoke(m_pdispWordApp, L"ActiveDocument", &vargTableAdd, DISPATCH_PROPERTYGET, 0))
return FALSE;
ClearAllArgs();
if (!WordInvoke(vargTableAdd.pdispVal, L"Tables",&vargRange , DISPATCH_PROPERTYGET, 0))
return FALSE;
ClearAllArgs();
if (!AddArgumentDispatch(L"Range", 0, varg2.pdispVal))
return FALSE;
if (!AddArgumentInt2(L"NumRows", 0, 2))
return FALSE;
if (!AddArgumentInt2(L"NumColumns", 0, 2))
return FALSE;
if (!AddArgumentInt2(L"DefaultTableBehavior", 0, wdWord9TableBehavior))
return FALSE;
if (!AddArgumentInt2(L"AutoFitBehavior", 0, wdAutoFitFixed ))
return FALSE;
if (!WordInvoke(vargRange.pdispVal, L"Add",&varg3 , DISPATCH_METHOD, 0 ))
return FALSE;
ReleaseVariant(&vargTableAdd);
ReleaseVariant(&vargRange);
// Code ends....
Thanks once again...
Softy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|