I'm doing a WinForm program on Visual Studio who automates the creation and automation of a MS-Project file.
I use those references :
Microsoft Office 16.0 Object Library
Microsoft Office Project 16.0 Object Library
Microsoft Project Task Launch Control
In some point in my MS-Project file, I want to customize displaying of the columns in the table.
For this purpose I use TableEditEx function. I've already made a new customized column like this :
Microsoft.Office.Interop.MSProject.Application projApp = new Microsoft.Office.Interop.MSProject.Application();
projApp.Application.SelectTaskColumn(Column: "Add New Column");
projApp.Application.TableEditEx(Name: "&Entry", TaskTable: true, NewName: "Progression", NewFieldName: "Text1", Title: "Completion", ShowInMenu: true, Width: 12);
projApp.Application.TableApply(Name: "&Entry");
And now I want to hide column "Resource Names" for example. To do so I tried the following code :
projApp.Application.SelectTaskColumn(Column: "Resource Names");
projApp.Application.TableEditEx(Name: "&Entry", TaskTable: true, Create: false, ShowInMenu: false);
projApp.Application.TableApply(Name: "&Entry");
But it doesn't appear to do anything more than selecting the column.
I've researched here :
Documentation : TableEditEx
To hide the column (not delete it), set the column width = 0:
projApp.Application.TableEditEx(Name: "&Entry", TaskTable: true, FieldName: "Resource Names", Width: 0);
projApp.Application.TableApply(Name: "&Entry");
That way, the user can unhide the column later without having to edit the table to insert it.
Okay,
I found a solution on my own. I've tried doing a macro to get the VBA code to hide a column and I got this :
SelectTaskColumn Column:="Resource Names"
ColumnDelete
And I converted it in C# code :
projApp.Application.SelectTaskColumn(Column: "Resource Names");
projApp.Application.ColumnDelete();
And it worked, thanks myself !
Related
I used COM Interop to automatically create a MS-Project file and I added few tasks in it.
I am trying to automatically add a header and a footer in this file similarly than the way it works in MS-Word as below :
foreach (Microsoft.Office.Interop.Word.Section section in myDoc.Sections)
{
Microsoft.Office.Interop.Word.Range headerRange = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
headerRange.Fields.Add(headerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage);
headerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
}
But I don't find document on the subject, is it in the least possible?
Edit
I have tried with this syntax :
Application.FilePageSetupHeader , 1, "Date: &[Date]"
But it looks like a VBA syntax and I am using C# with Interop COM.
I've tried with this instruction that I found while doing some tests :
project.Application.FilePageSetupHeader(1, PjAlignment.pjCenter, "Date");
But it alway gives me this error System.Runtime.InteropServices.COMException: 'The argument value is not valid.'
Does anybody know a clean syntax in C# or the way FilePageSetupHeader works in C# ?
The headers and footers in MS Project are rudimentary. There is a single string property for each section (left, center, right); formatting is done via format codes.
For example, this adds the date to the center header:
Application.FilePageSetupHeader , PjAlignment.pjCenter, "Date: &[Date]"
FilePageSetupHeader Documentation
FilePageSetupFooter Documentation
As all the parameters of the function FilePageSetupHeader are optionals, it seems that the first parameter, the view is required. So I had to get it like this (I have only one view in my project) :
Microsoft.Office.Interop.MSProject.Views views = project.Views;
Microsoft.Office.Interop.MSProject.View view = null;
foreach(Microsoft.Office.Interop.MSProject.View vw in views)
{
view = vw;
}
Then you can use it edit your header :
projApp.Application.FilePageSetupHeader(view, Microsoft.Office.Interop.MSProject.PjAlignment.pjCenter,
"Date: " + DateTime.Now.ToString("dd/MM/yyyy"));
Note : projApp is a Microsoft.Office.Interop.MSProject.Application object.
Microsoft.Office.Interop.Word version 14.0.0.0. .NET 4.0 VS 2010.
The MS Word API's Style class has a BaseStyle property that can be used to set the style's base (based on) style. That property works fine for me in VBA.
However from C# using Word interopt there is no BaseStyle property. However, there are two (undocumented as far as I can tell) functions set_BaseStyle() and get_BaseStyle().
When I call set_BaseStyle() I get a COMException with the message:
"This command is not available."
I think this means that the COM interface does not support the procedure (command). But why? Why does it appear in intellisense and compile? Is there a workaround?
This simple example works on my machine (VS 2012, Office 2007)
Application application = new Application {Visible = true};
string styleName1 = "Heading 1";
object styleNameObject1 = styleName1;
string styleName2 = "Heading 2";
object styleNameObject2 = styleName2;
var document = application.Documents.Add();
document.Select();
application.Selection.set_Style(ref styleNameObject2);
Style style = (Style)application.Selection.get_Style();
Style baseStyle = style.get_BaseStyle();
style.set_BaseStyle(ref styleNameObject1);
application.Selection.Range.Text = "This is the title";
application.Quit(false);
So the problem probably lies in your setup. The message is rather vague and it says word cannot do stuff, for other examples look at C# and Word2010 : DeleteAllComments throws "This command is not available." or search and replace in Word documents via .NET automation.
Is the file readonly? Does it happen with other styles or simpler files (such as my example)? Are Macros allowed in Word?
I found the problem.
The sample code posted by Vadim was a big help, as it did work, and I slowly slowly converted in to my code and eventually broke it, them moved back and forth until I homed in on the problem.
However, I can't explain what I found!
I was specifying all parameters when I opened the (existing) document with Application.Documents.Open(). It turns out that if I specify false (0) for the isvisible parameter, the code fails. If I secify true (-1) it works.
Note that in either case I can make 100s of other changes to the document. For some reason I cannot change the base style if it is invisible.
strange.
Thanks for your help.
I am currently in the need to compare two Microsoft Word documents with Microsoft.Office.Interop.Word. I've found the Application.CompareDocument method, which does exactly what I want. The following C# source code (snippet) compares a document saved in the filesystem with the current active document and opens the result in a new document:
using Word = Microsoft.Office.Interop.Word;
// [...]
Word.Document originalDocument = this.application.Documents.Open(filePath, ReadOnly: true, Visible: false);
Word.Document diffDocument = this.application.CompareDocuments(
originalDocument,
this.application.ActiveDocument);
((Word._Document)originalDocument).Close(SaveChanges: false);
// TODO Activate two built-in Microsoft Word buttons.
// [...]
But I also need to activate two built-in buttons in the view of the newly created Word document. After searching on MSDN for a while, I am unable to find a way to achieve what I want. I've added two screenshots to this question, which display the built-in buttons I want to activate (sadly, I am using the German version of Microsoft Word 2010, so I don't know what the exact translations are).
"Quelldokumente anzeigen" (could be translated as "Display source documents"). I need to activate the button "Beide anzeigen" (could be translated as "Display both").
"Überarbeitungsbereich" (could be translated as "Revision pane"). I need to activate the button "Überarbeitungsbereich vertikal..." (could be translated as "Vertical revision pane...").
To conclude, I want to know how I can modifiy the state (either directly or indirectly via an method call) of these two buttons.
EDIT (2013-08-03)
The revisions pane can be set via the following method:
diffDocument.ActiveWindow.View.SplitSpecial = Word.WdSpecialPane.wdPaneRevisionsVert;
I am still searching for a solution to display both the source document and revised document panes.
EDIT (2013-08-05)
The show source documents button can be modified to show both source documents via the following method:
diffDocument.ActiveWindow.ShowSourceDocuments = Word.WdShowSourceDocuments.wdShowSourceDocumentsBoth;
Possible solutions of your problems:
Ad 1. but first, you need to open your documents with ReadOnly: false while,according to both C# and VBA test it will not work when you set parameters to ReadOnly: true:
((Word._Document)diffDocument).Windows.CompareSideBySideWith(originalDocument);
Ad 2. this time you need to refer to window object of Word Application. Here is code for active window:
appWRD.ActiveWindow.View.SplitSpecial = Word.WdSpecialPane.wdPaneRevisionsVert;
where: appWRD is Word.Application in my code.
Ad 1 again. (one above was a result of misunderstanding).
According to some test this code should give you what you need:
appWRD.ActiveWindow.ShowSourceDocuments = Word.WdShowSourceDocuments.wdShowSourceDocumentsBoth;
I'm trying to control an Excel (2010) PivotTable from a VSTO (excel AddIn) in C# (4.0). I've got no problem adding PivotFields (Dimensions) and DataFields (Measures) to the PivotTable.
The problem is I can't remove a DataField.
My DataField is a PivotField object.
I've tried :
myDataField.Hidden = true;
myDataField.DisplayInReport = false;
myDataField.Orientation = XlPivotFieldOrientation.xlHidden;
// This last one is what I use to remove a (Dimension) PivotField
Every one of these lines throws a COM Exception with absolutely no information in it. The only thing I have is the message : "Exception de HRESULT : 0x800A03EC", which seems to be common to every VSTO exception.
If anyone has a solution, that would help me a lot.
I want to add in a C# application a bookmark to a particular range to my Word document at runtime . I have found one solution
Microsoft.Office.Tools.Word.Bookmark bookmark1;
bookmark1 = this.Controls.AddBookmark(this.Paragraphs[1].Range, "bookmark1");
But an error shows that the Windows form has no definition for AddBookmark. Please help.
You are using a WinForms application, therefore by using the keyword this it will refer to the form class you are using which does not have a definition for Controls.AddBookmark.
I suggest you take a look here: http://msdn.microsoft.com/en-us/library/cc442946.aspx
This will show you how to create a word addin from which you can then use this code to add a bookmark.
Microsoft.Office.Tools.Word.Bookmark bookmark1; bookmark1 = this.Controls.AddBookmark(this.Paragraphs[1].Range, "bookmark1");