Automation is a process that allows applications that are written in languages such as Visual Basic .NET or C# to programmatically control other applications. Automation to Word allows you to perform actions such as creating new documents, adding text to documents, mail merge, and formatting documents. With Word and other Microsoft Office applications, virtually all of the actions that you can perform manually through the user interface can also be performed programmatically by using automation. Word exposes this programmatic functionality through an object model. The object model is a collection of classes and methods that serve as counterparts to the logical components of Word. For example, there is an Application object, a Document object, and a Paragraph object, each of which contain the functionality of those components in Word.
You can call the interesting methods and properties that Microsoft Word provides to you to manipulate documents in Word. The best way to learn how to navigate the object models of Word, Excel, and PowerPoint is to use the Macro Recorder in these Office applications:
1. Choose Record New Macro from the Macro option on the Tools menu and execute the task you’re interested in.
2. Choose Stop Recording from the Macro option on the Tools menu.
3. Once you are done recording, choose Macros from the Macro option on the Tools menu, select the macro you recorded, then click Edit.
This takes you to the generated VBA code that accomplishes the task you recorded. Keep in mind that the recorded macro will not be the best possible code in most cases, but it provides a quick and usable example.
For example to open an existing file and append some text:
object fileName = “c:\\test.doc”;
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
Word.ApplicationClass oWordApp = new Word.ApplicationClass();
EDrawOfficeViewer1.Open(ref fileName, ref missing, ref missing);
Word.ApplicationClass oWordApp = EDrawOfficeViewer1.GetApplication();
oWordApp.Selection.TypeText(“This is the text”);
oWordApp.Selection.TypeParagraph();
3 Responses to “Macro make Office Automation Easier”
Leave a Reply
You must be logged in to post a comment.
September 23rd, 2007 at 4:27 am
Office Viewer 5.2 version provides some common used interface for office automation.
/* Word Functions */
[id(72), helpstring(“Copies the whole content to the clipboard.”)]
HRESULT WordCopyToClipboard();
[id(73), helpstring(“Copies the whole content to the clipboard as picture.”)]
HRESULT WordCopyToClipboardAsPicture();
[id(74), helpstring(“Pastes to the opened Word file from the clipboard data.”)]
HRESULT WordPasteFromClipboard();
[id(75), helpstring(“Pastes to the opened Word file from the clipboard data with special format.”)]
HRESULT WordPasteSpecialFromClipboard([in] WdPasteDataType lFormatType, [in] VARIANT_BOOL vFloatOverText);
[id(76), helpstring(“Inserts a file to opened Word file.”)]
HRESULT WordInsertFile([in] BSTR FilePath);
[id(77), helpstring(“Inserts text content to opened Word file.”)]
HRESULT WordInsertText([in] BSTR Text);
[id(78), helpstring(“Inserts a picture to opened Word file.”)]
HRESULT WordInsertPicture([in] BSTR FilePath, [in] VARIANT_BOOL InlineObject);
[id(79), helpstring(“Inserts a break to opened Word file.”)]
HRESULT WordInsertBreak([in] WdBreakType BreakType);
[id(80), helpstring(“Goes to the specified item in the Word document.”)]
HRESULT WordGotoItem([in] WdGoToItem What, [in] WdGoToDirection Which, [in, optional] VARIANT Count, [in, optional] VARIANT Name);
[id(81), helpstring(“Replaces all the specified string value with another string value.”)]
HRESULT WordReplace([in] BSTR Text, [in] BSTR ReplaceText, [in] VARIANT_BOOL MatchWholeWord, [in] VARIANT_BOOL MatchCase);
[id(82), helpstring(“Compares and merges documents then displays it in the current windows.”)]
HRESULT WordMergeAndCompare([in] BSTR TargetFilePath);
September 23rd, 2007 at 4:31 am
/* Excel Functions */
[id(83), helpstring(“Adds a new worksheet to an opened Excel file.”)]
HRESULT ExcelAddWorkSheet([in] int Index);
[id(84), helpstring(“Deletes a new worksheet from an opened Excel file.”)]
HRESULT ExcelDeleteWorkSheet([in] int Index);
[id(85), helpstring(“Activates the worksheet by the index.”)]
HRESULT ExcelActivateWorkSheet([in] int Index);
[id(86), helpstring(“Returns the counts of worksheets in the opened workbook.”)]
HRESULT ExcelGetWorkSheetCount([out,retval] long* Count);
[id(87), helpstring(“Populates a specified cell with a string value.”)]
HRESULT ExcelSetCellValue([in] int Column, [in] int Row, [in] BSTR Value);
[id(88), helpstring(“Returns the contents of the specified cell.”)]
HRESULT ExcelGetCellValue([in] int Column, [in] int Row, [out,retval] BSTR* Value);
[id(89), helpstring(“Sets the height of the specified rows.”)]
HRESULT ExcelSetRowHeight([in] int Row, [in] double Height);
[id(90), helpstring(“Sets the width of the specified columns.”)]
HRESULT ExcelSetColumnWidth([in] int Column, [in] double Width);
[id(91), helpstring(“Deletes the specified row.”)]
HRESULT ExcelDeleteRow([in] int Row);
[id(92), helpstring(“Deletes the specified column.”)]
HRESULT ExcelDeleteColumn([in] int Column);
[id(93), helpstring(“Inserts a new row after the specified row.”)]
HRESULT ExcelInsertRow([in] int Row);
[id(94), helpstring(“Inserts a new column after the specified column.”)]
HRESULT ExcelInsertColumn([in] int Column);
[id(95), helpstring(“Inserts a page break after the specified row.”)]
HRESULT ExcelInsertPageBreakInRow([in] int Row);
[id(96), helpstring(“Inserts a page break after the specified column.”)]
HRESULT ExcelInsertPageBreakInColumn([in] int Column);
[id(97), helpstring(“Copies the whole sheet to clipboard.”)]
HRESULT ExcelCopyToClipboard();
[id(98), helpstring(“Inserts data to the worksheet like clipboard.”)]
HRESULT ExcelPasteStringToWorksheet([in] BSTR bstText);
September 23rd, 2007 at 4:33 am
/* SlideShow Functions */
[id(99), helpstring(“Opens then plays the PowerPoint slide show file in the same window or full screen.”)]
HRESULT SlideShowOpenAndPlay([in] VARIANT Document, [in] VARIANT_BOOL bFullScreen, [in] VARIANT_BOOL bShowWebToolbar, [in] VARIANT_BOOL bLoopUntilStopped,
[in] VARIANT_BOOL bShowScrollBar, [in, optional] VARIANT WebUsername, [in, optional] VARIANT WebPassword);
[id(100), helpstring(“Plays the PowerPoint slide show file in the same window or full screen.”)]
HRESULT SlideShowPlay([in] VARIANT_BOOL bFullScreen, [in] VARIANT_BOOL bShowWebToolbar, [in] VARIANT_BOOL bLoopUntilStopped, [in] VARIANT_BOOL bShowScrollBar);
[id(101), helpstring(“Exits the slide show play window.”)]
HRESULT SlideShowExit();
[id(102), helpstring(“Goes to the first slide in the opened PowerPoint file.”)]
HRESULT SlideGotoFirst();
[id(103), helpstring(“Goes to the previous slide in the opened PowerPoint file.”)]
HRESULT SlideGotoPrevious();
[id(104), helpstring(“Goes to the next slide in the opened PowerPoint file.”)]
HRESULT SlideGotoNext();
[id(105), helpstring(“Goes to the last slide in the opened PowerPoint file.”)]
HRESULT SlideGotoLast();
[id(106), helpstring(“Goes to the specified slide in the opened PowerPoint file based on the index.”)]
HRESULT SlideGotoPage(long nPage);
[id(107), helpstring(“Returns the count of slides.”)]
HRESULT SlideGetCount([out,retval] long* nCount);