In Word 2007 I would always check whether there were any document in my current application before I call Application.ActiveDocument. This is because if you call this method and there are no documents it would throw an exception.
Now developing in Word 2010, it only gets more complicated. There is now a ActiveProtectedViewWindow as well and this document cannot be retrieved in the applications document list.
So I created a helper method to get the current active document. Here's the code
Featured Post
SQL Query in SharePoint
The "FullTextSqlQuery" object's constructor requires an object that has context but don't be fooled. This context will no...
Showing posts with label Office.Interop. Show all posts
Showing posts with label Office.Interop. Show all posts
Wednesday, April 13, 2011
Thursday, October 28, 2010
Office.Interop Tip of the week
I needed to get all content controls inside a word document and add an OnEnter and OnExit eventhandler however when you loop through the Controls in a document you are only allowed to access them as Host Controls and not Host Items so the event handlers are not accessible. Then I found out that a document contains a "ContentControlOnEnter" and "ContentControlOnExit" event handler!
Update 17 May 2010
Have just found out that you should never assign this event to a Microsoft.Office.Interop.Word.Document but rather a Microsoft.Office.Tools.Word.Document. I was using the first document and my events sometimes get removed. To get the latter document, you can use this method Microsoft.Office.Tools.Word.Document.GetVstoObject();
Update 17 May 2010
Have just found out that you should never assign this event to a Microsoft.Office.Interop.Word.Document but rather a Microsoft.Office.Tools.Word.Document. I was using the first document and my events sometimes get removed. To get the latter document, you can use this method Microsoft.Office.Tools.Word.Document.GetVstoObject();
Wednesday, October 20, 2010
Using the Office.Interop to return a list of Macros in an Word 2007 template
I struggled for hours to find a code example for this but eventually stumbled across this page: http://support.microsoft.com/kb/262961 which helped me a lot!!! I converted it to C# and modified it slightly. I have not yet fully completed my task but here is a code snippet for now - I will explain the theory behind macros next. Hope this helps someone so long:
You will need to reference the following to get this code working:
using System.Collections.Generic;
using Microsoft.Office.Interop.Word;
using Microsoft.Vbe.Interop;
public static List<string> GetMacroList(Document document)
{
List<string> macroList = new List<string>();
vbext_ProcKind prockind = vbext_ProcKind.vbext_pk_Proc;
string curMacro = string.Empty;
string newMacro = string.Empty;
foreach (VBProject pj in document.Application.VBE.VBProjects)
{
try
{
if (pj.Protection == vbext_ProjectProtection.vbext_pp_none)
{
foreach (VBComponent vbcomp in pj.VBComponents)
{
if (vbcomp.CodeModule.CountOfLines > 0)
{
for (int i = 1; i < vbcomp.CodeModule.CountOfLines - 1; i++)
{
newMacro = vbcomp.CodeModule.get_ProcOfLine(i, out prockind);
if (curMacro != newMacro)
{
curMacro = newMacro;
macroList.Add(curMacro);
}
}
}
}
}
else
{
//do not have permission - tell the user
}
}
catch
{
//catch something...
}
}
return macroList;
}
You will need to reference the following to get this code working:
using System.Collections.Generic;
using Microsoft.Office.Interop.Word;
using Microsoft.Vbe.Interop;
public static List<string> GetMacroList(Document document)
{
List<string> macroList = new List<string>();
vbext_ProcKind prockind = vbext_ProcKind.vbext_pk_Proc;
string curMacro = string.Empty;
string newMacro = string.Empty;
foreach (VBProject pj in document.Application.VBE.VBProjects)
{
try
{
if (pj.Protection == vbext_ProjectProtection.vbext_pp_none)
{
foreach (VBComponent vbcomp in pj.VBComponents)
{
if (vbcomp.CodeModule.CountOfLines > 0)
{
for (int i = 1; i < vbcomp.CodeModule.CountOfLines - 1; i++)
{
newMacro = vbcomp.CodeModule.get_ProcOfLine(i, out prockind);
if (curMacro != newMacro)
{
curMacro = newMacro;
macroList.Add(curMacro);
}
}
}
}
}
else
{
//do not have permission - tell the user
}
}
catch
{
//catch something...
}
}
return macroList;
}
Subscribe to:
Posts (Atom)
