I want to use VC++.Net 2005 to automate Microsoft word.I have checked
the code available online for C# but the code could not be translated
as it is to VC++ as the compiler was giving errors for the properties
could not be found. Below is the code where i am stuck,Please help.
Object ^ missing = System::Reflection::Missing::Value;
Object ^ EndODoc = "\\endofdoc";
Word::_Application ^ myWord;// = new Word.Application();
int return_Result = 0;
// Create a word object that we can manipulate
Word::Application ^ Word_App;
Word::_Document ^ Word_doc;
try
{
Word_App = gcnew Word::Application();
Word_doc = gcnew Word::Document();
}
catch (Exception ^ e1)
{
return_Result = 1;
//goto Exit;
}
Word::AutoCorrect ^ autocorrect = Word_App->AutoCorrect;
Word::AutoCorrectEntries ^ autoEntries = autocorrect-
>Entries;
String ^ theEnd = "\nThe End";
autoEntries->Add("Introduction", "Introduction");
Word::Documents ^ Docs = Word_App->Documents;
Word_App->Visible = true;
Word::_Document ^ my_Doc = (Word::_Document ^)Word_doc;
Word_doc = Docs->Add(missing,missing,missing,missing);
Object ^start;
Object ^end;
Word::Range ^range = Word_doc->Range( missing, missing);
Word::Paragraph ^ pgs;// = gcnew Word::Paragraph();
pgs = Word_doc->Content->Paragraphs->Add(missing);
pgs->Range->Text = "Helllo";
(The Range property is present for the Paragraph class but its not
accessible.)
Ben Schwehn - 18 Sep 2007 12:10 GMT
> I want to use VC++.Net 2005 to automate Microsoft word.I have checked
> the code available online for C# but the code could not be translated
[quoted text clipped - 5 lines]
> (The Range property is present for the Paragraph class but its not
> accessible.)
What error do you get?
pgs->Range->Text = "Helllo";
doesn't even compile for me, replaced with what the compiler error (
error C3293: 'Range': use 'default' to access the default property
(indexer) for class 'Microsoft::Office::Interop::Word::Paragraph') tells
you do do:
pgs->default->default = "Helllo";
is that your problem? Works fine for me anyways (using word 11.0)
Ben
antarikshv - 18 Sep 2007 13:27 GMT
> > I want to use VC++.Net 2005 to automate Microsoft word.I have checked
> > the code available online for C# but the code could not be translated
[quoted text clipped - 20 lines]
>
> Ben
Hey Ben thanks a lot for the help. atleast now I am able to start with
putting text onto the word document. There is another solution i am
seeking. I wanted to add text into the tables and the code i am
referring for this is in C#. For your reference i am putting the code
here, but if you know how to do that then please let me know
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark
*/
The code
//Insert a 3 x 5 table, fill it with data, and make the
first row
//bold and italic.
Word.Table oTable;
Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref
oEndOfDoc).Range;
oTable = oDoc.Tables.Add(wrdRng, 3, 5, ref oMissing, ref
oMissing);
oTable.Range.ParagraphFormat.SpaceAfter = 6;
int r, c;
string strText;
for (r = 1; r <= 3; r++)
for (c = 1; c <= 5; c++)
{
strText = "r" + r + "c" + c;
oTable.Cell(r, c).Range.Text = strText;
}
oTable.Rows[1].Range.Font.Bold = 1;
oTable.Rows[1].Range.Font.Italic = 1;
Also is there any method to access the EndOfDocument property from VC+
+.Net
Ben Schwehn - 18 Sep 2007 13:36 GMT
> referring for this is in C#. For your reference i am putting the code
> here, but if you know how to do that then please let me know
What's wrong with the code?
Ben