Excel DocumentProperties

  • Thread starter Peter Karlström
  • Start date
P

Peter Karlström

Hi

I'm developing a COM Addin for Excel, which will be communicating data
with a document handling system using Custom Document Properties.
I've already done this in Word but now I find that it't not implemented in
Excel in the same fashion.

Is there a way to "Insert" data from the spreadsheets Custom properties
into Cells and retain the automatic updating?
I know this works in VBA creating av function which gets the propertyvalue and
return it, but is this possible in a COM Addin.
We don't want to use macros.

Thanks in advance
 
J

Jialiang Ge [MSFT]

Hello Peter,

From your post, my understanding on this issue is: you want to know how to
display a custom property value automatically when the property is changed
in a COM add-in for Excel. If I'm off base, please feel free to let me know.

I also notice that you once pasted the same question in newsgroup
microsoft.public.excel.worksheet.functions, and a community member
explained how to do it in VBA macro:
Function balance() As Variant
balance = ActiveWorkbook.CustomDocumentProperties("Balance").Value
End Function

In order to do it in COM addin, I think we could register the document open
event and update the cell's value according to the Customer Document
properties in the event handler. When an Excel workbook is opened, it is
not allowed to modify the custom document properties outside the workbook,
so there are two scenarios where we need to update the cells:
1. When a workbook is opened.
2. When the custom document property is changed inside the workbook (e.g,
in macro or COM add-in code)

For the first scenario, we could add the event handler for Workbook open:
applicationObject.WorkbookOpen += new
Microsoft.Office.Interop.Excel.AppEvents_WorkbookOpenEventHandler(applicatio
nObject_WorkbookOpen);

void applicationObject_WorkbookOpen(Microsoft.Office.Interop.Excel.Workbook
Wb)
{
DocumentProperties properties =
(DocumentProperties)Wb.CustomDocumentProperties;
if (properties["Balance"] != null)
{
Excel.Worksheet sheet1 = (Excel.Worksheet)Wb.Worksheets[1];
Excel.Range range = (Excel.Range)sheet1.Cells[1, 1];
range.Value2 = properties["Balance"].Value;
}
}
In the code above, I suppose you have a custom document property:
"Balance", and I set the cell A1 in worksheet1 as its value.

For the second scenario, we could programmatically update the cell after
the code that change the custom document property. For instance,
DocumentProperties properties =
(DocumentProperties)Wb.CustomDocumentProperties;
properties["Balance"].Value = "123"; // the "Balance" property is changed
Excel.Worksheet sheet1 = (Excel.Worksheet)Wb.Worksheets[1];
Excel.Range range = (Excel.Range)sheet1.Cells[1, 1];
range.Value2 = properties["Balance"].Value // update the cell's value
according to the new property value

Please let me know if you have any other concerns, or need anything else.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Karlström

Hi Jialiang Ge

Thanks for your reply.

This solution will work, and we will probably only update the cell-values
when the workbook is opened, since there will be a one-way communication
between Excel and the other system.

But the thing I really asked for, was if it is possible to create a function
in a
COM Addin which can be used as an "Excel-function"?
For Example; in cell B6 you type in =GetProp("balance"), where the COM Addin
function GetProp return the Custom property value or the text Missing if the
property don't exist.

And I forgot to mention earlier that I'm using VB6 in this project.

Thanks in advance

--
Peter Karlström
Midrange AB
Sweden


Jialiang Ge said:
Hello Peter,

From your post, my understanding on this issue is: you want to know how to
display a custom property value automatically when the property is changed
in a COM add-in for Excel. If I'm off base, please feel free to let me know.

I also notice that you once pasted the same question in newsgroup
microsoft.public.excel.worksheet.functions, and a community member
explained how to do it in VBA macro:
Function balance() As Variant
balance = ActiveWorkbook.CustomDocumentProperties("Balance").Value
End Function

In order to do it in COM addin, I think we could register the document open
event and update the cell's value according to the Customer Document
properties in the event handler. When an Excel workbook is opened, it is
not allowed to modify the custom document properties outside the workbook,
so there are two scenarios where we need to update the cells:
1. When a workbook is opened.
2. When the custom document property is changed inside the workbook (e.g,
in macro or COM add-in code)

For the first scenario, we could add the event handler for Workbook open:
applicationObject.WorkbookOpen += new
Microsoft.Office.Interop.Excel.AppEvents_WorkbookOpenEventHandler(applicatio
nObject_WorkbookOpen);

void applicationObject_WorkbookOpen(Microsoft.Office.Interop.Excel.Workbook
Wb)
{
DocumentProperties properties =
(DocumentProperties)Wb.CustomDocumentProperties;
if (properties["Balance"] != null)
{
Excel.Worksheet sheet1 = (Excel.Worksheet)Wb.Worksheets[1];
Excel.Range range = (Excel.Range)sheet1.Cells[1, 1];
range.Value2 = properties["Balance"].Value;
}
}
In the code above, I suppose you have a custom document property:
"Balance", and I set the cell A1 in worksheet1 as its value.

For the second scenario, we could programmatically update the cell after
the code that change the custom document property. For instance,
DocumentProperties properties =
(DocumentProperties)Wb.CustomDocumentProperties;
properties["Balance"].Value = "123"; // the "Balance" property is changed
Excel.Worksheet sheet1 = (Excel.Worksheet)Wb.Worksheets[1];
Excel.Range range = (Excel.Range)sheet1.Cells[1, 1];
range.Value2 = properties["Balance"].Value // update the cell's value
according to the new property value

Please let me know if you have any other concerns, or need anything else.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jialiang Ge [MSFT]

Hello Peter,
But the thing I really asked for, was if it is possible to create
a function in a COM Addin which can be used as an
"Excel-function"?
The answer is Yes. There are several approaches to do it.

#1. KB: HOWTO: Use a COM Add-In Function as an Excel Worksheet Function
(Please note: if your add-in is for Excel 2002 and later, please skip this
approach and see #2)
http://support.microsoft.com/kb/256624
This approach needs to create a vba wrapper for the function in add-in.
Because you do not expect to use macro, I think it does not fit your
situation.

#2. How To Create a Visual Basic Automation Add-in for Excel Worksheet
Functions
http://support.microsoft.com/kb/285337
In Microsoft Excel 2000, you cannot call a function directly in a Component
Object Model (COM) Add-in from a worksheet cell formula. Instead, you must
create a Visual Basic for Applications (VBA) wrapper for the COM Add-in
function so that the function can be called indirectly.
Integration of COM Add-ins (called Automation Add-ins) in Excel 2002 and
later has been enhanced such that COM Add-in functions may now be called
directly from a worksheet formula without the need for a VBA wrapper. This
article illustrates how to create an Automation Add-in with Visual Basic
that exposes functions that can be called from Excel 2002 and later
worksheet formulas. And I think this approach should fit your situation.
The KB has a sample writen with VB.

#3. Set a reference to the add-in in the workbook.
http://www.fontstuff.com/vba/vbatut08.htm
Although this approach does not need a VBA wrapper, we still need to do
some configurations in the workbook (setting the reference) so that the VBA
could recognize the functions defined in your COM add-in.

If you have any other concern or need anything else, please feel free to
let me know.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Karlström

Hi Jialiang Ge

Thanks a million

This is (approach #2) the one I was looking for. It seems the Volatile call
has a
lot to do with the automatic update.


Regards
--
Peter Karlström
Midrange AB
Sweden
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top