Visual C#.NET Project using Microsoft.Office.Interop.Word 12.0 (Program adds text and hyperlink to the bottom of a word document). The program works perfectly when building and running from Visual Studio on local machine and server but fails when it gets called as ColdFusion Object โ Server running Windows Server 2008 R2 running IIS and ColdFusion 9 โ winword.exe does not close and locks the file preventing it from being served for download. Here is the code: The Exception being called is at the line below "Range oRange = doc.GoTo(ref line, ref direction, ref missing, ref missing); <<<<<<EXCEPTION: OBJECT INSTANCE CANNOT BE SET TO NULL" I believe this is null because it cannot access the document. I have no idea. Any help would be appreciated.
using
System;
System.Collections.Generic;
System.Text;
Microsoft.Office.Interop.Word;
Word = Microsoft.Office.Interop.Word;
System.Reflection;
System.Runtime.InteropServices;
System.Diagnostics;
System.IO;
namespace
ReportHyperlinkClass
{
public class ReportHyperlink
//Public Fields
//Standard missing value, used for blank references
object missing = System.Reflection.Missing.Value;
//Set the default save format to .doc
object saveFormat = WdSaveFormat.wdFormatDocument97;
//Reference the full document path
object path;
//Reference the hyperlink to add to the document
object link;
//Reference the OPTIONAL text to display for hte hyperlink
object hyperlinkText;
//Reference to the meessage to display before the hyperlink
object message;
//Direct references to the Interop library
Application MSWord;
Document doc;
//Constructor for the hyperlink object
//Param: documentPath - The full document path
//Param: hyperlink - The url for this hyperlink
//Param: textForHyperlink - Text to display for hyperlink, if empty string, will display URL
public ReportHyperlink(String documentPath, String hyperlink, String messageToDisplay, String textForHyperlink)
//Assign global variables the values of parameters passed
path = documentPath;
link = hyperlink;
hyperlinkText = textForHyperlink;
message = messageToDisplay;
}
public void AddHyperlink()
//Create new Microsoft Word Application
MSWord =
new Application();
//Do not show the application
MSWord.Visible =
false;
//Object parameters for document reference
object readOnly = false;
object isVisible = false;
try
//Create a reference to the existing Word Document using the path
doc = MSWord.Documents.Open(
ref path, ref missing, ref readOnly,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref isVisible,
ref missing);
// doc.Activate();
catch (Exception e)
Console.WriteLine("Error opening word document " + e.Message);
//Set a position in the word document using a range object
//Set range object to the last line in the document
object line = Word.WdGoToItem.wdGoToLine;
object direction = Word.WdGoToDirection.wdGoToLast;
Range oRange = doc.GoTo(ref line, ref direction, ref missing, ref missing);
//Print the text to display before the link
oRange.Text = message.ToString();
//Move the cursor to the last line, past the plain text
//Collapse the entire document and insert the hyperlink at the last position
object collapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
oRange.Collapse(
ref collapseEnd);
oRange.Select();
Microsoft.Office.Interop.Word.
Hyperlink HL;
//If hyperlinkText is blank display the URL, else display the texr
if (hyperlinkText.ToString() == "")
//Add the hyperlink to the document at the current position with the URL as the default text
HL = doc.Hyperlinks.Add(oRange,
ref link, ref missing, ref missing, ref missing, ref missing);
else
//Add the hyperlink to the document at the current position with the hyperlinkText as the default text
ref link, ref missing, ref missing, ref hyperlinkText, ref missing);
HL.Range.Font.Name =
"Arial";
HL.Range.Font.Size = 12;
public void SaveAndQuit()
MSWord.DisplayAlerts = Word.
WdAlertLevel.wdAlertsNone;
//Save the document and quit the MS Word Application
doc.SaveAs(
ref path, ref saveFormat, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
((Word.
_Document)doc).Close(ref missing, ref missing, ref missing);
_Application)MSWord).Quit(ref missing, ref missing, ref missing);
//Display error if the document cannot be saved
Console.WriteLine("Unable to save document.\nIs the document already opened?\n");
Console.WriteLine(e);
Range oRange = doc.GoTo(ref line, ref direction, ref missing, ref missing); <<<<<<EXCEPTION: OBJECT INSTANCE CANNOT BE SET TO NULL