always get an error message. "Object reference not set to an instance of an object"
Can anyone how to solve this?
Here is my code. - private void CreateNewWordFile(string sourceFileName,string destinationFileName,string stratBookmarkName,string endBookmarkName)
-
{
-
// Declaring the object variables we will need later
-
object varFileName = fileName;
-
object varFalseValue = false;
-
object varTrueValue = true;
-
object varMissing = Type.Missing;
-
// Create a reference to MS Word application
-
Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
-
-
// Creates a reference to a word document
-
Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(ref varFileName, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
-
// Activate the document
-
varDoc.Activate();
-
-
string test = varDoc.Bookmarks.Count.ToString();
-
-
//BOOK MARK FOR START OF SELECTION
-
object oBookmarkStart = "bookmark1";
-
object oRngoBookMarkStart = varDoc.Bookmarks.get_Item(ref oBookmarkStart).Range.Start;
-
-
//BOOK MARK FOR END OF SELECTION
-
object oBookmarkEnd = "bookmark2";
-
object oRngoBookMarkEnd = varDoc.Bookmarks.get_Item(ref oBookmarkEnd).Range.Start;
-
-
//SETTING THE RANGE ON THE BOOKMARK BETWEEN TWO BOOKMARKS
-
Word.Range rngBKMarkSelection = varDoc.Range(ref oRngoBookMarkStart, ref oRngoBookMarkEnd);
-
-
//SELECTING THE TEXT
-
rngBKMarkSelection.Select();
-
rngBKMarkSelection.Copy();
-
-
CreateNewDocument(destinationFileName);
-
InsertTableIntoNewDocument(destinationFileName);
-
InsertDataIntoTable(destinationFileName, rngBKMarkSelection);
-
-
//CLOSING THE FILE
-
varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
-
//QUITTING THE APPLICATION
-
varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
-
}
-
private void InsertDataIntoTable(string destinationFileName, Word.Range rngBKMarkSelection)
-
{
-
try
-
{
-
// Declaring the object variables we will need later
-
object varFileName = destinationFileName;
-
object varFalseValue = false;
-
object varTrueValue = true;
-
object varMissing = Type.Missing;
-
// Create a reference to MS Word application
-
Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
-
-
// Creates a reference to a word document
-
Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(ref varFileName, ref varMissing, ref varFalseValue, ref varMissing,
-
ref varMissing, ref varMissing, ref varMissing, ref varMissing,
-
ref varMissing, ref varMissing, ref varMissing, ref varMissing,
-
ref varMissing, ref varMissing, ref varMissing, ref varMissing);
-
// Activate the document
-
varDoc.Activate();
-
-
//error code here
-
object oUpperHeadingLevel = "1";
-
object oLowerHeadingLevel = "1";
-
object oTOCTableID = varDoc.Tables[1].ID.ToString();
-
varDoc.TablesOfContents.Add(rngBKMarkSelection, ref varTrueValue, ref oUpperHeadingLevel,
-
ref oLowerHeadingLevel, ref varMissing, ref oTOCTableID, ref varTrueValue,
-
ref varTrueValue, ref varMissing, ref varTrueValue, ref varTrueValue, ref varTrueValue);
-
//error code here
-
-
//THE LOCATION WHERE THE FILE NEEDS TO BE SAVED
-
object oSaveAsFile = destinationFileName;
-
varDoc.SaveAs(
-
ref oSaveAsFile, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
-
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
-
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
-
-
//CLOSING THE FILE
-
varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
-
//QUITTING THE APPLICATION
-
varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
-
}
-
catch (Exception varE)
-
{
-
MessageBox.Show("Error:\n" + varE.Message, "Error message");
-
}
-
}
6 2342
What is the line number?
62 to 69
Thank you very much.
Debug line by line and find the exact line number where the exception was thrown using F11.
Debug line by line and find the exact line number where the exception was thrown using F11.
Thanks for your suggestion.
I just solve the problem.
below is my source code. - private void SelectWantedData(string fileName, string startBookmark, string endBookmark)
-
{
-
try
-
{
-
// Declaring the object variables we will need later
-
object varFileName = fileName;
-
object varFalseValue = false;
-
object varTrueValue = true;
-
object varMissing = Type.Missing;
-
// Create a reference to MS Word application
-
Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
-
-
// Creates a reference to a word document
-
Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(
-
ref varFileName, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing,
-
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
-
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
-
ref varMissing);
-
// Activate the document
-
varDoc.Activate();
-
-
string test = varDoc.Bookmarks.Count.ToString();
-
-
//BOOK MARK FOR START OF SELECTION
-
object oBookmarkStart = startBookmark;
-
object oRngoBookMarkStart = varDoc.Bookmarks.get_Item(ref oBookmarkStart).Range.End;
-
-
//BOOK MARK FOR END OF SELECTION
-
object oBookmarkEnd = endBookmark;
-
object oRngoBookMarkEnd = varDoc.Bookmarks.get_Item(ref oBookmarkEnd).Range.Start;
-
-
//SETTING THE RANGE ON THE BOOKMARK BETWEEN TWO BOOKMARKS
-
Word.Range rngBKMarkSelection = varDoc.Range(ref oRngoBookMarkStart, ref oRngoBookMarkEnd);
-
-
//SELECTING THE TEXT
-
rngBKMarkSelection.Select();
-
rngBKMarkSelection.Copy();
-
-
//CLOSING THE FILE
-
varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
-
//QUITTING THE APPLICATION
-
varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
-
}
-
catch (Exception varE)
-
{
-
MessageBox.Show("Error:\n" + varE.Message, "Error message");
-
}
-
}
-
private void InsertDataIntoTable(string destinationFileName)
-
{
-
try
-
{
-
// Declaring the object variables we will need later
-
object varFileName = destinationFileName;
-
object varFalseValue = false;
-
object varTrueValue = true;
-
object varMissing = Type.Missing;
-
// Create a reference to MS Word application
-
Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
-
-
// Creates a reference to a word document
-
Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(
-
ref varFileName, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing,
-
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
-
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
-
ref varMissing);
-
// Activate the document
-
varDoc.Activate();
-
-
//paste data
-
Word.Cell cell = varDoc.Tables[1].Cell(1, 1);
-
cell.Range.Paste();
-
-
//THE LOCATION WHERE THE FILE NEEDS TO BE SAVED
-
object oSaveAsFile = destinationFileName;
-
varDoc.SaveAs(
-
ref oSaveAsFile, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
-
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
-
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
-
-
//CLOSING THE FILE
-
varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
-
//QUITTING THE APPLICATION
-
varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
-
}
-
catch (Exception varE)
-
{
-
MessageBox.Show("Error:\n" + varE.Message, "Error message");
-
}
-
}
You really should consider NOT using objects for every single variable you use.
You really should consider NOT using objects for every single variable you use.
Do you have any idea how to rewrite the codes?
The discussion is open to suggestions.
Many thanks for your replying.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: John A. |
last post by:
Hello all!
I've got a big bunch of pages using tables for layout. Eventually I'll
get them set up with more modernized code, but in the meantime I'd
like to slip in a little quick holiday decoration. I'd like to insert
a table cell with a fixed width (as much as such things can be fixed)
and a repeating garland background so as to show up along the left
side of the table, repeating down its length.
Our site has a couple thousand...
|
by: Niyazi |
last post by:
Hi,
I have to retrieve a data from AS400 DB2 and after working with data I have
to export into one of existing Excel file.
I can connect into specific library in AS400 DB2 using AS400 Client-Access
v5.2 program using (in VB.NET) ODBC driver (DSN Name …) . I can retrieve
datam work on it using VB.NET and I can send into 'NEW' Excel file.
My first problem starts here:
|
by: Philip Boonzaaier |
last post by:
I want to be able to generate SQL statements that will go through a list of
data, effectively row by row, enquire on the database if this exists in the
selected table- If it exists, then the colums must be UPDATED, if not, they
must be INSERTED.
Logically then, I would like to SELECT * FROM <TABLE>
WHERE ....<Values entered here>, and then IF FOUND
UPDATE <TABLE> SET .... <Values entered here> ELSE
INSERT INTO <TABLE> VALUES <Values...
|
by: robert |
last post by:
been ruminating on the question (mostly in a 390/v7 context) of
whether, and if so when, a row update becomes an insert/delete.
i assume that there is a threshold on the number of columns of
the table, or perhaps bytes, being updated where the engine just
decides, screw it, i'll just make a new one.
surfed this group and google, but couldn't find anything.
the context: we have some java folk who like to parametize/
|
by: Abareblue |
last post by:
I have no clue on how to insert a record into access. here is the whole
thing
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
| |
by: Bob Alston |
last post by:
I have a routine to copy data to new versions of my app via insert into
sql statements. Unfortunately, due to evolution of my app, sometimes
the new version has more restrictive editing than an older version that
I am updating. Thus I get this message. It tells me only how many
records have errors, not which errors or which records.
Anyone have a nice solution to identifying the specific records involved?
Maybe even the specific...
|
by: Giggle Girl |
last post by:
Can someone show me how to insert a row at any given row index of an
already created table? It only has to work in IE6 (used on intranet at
work).
Specifically, if a table is 20 rows in total (thought his will vary),
and someone wants to insert a new row at row 5, i need for one row to
be added, and each of the rows from 5-20 to be "cloned" to 6-21.
The contents of row 5 may vary as well; it is not a straight copy of an
exisiting row.
|
by: Marc Solé |
last post by:
Hello,
I want to insert an image to a specific column of a dataGridWiew. I explain
what I'm doing:
- I define the ColumnType as a DataGridViewImageColumn.
- I select the image (an icon of open folder).
When I accept, in the design form, appears in the ImageColumn the typical
red cross of an image not found.
|
by: L C |
last post by:
Hello,
Could someone point in the correct direction with regards to displaying my
data in "nicer" format. I have figured out ( with the tutorials on the web,
and the O'Reilly PHP & MySQL book) how to store, retrieve, manipulate, and
display my data using PHP. But when I do display, it is the block format.
I can't seem to find a tutorial that gets into a "prettier" type of display.
Is this a HTML issue?
|
by: nandan |
last post by:
Hi,
Iam using devexpress xtrareports with vb.net .I want to insert page break after writting some contents to the Detail area(band). can anybody help me?Thanks in advance
Nandan
I using the following code
Private Function drawtable()
Try
Dim table As XRTable
Dim row() As XRTableRow
Dim cell() As XRTableCell
table = New XRTable
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |