Skrive i word2000 skabelon fra c# (har fået hul igennem til word)
Jeg har lavet en simpel klasse der haler fat i et word2000 dokument... Som det ses nedenstående bruger den ikke skabeloner, men er blot beregnet til at lave en simpel "Hello World". Den er blot indsat for at have noget at gå ud fra.Målet med projektet er at skrive data ind i formularfelter defineret i en skabelon.
point til den der kan guide mig til det er lykkedes.
using System;
using Word;
using System.Windows.Forms;
namespace LaegeMiddelKandidatDiplom
{
/// <summary>
/// Summary description for DocumentBuilder.
/// </summary>
public class DocumentBuilder
{
Word.Application wordApplication; //word applikation
Word.Documents wordDocumentCollection; //samling af dokumenter i wordapplikationen
Word.Document wordDocument; //specifikt dokument i dokumentsamlingen
Word.Range range;
object templateName = Type.Missing;
object openAsNewTemplate = Type.Missing;
object openVisible = true; //vis dokumentet
object documentType = Word.WdDocumentType.wdTypeDocument; //kunne være email type ect.
object wordStartPosition=0;
object wordEndPosition=0;
public DocumentBuilder()
{
}
public void newWord2000Application()
{
try
{
wordApplication = new Word.Application();
wordApplication.Visible=true;
}
catch (Exception ex)
{
MessageBox.Show("Error creating word application: "+ex.ToString());
}
}
public void newWord2000DocumentCollectionToApplication()
{
try
{
wordDocumentCollection = (Word.Documents)wordApplication.Documents;
}
catch (Exception ex)
{
MessageBox.Show("Error getting document collection: "+ex.ToString());
}
}
public void newWord2000DocumentToDocumentCollection()
{
try
{
wordDocument=wordDocumentCollection.Add(ref templateName, ref openAsNewTemplate, ref documentType, ref openVisible);
}
catch (Exception ex)
{
MessageBox.Show("Error adding document to documentcollection: "+ex.ToString());
}
}
public void setWordRange()
{
try
{
range = (Word.Range)wordDocument.Range(ref wordStartPosition, ref wordEndPosition);
}
catch(Exception ex)
{
MessageBox.Show("Error setting documentrange; "+ex.ToString());
}
}
public void writeText(String text, bool insertAfter)
{
try
{
if(insertAfter==true)
range.InsertAfter(text);
else
range.InsertBefore(text);
}
catch(Exception ex)
{
MessageBox.Show("Error writing text using insert: "+ex.ToString());
}
}
public void quitWord()
{
object missing = Type.Missing;
wordApplication.Quit(ref missing, ref missing, ref missing);
wordApplication=null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
