Call COM to create a blank document and add a link

Source: Internet
Author: User

[Cpp] // AddHyperLinkViaCom. cpp: Defines the entry point for the console application. // # include "stdafx. h "HRESULT AutoWrap (int autoType, VARIANT * pvResult, IDispatch * pDisp, LPOLESTR ptName, int cArgs ...) {va_list marker; va_start (marker, cArgs); if (! PDisp) {_ exit (0) ;}dispparams dp = {NULL, NULL, 0, 0}; DISPID dispidNamed = DISPID_PROPERTYPUT; DISPID dispID; HRESULT hr; char buf [200]; char szName [200]; WideCharToMultiByte (CP_ACP, 0, ptName,-1, szName, 256, NULL, NULL); hr = pDisp-> GetIDsOfNames (IID_NULL, & ptName, 1, LOCALE_USER_DEFAULT, & dispID); if (FAILED (hr) {sprintf (buf, "IDispatch: GetIDsOfNames (\" % s \ ") failed w/err 0x % 08lx ", szName, Hr); printf ("% s \ n", buf); // _ exit (0); return hr;} VARIANT * pArgs = new VARIANT [cArgs + 1]; for (int I = 0; I <cArgs; I ++) {pArgs [I] = va_arg (marker, VARIANT);} dp. cArgs = cArgs; dp. rgvarg = pArgs; if (autoType & DISPATCH_PROPERTYPUT) {dp. cNamedArgs = 1; dp. rgdispidNamedArgs = & dispidNamed;} hr = pDisp-> Invoke (dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT, autoType, & dp, pvResult, NULL, NULL); if (FAILED (h R) {sprintf (buf, "IDispatch: Invoke (\" % s \ "= % 08lx) failed w/err 0x % 08lx", szName, dispID, hr ); printf ("% s \ n", buf); // _ exit (0); return hr;} va_end (marker); delete [] pArgs; return hr ;} int _ tmain (int argc, _ TCHAR * argv []) {HRESULT hr; CLSID wdCLSID; IDispatch * wdApp; hr = OleInitialize (NULL); if (SUCCEEDED (hr )) {CLSIDFromProgID (L "Word. application ", & wdCLSID); CoCreateInstance (wdCLSID, NULL, hr = C LSCTX_LOCAL_SERVER | CLSCTX_INPROC_SERVER, IID_IDispatch, (void **) & wdApp); if (SUCCEEDED (hr) {VARIANT x; x. vt = VT_I4; x. lVal = 1; hr = AutoWrap (DISPATCH_PROPERTYPUT, NULL, wdApp, L "Visible", 1, x); if (SUCCEEDED (hr) {IDispatch * wdDocuments; variantInit (& x); hr = AutoWrap (DISPATCH_PROPERTYGET, & x, wdApp, L "Documents", 0); if (SUCCEEDED (hr) {wdDocuments = x. pdispVal; IDispatch * wdDocument; VariantInit (& x ); VARIANT m; m. vt = VT_ERROR; m. lVal = (long) DISP_E_PARAMNOTFOUND; variant f, T; F. vt = VT_BOOL; F. boolVal = false; T. vt = VT_BOOL; T. boolVal = true; // DISPATCH_METHOD cannot be used here. DISPATCH_PROPERTYGET hr = AutoWrap (DISPATCH_METHOD, & x, wdDocuments, L "Add", 4, T, m, F, m ); if (SUCCEEDED (hr) {wdDocument = x. pdispVal; IDispatch * wdHyperLinks; IDispatch * wdRange; VariantInit (& x); hr = AutoWrap (DISPATCH_PROPERTYGET, & X, wdDocument, L "HyperLinks", 0); if (SUCCEEDED (hr) {wdHyperLinks = x. pdispVal; hr = AutoWrap (DISPATCH_METHOD, & x, wdDocument, L "Range", 2, m, m); if (SUCCEEDED (hr) {wdRange = x. pdispVal; VariantInit (& x); variant a, D, Ar;. vt = VT_BSTR;. bstrVal = SysAllocString (L "http://www.google.com"); D. vt = VT_BSTR; Ar. vt = VT_DISPATCH; Ar. pdispVal = wdRange; D. bstrVal = SysAllocString (L "google"); hr = AutoWrap (D ISPATCH_METHOD, & x, wdHyperLinks, L "Add", 6, m, D, m, m, A, Ar); if (SUCCEEDED (hr )) {printf ("Add HyperLink successfully! \ N ") ;}}}}}} system (" pause "); CoUninitialize (); return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.