Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

How can I output my results to Microsoft Word using C# .NET?

How can I output my results to Microsoft Word using C# .NET?

Problem:

How can I output my results to Microsoft Word using C# .NET?

Resolution:


  • Product Name: CaliberRM
  • Product Version: 2005 R2, 2005 R2 SP1
  • Product Component: SDK

You will first need to add a Microsoft Word COM reference to your Project:

And include it at the top of your project like so:

����� using Microsoft.Office.Interop.Word;

You will also need add a reference to the CaliberRM .NET dll as described in the following article:

How can I access CaliberRM using the SDK with C# .NET in Microsoft Visual Studio .NET 2003?

The example code below allows you to print your results to Microsoft Word. (It is simply printing all Requirements Types and Requirements for a selected baseline):

using System;
using System.Windows.Forms;
using Starbase.CaliberRM.Interop;
using Microsoft.Office.Interop.Word;


namespace OutputReqsToWord
{

public partial class Form1 : Form
{

//Set up objects
CaliberServerFactory myFactory = new CaliberServerFactory();
CaliberServer myServer;
Project myProject;
Session mySession;
Boolean loggedIn;

Baseline myBaseline;�

RequirementTree tree;
RequirementTreeNode treeNode, treeNode2;

ApplicationClass WordApp = new ApplicationClass();

public void doLogin(String host, String username, String password)
{
try
{
//get server info
myServer = myFactory.Create(host);

//get username and password and attempt to login
mySession = myServer.login(username,password);

loggedIn = true;

txtHost.Enabled = false;
txtPassword.Enabled = false;
txtUsername.Enabled = false;
cmdLogin.Text = "Logged In";
cmdLogin.Enabled = false;

}
catch
{
loggedIn = false;
}

}


public Form1()
{
InitializeComponent();
}


private void cmdLogin_Click_1(object sender, EventArgs e)
{
doLogin(txtHost.Text, txtUsername.Text, txtPassword.Text);

if (loggedIn == true)
{
System.Console.WriteLine("Logged in");

lblProjects.Visible = true;
lblBaselines.Visible = true;
cmbProjects.Visible = true;
cmbBaselines.Visible = true;
cmdListReq.Visible = true;

int no_projects;

//count number of Projects
no_projects = mySession.Projects.Count;

//populate project combobox
for (int i = 0; i
{
myProject = (Project)mySession.Projects;
System.Console.WriteLine("Project: " + myProject.Name);
cmbProjects.Items.Add(myProject.Name);
}

}
else
{
MessageBox.Show("Incorrect Username or password","Login Failed");
}
}


private void cmbProjects_SelectedIndexChanged_1(object sender, EventArgs e)
{
//when new project is selected, clear baseline combobox
cmbBaselines.Items.Clear();
cmbBaselines.Text = (" ");

//get selected project
myProject = (Project)mySession.Projects[cmbProjects.SelectedIndex];
System.Console.WriteLine("Project Selected: " + myProject.Name);�����������

int no_baselines;

//count number of baselines
no_baselines = myProject.Baselines.Count;

//populate baseline combobox
for (int i = 0; i
{
myBaseline = (Baseline)myProject.Baselines;
cmbBaselines.Items.Add(myBaseline.Name);

if (myBaseline.Name == "Deleted View")
{
cmbBaselines.Items.Remove("Deleted View");
}

}

}


private void cmdListReq_Click_1(object sender, EventArgs e)
{

//get selected baseline
myBaseline = (Baseline)myProject.Baselines[cmbBaselines.SelectedIndex];

tree = (RequirementTree)myBaseline.RequirementTree;
treeNode = (RequirementTreeNode)tree.Root;

// Set up all the parameters as generic objects so we can pass them in Documents.Add
object fileName = "normal.dot"; // template file name
object newTemplate = false;
object docType = 0;
object isVisible = true;

// Create a new Document, by calling the Add function in the Documents collection
Document aDoc = WordApp.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);

// need to see the created document, so make it visible
WordApp.Visible = true;
aDoc.Activate();

TraverseTree(treeNode);

}


public void TraverseTree(RequirementTreeNode treeNode)
{
int no_children;

no_children = treeNode.Children.Count;

for (int i = 0; i
{
treeNode2 = (RequirementTreeNode)treeNode.Children;

//Format Output
String formatting = "";

for (int j = 1; j
{
formatting = formatting + "\t";
}

//Print to Word
WordApp.Selection.TypeText(formatting + treeNode2.HierarchyNumber + " " + treeNode2.Name + "\n");
//recursion
TraverseTree((RequirementTreeNode)treeNode2);
}

}


private void toolStripMenuItem2_Click_1(object sender, EventArgs e)
{
if (loggedIn != true)
{
System.Windows.Forms.Application.Exit();
}
else
{
mySession.logout();
System.Windows.Forms.Application.Exit();
}

}
}
}

Please�see Sample Project attached


Related documents:


Old KB# 15807

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Version history
Revision #:
1 of 1
Last update:
‎2013-02-15 18:25
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.