Create generic method with Word Doc or Excel Workbook parameter

mach_9

Member
Joined
May 24, 2016
Messages
20
Programming Experience
5-10
I'm new to VS and C# programming and I would like to create one generic function/method that will return a Boolean value and pass in either the Word or Excel object, what object reference would I need to use in C#? I?d prefer not to create two parameters but one.

Objects to pass in:
Word.Document myDoc= myWordApp.Documents.Open(FileName: FileName, ReadOnly: false);
Excel.Workbook myExcelWorkBook = myExcelApp.Workbooks.Open(Filename: FileName, ReadOnly: false);

I've created the method below:
public void FindExcelMacros(ref Boolean HasMacro, Excel.Workbook myExcelWorkBook)
{ if (myExcelWorkBook.HasVBProject)
{
HasMacro = true;
return;
}
HasMacro = false;
}


But would like something below:public void CheckForMacros(ref Boolean HasMacro, Excel or Word Object MsOfficeObject)
{
if (MsOfficeObject.HasVBProject)
{
HasMacro = true;
return;
}
HasMacro = false;
}

Thank you,

William
 
They have nothing in common except being objects, so you can use parameter type Object and use reflection to get the property and value (GetType/GetProperty/GetValue).
 
I've created the method below:

public Boolean CheckForMacros(Object O)
{
if (O is Excel.Workbook)
{
return (O as Excel.Workbook).HasVBProject;
}
else if (O is Word.Document)
{
return (O as Word.Document).HasVBProject;
}

return false;
}

Thanks for the help.

This can be closed.
 
Back
Top Bottom