-
Word COM problem
Hi, I am trying to use the Word COM to create a document, orient it to landscape, change font to Arial, add some merge fields, attach a data source and then save it, I have a hit a problem with the font, here is my code:
Code:
//Start Word.
if(!oApp.CreateDispatch("Word.Application",NULL))
{
AfxMessageBox("Unable to start Word.");
return;
}
//Create a new mail merge document.
oDocs = oApp.GetDocuments();
oDoc = oDocs.Add(vOpt, vOpt, vOpt, vOpt); //for Word 2000
MailMerge oMerge = oDoc.GetMailMerge();
PageSetup oSet = oDoc.GetPageSetup();
_Font oFont;
oSet.SetOrientation(1);
//Insert Fields into the document.
oMMFields = oMerge.GetFields();
Selection oSel;
oSel.TypeText("Jobname: ");
AddFieldAtSelection("jobname");
oSel = oApp.GetSelection();
oSel.TypeParagraph();
oSel.TypeText("Job No: ");
AddFieldAtSelection("jobno");
oSel = oApp.GetSelection();
oSel.TypeParagraph();
oSel.TypeText("From: ");
AddFieldAtSelection("start");
oSel.TypeText(" ");
oSel = oApp.GetSelection();
oSel.TypeText("To: ");
AddFieldAtSelection("end");
oSel = oApp.GetSelection();
oFont = oSel.GetFont();
oFont.SetName("Arial");
oFont.SetSize(24);
oSel.SetFont(oFont);
oSel.ReleaseDispatch();
oMerge.SetMainDocumentType(0); //1=wdMailingLabels
oMerge.OpenDataSource(m_dataname, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);
//oDoc.Close(vFalse, vOpt, vOpt);
//oDoc.ReleaseDispatch();
oApp.SetVisible(TRUE);
oDocs.ReleaseDispatch();
oApp.ReleaseDispatch();
this is my first time using COM and I am finding it very confusing, so please be gentle.
Cheers.
-
Woah, check out the indentation!
Do you think you could remove the 5 or so leading tabs from each line?
-
Sorry about that, dint realise it before I posted, it's cut right out of the middle of my program.
-
By default, the selection is the flashing cursor. Open up word, type some text and then change the font. Of course, the already typed text will not be changed as it is not selected. So you need to set the font before you type the text that you want it to apply to. The selection can also be expanded by calling a method such as MoveUp().
If you want to change the text for the entire document you could use Document.Range.Font instead.
You don't need to retrieve the selection object multiple times, just retrieve it once after you create the document.
-
Right, I dint really understand a lot of that, I am trying to do this from one example on microsofts site, and it's doing my head in, can anyone recommend a resource to learn COM from, 'cos diving in just aint working right now.
-
I'm not particularly familiar with the MFC COM driver, but try these modifications.
Code:
if(!oApp.CreateDispatch("Word.Application",NULL))
{
AfxMessageBox("Unable to start Word.");
return;
}
// Make word visible so we can see what is going on...
oApp.SetVisible(TRUE);
// Create a new mail merge document.
oDocs = oApp.GetDocuments();
oDoc = oDocs.Add(vOpt, vOpt, vOpt, vOpt); // for Word 2000
MailMerge oMerge = oDoc.GetMailMerge();
PageSetup oSet = oDoc.GetPageSetup();
Selection oSel = oApp.GetSelection(); // Only retrieve the selection once.
// Set the font before we start typing.
_Font oFont = oSel.GetFont();
oFont.SetName("Arial");
oFont.SetSize(24);
oSel.SetFont(oFont);
oSet.SetOrientation(1);
// Insert Fields into the document.
oMMFields = oMerge.GetFields();
oSel.TypeText("Jobname: ");
AddFieldAtSelection("jobname");
oSel.TypeParagraph();
oSel.TypeText("Job No: ");
AddFieldAtSelection("jobno");
oSel.TypeParagraph();
oSel.TypeText("From: ");
AddFieldAtSelection("start");
oSel.TypeText(" ");
oSel.TypeText("To: ");
AddFieldAtSelection("end");
oMerge.SetMainDocumentType(0); // 1 = wdMailingLabels
oMerge.OpenDataSource(m_dataname, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);
// Cleanup...
oSel.ReleaseDispatch();
oDoc.ReleaseDispatch();
oDocs.ReleaseDispatch();
oApp.ReleaseDispatch();
You may be able to find some tutorials here. Fordy may be along soon with some better advice.
-
Thanks a lot, that has helped greatly, I still don't really know what I am doing completely but I am managing to get something out of it now, I had to get rid of the oSel.SetFont(oFont); bit too, I was getting a "parameter is incorrect" error message and traced it to there, found out the Selection class doesn't have a SetFont function, doesn't help.
Thanks again, I can get it going from here methinks, no doubt I will be posting again with the same code soon.
Cheers.