Microsoft KB Archive/252719: Difference between revisions

From BetaArchive Wiki
m (Text replacement - ">" to ">")
m (Text replacement - """ to """)
 
(One intermediate revision by the same user not shown)
Line 91: Line 91:


   //Start Word
   //Start Word
   if(!(oWord.CreateDispatch("Word.Application", NULL)))
   if(!(oWord.CreateDispatch("Word.Application", NULL)))


   {
   {
       AfxMessageBox("Error starting Word.", MB_SETFOREGROUND);
       AfxMessageBox("Error starting Word.", MB_SETFOREGROUND);
       return;
       return;
   }
   }
Line 100: Line 100:
   //Open a document
   //Open a document
   oDocs = oWord.GetDocuments();
   oDocs = oWord.GetDocuments();
   oDoc = oDocs.Open(COleVariant("C:\\Doc1.doc"), vOpt, vOpt,  
   oDoc = oDocs.Open(COleVariant("C:\\Doc1.doc"), vOpt, vOpt,  
               vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);
               vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);


Line 106: Line 106:
   //      need to specify 12 arguments for the Open method
   //      need to specify 12 arguments for the Open method
   //  
   //  
   // oDoc = oDocs.Open(COleVariant("C:\\Doc1.doc"), vOpt, vOpt, vOpt,  
   // oDoc = oDocs.Open(COleVariant("C:\\Doc1.doc"), vOpt, vOpt, vOpt,  
   //          vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);
   //          vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);
   //
   //
Line 112: Line 112:
   //      need to specify 16 arguments for the Open method
   //      need to specify 16 arguments for the Open method
   //  
   //  
   // oDoc = oDocs.Open(COleVariant("C:\\Doc1.doc"), vOpt, vOpt, vOpt,  
   // oDoc = oDocs.Open(COleVariant("C:\\Doc1.doc"), vOpt, vOpt, vOpt,  
   //          vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt,
   //          vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt,
   //          vOpt, vOpt, 0, vOpt);
   //          vOpt, vOpt, 0, vOpt);
Line 119: Line 119:
   // library, you will need to specify 16 arguments for the Open method
   // library, you will need to specify 16 arguments for the Open method
   //  
   //  
   // oDoc = oDocs.Open(COleVariant("C:\\Doc1.doc"), vOpt, vOpt, vOpt,  
   // oDoc = oDocs.Open(COleVariant("C:\\Doc1.doc"), vOpt, vOpt, vOpt,  
   //          vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt,
   //          vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt,
   //          vOpt, vOpt, vOpt, vOpt);
   //          vOpt, vOpt, vOpt, vOpt);
