Word COM problem

This is a discussion on Word COM problem within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I am trying to use the Word COM to create a document, orient it to landscape, change font to ...

  1. #1
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128

    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.
    Last edited by UnclePunker; 01-06-2005 at 08:42 AM.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Woah, check out the indentation!

    Do you think you could remove the 5 or so leading tabs from each line?

  3. #3
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    Sorry about that, dint realise it before I posted, it's cut right out of the middle of my program.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    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.

  5. #5
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    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.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    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.
    Last edited by anonytmouse; 01-06-2005 at 09:54 AM.

  7. #7
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    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.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Problem either on display method or structure..
    By DarrenY in forum C Programming
    Replies: 2
    Last Post: 09-04-2005, 08:16 AM
  3. New problem [Beginner]
    By Vintik in forum C++ Programming
    Replies: 2
    Last Post: 08-18-2005, 11:33 PM
  4. Passing structures to a function
    By earth_angel in forum C++ Programming
    Replies: 5
    Last Post: 07-13-2005, 06:13 AM
  5. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 05:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21