Hot File

A little known trick to help with MS Office automation

View: 925    Dowload: 0   Comment: 0   Post by: hanhga  
Author: kubben   Category: MS Office   Fields: Education - Schools

0 point/3 review File has been tested

Have you ever wondered how to do Microsoft Office automation? Do you have some experience with Word or Excel or some other product, but you just can’t seem to program it to do what you want? Well, I discovered a neat little trick, which I am going to share with you, that has often helped me figure out how to do Office automation.

Introduction

Background

What is Microsoft Office automation? It is the process of using OLE and COM object hooks into the Microsoft Office suite in your programming code. Why would you want to do this? I have found that often there is a need to do something or display something and it would be really handy if we could just use MS Office behind the scenes to do it. Perhaps you want some custom output in Word or Excel format. In many cases, you can use some reporting tool that will export to RTF or Excel. Still, sometimes the output you are trying to create is unique enough that you need to program it with MS Office automation.

The most recent thing I have done with MS Office automation was I used Word to add a page footer with a field that had the current date time stamp on it. My application was already using Word to print RTF documents. The RTFs were created using Crystal Reports. The users wanted a printed date on the RTF. The only way I could think of doing it was using Word Automation.

The Trick

One word: Macros. I know it sounds too easy. I found, a few years ago, when I was doing some Excel automation in a Delphi app, that I could not find any good reference on the web or a book that would help me figure out what I wanted to do. Anyway, I finally figured out that if I started to record a macro in Excel (this works in Word etc.) and did whatever I was trying to do in automation, I found that the macro code was always very close to the code I needed to write in automation to get the same results.

An Example

Here is how you start a macro in word:

Next, we will insert a date into the header while the macro is recording.

Next, we stop the macro and look at the code it created. Click on Tools > Macro > Macros and this window comes up:

Select the macro you just recorded and click Edit.

You do have to know what code you are looking for. This is the macro code we are interested in:

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldDate

The C# code looks like this:

wordApp.ActiveWindow.ActivePane.View.SeekView = 
Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter;

wordApp.Selection.Fields.Add(wordApp.Selection.Range,
ref fieldtype, ref datetime, ref isFalse);

You can see how similar the code is to the macro code. Here is the full source to add the current date-time to a Word document:

// give any file name of your choice. 
object fileName = "c:\\temp\\test.doc"; 
object  read_only  = false;
object  visible  = true;
object  isFalse    = false;

//provide a date format for the date we are inserting.
object  datetime = @"DATE \@ ""yyyy/MM/dd hh:mm:ss""";
object  fieldtype = 
Microsoft.Office.Interop.Word.WdFieldType.wdFieldDate;

//  the way to handle parameters you don't care about in .NET 
object missing = System.Reflection.Missing.Value; 

//   Open the document that was chosen by the dialog 
Microsoft.Office.Interop.Word.ApplicationClass wordApp = 
new Microsoft.Office.Interop.Word.ApplicationClass();

wordApp.Visible = false;

Microsoft.Office.Interop.Word.Document aDoc = 
wordApp.Documents.Open( 
    ref fileName, ref missing, ref read_only, 
    ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref visible, ref missing, 
        ref missing, ref missing, ref missing );

try
{
    wordApp.ActiveWindow.ActivePane.View.SeekView = 
      Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter;

    wordApp.Selection.Fields.Add(wordApp.Selection.Range,
ref fieldtype, ref datetime, ref isFalse);
    aDoc.Save();
                
}
finally
{
    aDoc.Close(ref isFalse, ref missing, ref missing);
    wordApp.Quit(ref isFalse,ref missing,ref missing);
}

Conclusion

So I suggest if you find yourself in need of using MS Office automation and you are just not sure how to do what you need to do, record a macro while you are doing it and then look at the macro code. There is a good chance that the macro code will point you in the right direction for the code you need to write.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

A little known trick to help with MS Office automation

A little known trick to help with MS Office automation Posted on 22-03-2014  Have you ever wondered how to do Microsoft Office automation? Do you have some experience with Word or Excel or some other product, but you just can’t seem to program it to do what you want? Well, I discovered a neat little trick, which I am going to share with you, that has often helped me figure out how to do Office automation. 5/10 925

Comment:

To comment you must be logged in members.

Files with category

  • Document Library - Enable New Document, Edit Document Features

    View: 2024    Download: 0   Comment: 0   Author: none  

    Document Library - Enable New Document, Edit Document Features

    Category: MS Office
    Fields: Other

    0/5 review
    In this article we can explore the underlying aspects involved in enabling the New Document feature in a Document Library.

  • Generating Excel (XML Spreadsheet) in C#

    View: 2584    Download: 2   Comment: 0   Author: none  

    Generating Excel (XML Spreadsheet) in C#

    Category: MS Office
    Fields: Other

    0.5625/8 review
    Recently, I was working on a web based report in ASP.NET. The report had to be generated in MS Excel format and the data was too much. To give you an idea, I had to generate more than 300 worksheets and the file size was more than 5 MB. I tried the...

  • Searching - Human Way and Index Way

    View: 2110    Download: 0   Comment: 0   Author: none  

    Searching - Human Way and Index Way

    Category: MS Office
    Fields: Other

    0.093023255813953/86 review
    It was really an art to optimize the data retrieval from database and data retrieval optimization is part of every developer's life. Indexes are one of the best ways of Optimization that SQL Servers provide. Understanding indexes some time may be...

  • Creating Pivot Tables and Charts Using ActiveX

    View: 2721    Download: 0   Comment: 0   Author: none  

    Creating Pivot Tables and Charts Using ActiveX

    Category: MS Office
    Fields: none

    1.125/4 review
    Most of us (developers) use the “Generate Excel” feature in our applications. This allows us to generate an Excel copy on any of our reports. Most of us are also familiar with “Pivot Table” and “Pivot Charts”. This feature from MS Excel allows the...

  • Automating MS Excel Documents

    View: 1999    Download: 0   Comment: 0   Author: none  

    Automating MS Excel Documents

    Category: MS Office
    Fields: Other

    0/3 review
    I have been working on automating MS Office applications since some time. I have seen that it is very easy to find information on automation of MS Word but it is difficult to find information on automation of PowerPoint or Excel. So I decided to...

  • Embedding PowerPoint presentation player into a WPF application

    View: 1136    Download: 0   Comment: 0   Author: none  

    Embedding PowerPoint presentation player into a WPF application

    Category: MS Office
    Fields: Other

    0/2 review
    On one occasion I came across an interesting and, I can even say, challenging task of building a customized player of PowerPoint presentations. This task emerged as part of a project which my teammates and I developed at Reliable Systems. Our...

  • Parallel Processing MS Access VBA Application

    View: 1109    Download: 0   Comment: 0   Author: none  

    Parallel Processing MS Access VBA Application

    Category: MS Office
    Fields: Other

    0/1 review
    I got a challenging assignment to provide alarm capability on top of an existing application, written in VBA Access. This required a few issues to be dealt with and resolved. I will, in this article, describe the steps I took to provide parallel...

  • Microsoft Office XML formats, defective by design?

    View: 3737    Download: 0   Comment: 0   Author: none  

    Microsoft Office XML formats, defective by design?

    Category: MS Office
    Fields: Other

    0/549 review
    Microsoft is trying to push new file formats that are using Zip and XML. Are those new file formats any good for Office developers? In other words, should anyone feel safe to make direct access to file parts, and start getting free of running...

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd