Article ID: 183369
Article Last Modified on 1/22/2007
Public Sub testmacro(x As String)
MsgBox "First Method" & vbcr & x
End Sub
Sub GetSetVarVals()
For Each myVar In ActiveDocument.Variables
If myVar.Name = "VarVal" Then
ActiveDocument.Variables("VarVal").Delete
End If
Next myVar
ActiveDocument.Variables.Add Name:="VarVal", _
Value:=ActiveDocument.Variables("FullName").Value
' Retrieve the contents of the document variable.
MsgBox "Second method" & _
vbcr & ActiveDocument.Variables("VarVal").Value
End Sub
Sub DelVariables() 'Delete the variable "FullName".
For Each myVar In ActiveDocument.Variables
If myVar.Name = "FullName" Then
ActiveDocument.Variables("FullName").Delete
End If
Next myVar
End Sub
178749 How To Create an Automation Project Using MFC and a Type Library
#include "msword8.h" //For Word 2000, include msword9.h. For Word 2002, include msword.h
UpdateData(TRUE); // Transfers data from edit box
// on opening dialog box
// to the member variable m_Argument.
// Convenient constants.
COleVariant covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND,
VT_ERROR);
// Objects.
_Application objWord;
Range objRange;
objWord.CreateDispatch("Word.Application");
objWord.SetVisible(TRUE);
Documents oDocs(objWord.GetDocuments()); // Use the Constructor
// for Documents.
_Document oDoc;
oDoc.AttachDispatch(oDocs.Open(COleVariant("C:\\Test.doc",VT_BSTR),
covFalse, //Confirm Conversions.
covFalse, // Not read only.
covTrue, // Add to recent documents.
covOptional, // PassWordDocument.
covOptional, // PassWordTemplate.
covFalse, // Revert.
covOptional, // WritePasswordDocument.
covOptional, // WritePasswordTemplate.
covOptional, // Format, e.g., WordOpenFormat.
covOptional, // New with Word 9 - Encoding
covOptional, // Visible - Ditto
covOptional, // New with Word 10 - OpenConflictDocument
covOptional, // Ditto - OpenAndRepair
(long) 0, // Ditto - DocumentDirection - Left to Right
covOptional // Ditto - NoEncodingDialog
)
);
// Here is code for the method that passes an argument to a Word
// VBA macro defined in the ThisDocument class. The macro specifies
// a string parameter. Such a macro won't appear in the document's
// macro list.
// The macro is not declared in the Word typelib, so the code
// retrieves the dispid at run time from the IDispatch interface
// for the Document object. It uses that dispid in the call to
// InvokeHelper.
OLECHAR FAR* szMember = OLESTR("testmacro");
DISPID dispid;
if(FAILED(oDoc.m_lpDispatch->GetIDsOfNames ( IID_NULL,
&szMember, //Function name.
1, //Number of functions.
LOCALE_SYSTEM_DEFAULT,
&dispid)))
{
AfxMessageBox("Unable to get dispID for testmacro");
}
COleVariant result;
UCHAR *parmStr = (BYTE *)( VTS_VARIANT );
COleVariant x;
x = COleVariant(m_Argument);
oDoc.InvokeHelper( dispid,
DISPATCH_METHOD | DISPATCH_PROPERTYGET,
VT_VARIANT,
(void *)&result,
parmStr,
&x);
// Here is code that passes an argument using Word variables.
// This method won't work on a protected document, because that
// won't let the user--in this case your MFC client--set
// variable values.
AfxMessageBox("Next, pass an argument using a Variable");
Variables oVariables = oDoc.GetVariables(); // Create Variables
// collection for current doc.
objWord.Run("DelVariables"); // Word Macro to purge the variable
// "FullName".
covOptional, covOptional, // Word 9 & Word 10 take
covOptional, covOptional, // up to 30 arguments
covOptional, covOptional, // in addition to Macro's name
covOptional, covOptional,covOptional, covOptional,
covOptional, covOptional,covOptional, covOptional,
covOptional, covOptional,covOptional, covOptional,
covOptional, covOptional,covOptional, covOptional,
covOptional, covOptional,covOptional, covOptional,
covOptional, covOptional,covOptional, covOptional);
VARIANT *v = new VARIANT; // Creating pointer to a VARIANT.
v->vt = VT_BSTR; // Variant type = VT_BSTR
v->bstrVal = m_Argument.AllocSysString(); //The string from Edit Box.
oVariables.Add("FullName", v); // "FullName" is the Word Variables
// item.
objWord.Run("GetSetVarVals"); // Word Macro to show the contents of
// "FullName".
covOptional, covOptional, // Word 9 & Word 10 take
covOptional, covOptional, // up to 30 arguments
covOptional, covOptional, // in addition to Macro's name
covOptional, covOptional,covOptional, covOptional,
covOptional, covOptional,covOptional, covOptional,
covOptional, covOptional,covOptional, covOptional,
covOptional, covOptional,covOptional, covOptional,
covOptional, covOptional,covOptional, covOptional,
covOptional, covOptional,covOptional, covOptional);
delete v; // Release the memory block for the Variant.
objWord.Quit(covFalse, covOptional, covFalse);
// The parameters mean Save Changes, Original Format, RouteDocument.
oDoc.AttachDispatch(oDocs.Open(
COleVariant("C:\\Test.doc",VT_BSTR)...
178784 How To Use Automation to Open and Print a Word Document
180312 How To Use Automation to Set the Printer from an MFC Project
222101 How To Find and Use Office Object Model Documentation
Additional query words: IDispatch Word 8.0 Word8 Word97
Keywords: kbautomation kbhowto kbinterop KB183369