Line 135: Line 135:
   //Retrieve the DISPIDs for the function as well as two of its named
   //Retrieve the DISPIDs for the function as well as two of its named
   //arguments, Printer and DoNotSetAsSysDefault
   //arguments, Printer and DoNotSetAsSysDefault
   szMethod[0]=OLESTR("FilePrintSetup"); //method name
   szMethod[0]=OLESTR("FilePrintSetup"); //method name


   szMethod[1]=OLESTR("Printer");        //argument name
   szMethod[1]=OLESTR("Printer");        //argument name
   szMethod[2]=OLESTR("DoNotSetAsSysDefault"); //argument name
   szMethod[2]=OLESTR("DoNotSetAsSysDefault"); //argument name


   hr = oWordBasic.m_lpDispatch->GetIDsOfNames(IID_NULL, szMethod, 3,  
   hr = oWordBasic.m_lpDispatch->GetIDsOfNames(IID_NULL, szMethod, 3,  
Line 150: Line 150:
   dp.cNamedArgs = 2;
   dp.cNamedArgs = 2;
   dp.rgvarg = vArgs;
   dp.rgvarg = vArgs;
   dp.rgdispidNamedArgs=&(dispid[1]);   
   dp.rgdispidNamedArgs=&(dispid[1]);   


   vArgs[1].vt = VT_I2;
   vArgs[1].vt = VT_I2;
   vArgs[1].iVal = 1;    //DoNotSetAsSysDefault = 1
   vArgs[1].iVal = 1;    //DoNotSetAsSysDefault = 1
   vArgs[0].vt = VT_BSTR;
   vArgs[0].vt = VT_BSTR;
   vArgs[0].bstrVal = ::SysAllocString(OLESTR("Generic / Text Only"));
   vArgs[0].bstrVal = ::SysAllocString(OLESTR("Generic / Text Only"));
   //NOTE: You should replace "Generic / Text Only" in the line  
   //NOTE: You should replace "Generic / Text Only" in the line  
   //above with the name of a printer installed on your system.
   //above with the name of a printer installed on your system.


   hr = oWordBasic.m_lpDispatch->Invoke(dispid[0], IID_NULL,  
   hr = oWordBasic.m_lpDispatch->Invoke(dispid[0], IID_NULL,  
               LOCALE_USER_DEFAULT,DISPATCH_METHOD, &dp, NULL, NULL, NULL);
               LOCALE_USER_DEFAULT,DISPATCH_METHOD, &dp, NULL, NULL, NULL);


   ::SysFreeString(vArgs[0].bstrVal);
   ::SysFreeString(vArgs[0].bstrVal);
Line 198: Line 198:
<li>Modify the name of the printer in the aforementioned button handler to match the name of a printer installed on your system. The printer name in the code should appear the same as it does in the Printers section of your Control Panel.</li>
<li>Modify the name of the printer in the aforementioned button handler to match the name of a printer installed on your system. The printer name in the code should appear the same as it does in the Printers section of your Control Panel.</li>
<li><p>Include the wrapper classes for the Word object model in WordPrinterDlg.cpp. For Word 97, use:</p>
<li><p>Include the wrapper classes for the Word object model in WordPrinterDlg.cpp. For Word 97, use:</p>
<pre class="codesample">#include &quot;msword8.h&quot;</pre>
<pre class="codesample">#include "msword8.h"</pre>
<p>and for Word 2000, use:</p>
<p>and for Word 2000, use:</p>
<pre class="codesample">#include &quot;msword9.h&quot;</pre>
<pre class="codesample">#include "msword9.h"</pre>
<p>and for Word 2002, Office Word 2003, or Office Word 2007, use:</p>
<p>and for Word 2002, Office Word 2003, or Office Word 2007, use:</p>
<pre class="codesample">#include &quot;msword.h&quot;</pre></li>
<pre class="codesample">#include "msword.h"</pre></li>
<li><p>Add the following line to '''CWordPrintApp::InitInstance''' in WordPrinter.cpp to initialize OLE libraries:</p>
<li><p>Add the following line to '''CWordPrintApp::InitInstance''' in WordPrinter.cpp to initialize OLE libraries:</p>
<pre class="codesample">AfxOleInit();</pre></li>
<pre class="codesample">AfxOleInit();</pre></li>
Line 209: Line 209:
For example, create one of the following Word documents:
For example, create one of the following Word documents:
<ul>
<ul>
<li>In Word 2007, create the Word document &quot;C:\doc1.docx&quot;.</li>
<li>In Word 2007, create the Word document "C:\doc1.docx".</li>
<li>In Word 2003 and in earlier versions of Word, create the Word document &quot;C:\doc1.doc&quot;.</li></ul>
<li>In Word 2003 and in earlier versions of Word, create the Word document "C:\doc1.doc".</li></ul>
</li>
</li>
<li>Build and run the application.</li>
<li>Build and run the application.</li>
Line 224: Line 224:


</div>
</div>
Documentation for WordBasic functions is not included with Word 97 and later versions of Word. WordBasic functions are documented in the &quot;Microsoft Word Developer's Kit&quot; (ISBN:1-55615-880-7) and in the Help included with Microsoft Word 95.
Documentation for WordBasic functions is not included with Word 97 and later versions of Word. WordBasic functions are documented in the "Microsoft Word Developer's Kit" (ISBN:1-55615-880-7) and in the Help included with Microsoft Word 95.


</div>
</div>

Latest revision as of 12:52, 21 July 2020

Article ID: 252719

Article Last Modified on 4/18/2007



APPLIES TO

  • Microsoft Office Word 2007
  • Microsoft Office Word 2003
  • Microsoft Word 2002 Standard Edition
  • Microsoft Word 2000 Standard Edition
  • Microsoft Word 97 Standard Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Foundation Class Library 4.2



This article was previously published under Q252719

SUMMARY

This article demonstrates how to automate Microsoft Word from an MFC Automation client, and call WordBasic functions.

MORE INFORMATION

Microsoft Word versions prior to Word 97 implemented WordBasic as their programming language. Word 97 introduced Microsoft Visual Basic for Applications (VBA) as its new standard, but for the purpose of maintaining backward compatibility, WordBasic functions can still be called. When you develop solutions for Word, Microsoft recommends that you use the Word object model instead of WordBasic, if you can. For additional information about WordBasic equivalents in the Word Object model, visit the following Microsoft Developer Network (MSDN) Web site:

Visual Basic Equivalents for WordBasic Commands
http://msdn2.microsoft.com/en-us/library/aa158432(office.10).aspx


However, if you need functionality provided by a specific WordBasic function for which you cannot find a suitable Word equivalent, then you can still call the WordBasic function.

While automating Word 97 and later versions of Word, you can execute WordBasic functions if you first acquire an interface to the WordBasic object by calling the WordBasic method of the Application class. The WordBasic method returns an object of the COleDispatchDriver class; you can then use the object that is returned to invoke WordBasic functions.

Steps to Create Sample MFC Automation Client

The following steps demonstrate how you can call the WordBasic FilePrintSetup function through Automation using named arguments.

The Word object model provides a property for changing the active printer: the ActivePrinter property. When you call the ActivePrinter property, it changes the system default printer. If you want to change only the active printer in Word and not the system default printer, you can use the WordBasic FilePrintSetup function instead.

  1. Create a new dialog-based MFC EXE project named WordPrint.
  2. Open ClassWizard (CTRL+W), click the Automation tab, click Add Class, and then select From a type library. Browse to the folder where you installed Office and select one of the following object libraries for Microsoft Word:
    • For Word 2002 and for later versions of Word, select Msword.olb.
    • For Word 2000, select Msword9.olb.
    • For Word 97, select Msword8.olb.
  3. Select all the classes that the ClassWizard displays, and click OK.
  4. Select the dialog resource IDD_WORDPRINT_DIALOG and add a button.
  5. Add a BN_CLICKED-handler for the button with the following code:

       _Application oWord ;
       Documents oDocs;
       _Document oDoc;
       COleDispatchDriver oWordBasic;
       COleVariant vOpt(DISP_E_PARAMNOTFOUND, VT_ERROR);  //For optional args
    
       //Start Word
       if(!(oWord.CreateDispatch("Word.Application", NULL)))
    
       {
          AfxMessageBox("Error starting Word.", MB_SETFOREGROUND);
          return;
       }
    
       //Open a document
       oDocs = oWord.GetDocuments();
       oDoc = oDocs.Open(COleVariant("C:\\Doc1.doc"), vOpt, vOpt, 
                  vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);
    
       // Note: If you are using the Word 2000 library, you will
       //      need to specify 12 arguments for the Open method
       // 
       // oDoc = oDocs.Open(COleVariant("C:\\Doc1.doc"), vOpt, vOpt, vOpt, 
       //           vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);
       //
       // Note: If you are using the Word 2002 library, you will
       //      need to specify 16 arguments for the Open method
       // 
       // oDoc = oDocs.Open(COleVariant("C:\\Doc1.doc"), vOpt, vOpt, vOpt, 
       //           vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt,
       //           vOpt, vOpt, 0, vOpt);
    
       // Note: If you are using the Office Word 2003 library or the Office Word 2007
       // library, you will need to specify 16 arguments for the Open method
       // 
       // oDoc = oDocs.Open(COleVariant("C:\\Doc1.doc"), vOpt, vOpt, vOpt, 
       //           vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt,
       //           vOpt, vOpt, vOpt, vOpt);
    
    
    
       oWordBasic = oWord.GetWordBasic();
    
       //Call the WordBasic FilePrintSetup function.
    
       HRESULT hr;
       OLECHAR FAR* szMethod[3];
       DISPID dispid[3];
    
       //Retrieve the DISPIDs for the function as well as two of its named
       //arguments, Printer and DoNotSetAsSysDefault
       szMethod[0]=OLESTR("FilePrintSetup"); //method name
    
       szMethod[1]=OLESTR("Printer");        //argument name
       szMethod[2]=OLESTR("DoNotSetAsSysDefault"); //argument name
    
       hr = oWordBasic.m_lpDispatch->GetIDsOfNames(IID_NULL, szMethod, 3, 
                                   LOCALE_USER_DEFAULT, dispid);
    
       //Invoke the FilePrintSetup function using named arguments.
       VARIANT vArgs[2];
       DISPPARAMS dp;
       dp.cArgs = 2;
    
       dp.cNamedArgs = 2;
       dp.rgvarg = vArgs;
       dp.rgdispidNamedArgs=&(dispid[1]);  
    
       vArgs[1].vt = VT_I2;
       vArgs[1].iVal = 1;     //DoNotSetAsSysDefault = 1
       vArgs[0].vt = VT_BSTR;
       vArgs[0].bstrVal = ::SysAllocString(OLESTR("Generic / Text Only"));
       //NOTE: You should replace "Generic / Text Only" in the line 
       //above with the name of a printer installed on your system.
    
       hr = oWordBasic.m_lpDispatch->Invoke(dispid[0], IID_NULL, 
                  LOCALE_USER_DEFAULT,DISPATCH_METHOD, &dp, NULL, NULL, NULL);
    
       ::SysFreeString(vArgs[0].bstrVal);
    
       //Print the document
       oDoc.PrintOut(COleVariant((short)false), vOpt, vOpt, 
                     vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, 
                     vOpt, vOpt, vOpt, vOpt, vOpt);
    
       // Note: If you are using the Word 2000 or the Word 2002 library, you will
       //       need to specify 19 arguments for the Printout method
       // 
       // oDoc.PrintOut(COleVariant((short)false), vOpt, vOpt, vOpt, 
       //             vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, 
       //             vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);
    
       // Note: If you are using the Office Word 2003 library, you will
       //       need to specify 18 arguments for the Printout method
       // 
       // oDoc.PrintOut(COleVariant((short)false), vOpt, vOpt, vOpt, 
       //             vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, 
       //             vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);
    
    
       //Close the document without saving changes
       oDoc.Close(COleVariant((short)false), vOpt, vOpt);
    
       //Clean-up
       oDoc.ReleaseDispatch();
       oDocs.ReleaseDispatch();
       oWordBasic.ReleaseDispatch();
    
       //Quit Word
       oWord.Quit(COleVariant((short)false), vOpt, vOpt);
                            

    NOTE: The code above uses the Word 97 object library. If you are using the object library for a later version of Word, you mustmodify the calls to Documents::Open() and Document::Printout() to include additional arguments. There are in-line comments in the code that describe the changes you should make for Word 2000, for Word 2002, for Office Word 2003, or for Office Word 2007.

  6. Modify the name of the printer in the aforementioned button handler to match the name of a printer installed on your system. The printer name in the code should appear the same as it does in the Printers section of your Control Panel.
  7. Include the wrapper classes for the Word object model in WordPrinterDlg.cpp. For Word 97, use:

    #include "msword8.h"

    and for Word 2000, use:

    #include "msword9.h"

    and for Word 2002, Office Word 2003, or Office Word 2007, use:

    #include "msword.h"
  8. Add the following line to CWordPrintApp::InitInstance in WordPrinter.cpp to initialize OLE libraries:

    AfxOleInit();
  9. Create a Word document that this Automation client can use.

    For example, create one of the following Word documents:
    • In Word 2007, create the Word document "C:\doc1.docx".
    • In Word 2003 and in earlier versions of Word, create the Word document "C:\doc1.doc".
  10. Build and run the application.
  11. When you click the button you added to the dialog, Word starts and prints the document to the printer you specified.

Additional Notes

The WordBasic functions are not defined in the object libraries for Word 97 and the later versions of Word. Therefore, the wrapper classes that the ClassWizard generates from these libraries do not contain WordBasic functions. You can view the Word 95 type library (wb70en32.tlb) in the OLE / COM Object Viewer. Please see the following article in the Microsoft Knowledge Base for information on how to obtain the Word 95 type library:

143434 WD: How to Obtain the Word for Windows Type Library


Documentation for WordBasic functions is not included with Word 97 and later versions of Word. WordBasic functions are documented in the "Microsoft Word Developer's Kit" (ISBN:1-55615-880-7) and in the Help included with Microsoft Word 95.

REFERENCES

For more information about the ActivePrinter property in Word 97 and later versions of Word, please see the following Knowledge Base article:

216026 INFO: ActivePrinter Property in Word Sets System Default Printer



(c) Microsoft Corporation 2000, All Rights Reserved. Contributions by Lori Turner, Microsoft Corporation.



Additional query words: word basic WD2007

Keywords: kbexpertiseinter kbautomation kbhowto KB252719