Showing posts with label vb.net. Show all posts
Showing posts with label vb.net. Show all posts

Tuesday, January 04, 2011

Microsoft should have a generic document in Microsoft.Office.Core

I just spent a few days trying to figure out how to modify, and maintain document attributes for arbritrary files.  It's been frustrating.

The DSO file solution (http://support.microsoft.com/kb/224351) lets you modify office files, but does not work consistently on other files.  On text files, for example, it gives the impression of adding attributes to the file, you can even read them back.  As soon as they are copied, or a new file is created for them when updating, the information dissappears. (You can still find it in the .bak file which contains the original version you modified.)

Since DSO is out, and since I am trying to stay within .net and not use Ole objects, I went in search of a general method to do this within c# or vb.  Eventually I discovered a simple methodology for word in vb.
========================= Required Imports
Imports Office = Microsoft.Office.Core
Imports Microsoft.Office.Interop

.... Required variables
    Private oDoc As Object
    Private oBuiltInProps As Object
    Private oCustomProps As Object
    Private oProp As Object

.... Required Setup
   oDoc - set to an open word document
  oBuiltInProps = oDoc.BuiltInDocumentProperties
  oCustomProps = oDoc.CustomDocumentProperties

.... Interface
oCustomProps.item(xName) = xValue -- to set custom properties
oBuiltInProps.Item(xName).Value = xValue -- set built in properties
oBuiltInProps.Item(xName).Value.ToString -- to read built in properties
oCustomProps.item(xName).value.ToString -- to read custom properties
===============================================
This can be used for any office document (tried it for excel, Outlook and will try it for all since I need them) by just changing the oDoc to be a document of the specified class (WorkBook for Excel, etc.).

Here is my flame! WHY CANT Microsoft.Office.Core HAVE A GENERIC DOCUMENT SO I DONT HAVE TO LINK AND IMPORT ALL THE INTEROPS in order for the 4 lines of code above to work accross all of Office!

Let me know if I am wrong and there is a way to do this.
Leon

Wednesday, November 12, 2008

Petzold's book Applications=Code+Markup

I was a little surprised that there was no one in the MSDN discussion group about the book.
I just bought the book, and started on chapter 1. I also downloaded the Zip files with the vb equivalents of Petzold's book.
Problems:
1. OnSession Ending does not work, so that there seemed to be no way to prevent an application from exiting when you click the "X" on the last window.
I found a workaround using a windows intercept as follows:
--------------
Public Sub WindowsClosing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
Dim x = CType(sender, Window)
If Me.Windows.Count = 1 Then
e.Cancel = MsgBox("Do you wish to close the last window and exit the program?", MsgBoxStyle.YesNo) = MsgBoxResult.No
End If
End Sub
---------------
This works but is a bit of a cludge.
=====================
2. If you use the normal Create WPF project, you must delete Application.xaml and Windows1.xaml or the code in Petzold will not work as provided. Furthermore, if you are using "STRICT" you need to fix the lack of a cast in MyWPFExtension from cType (Global.System.Windows.Application.Current,Application)What is happening here?
I need to investigate how the automatically generated code is used.