473,656 Members | 2,793 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to insert data into a specific cell in a table?

50 New Member
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.


Expand|Select|Wrap|Line Numbers
  1. private void CreateNewWordFile(string sourceFileName,string destinationFileName,string stratBookmarkName,string endBookmarkName)
  2.         {
  3.             // Declaring the object variables we will need later
  4.             object varFileName = fileName;
  5.             object varFalseValue = false;
  6.             object varTrueValue = true;
  7.             object varMissing = Type.Missing;
  8.             // Create a reference to MS Word application
  9.             Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
  10.  
  11.             // Creates a reference to a word document
  12.             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);
  13.             // Activate the document
  14.             varDoc.Activate();
  15.  
  16.             string test = varDoc.Bookmarks.Count.ToString();
  17.  
  18.             //BOOK MARK FOR START OF SELECTION
  19.             object oBookmarkStart = "bookmark1";
  20.             object oRngoBookMarkStart = varDoc.Bookmarks.get_Item(ref oBookmarkStart).Range.Start;
  21.  
  22.             //BOOK MARK FOR END OF SELECTION
  23.             object oBookmarkEnd = "bookmark2";
  24.             object oRngoBookMarkEnd = varDoc.Bookmarks.get_Item(ref oBookmarkEnd).Range.Start;
  25.  
  26.             //SETTING THE RANGE ON THE BOOKMARK BETWEEN TWO BOOKMARKS
  27.             Word.Range rngBKMarkSelection = varDoc.Range(ref oRngoBookMarkStart, ref oRngoBookMarkEnd);
  28.  
  29.             //SELECTING THE TEXT
  30.             rngBKMarkSelection.Select();
  31.             rngBKMarkSelection.Copy();
  32.  
  33.             CreateNewDocument(destinationFileName);
  34.             InsertTableIntoNewDocument(destinationFileName);
  35.             InsertDataIntoTable(destinationFileName, rngBKMarkSelection);
  36.  
  37.             //CLOSING THE FILE
  38.             varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
  39.             //QUITTING THE APPLICATION
  40.             varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
  41.         }
  42.         private void InsertDataIntoTable(string destinationFileName, Word.Range rngBKMarkSelection)
  43.         {
  44.             try
  45.             {
  46.                 // Declaring the object variables we will need later
  47.                 object varFileName = destinationFileName;
  48.                 object varFalseValue = false;
  49.                 object varTrueValue = true;
  50.                 object varMissing = Type.Missing;
  51.                 // Create a reference to MS Word application
  52.                 Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
  53.  
  54.                 // Creates a reference to a word document
  55.                 Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(ref varFileName, ref varMissing, ref varFalseValue, ref varMissing,
  56.                                                                                        ref varMissing, ref varMissing, ref varMissing, ref varMissing,
  57.                                                                                        ref varMissing, ref varMissing, ref varMissing, ref varMissing,
  58.                                                                                        ref varMissing, ref varMissing, ref varMissing, ref varMissing);
  59.                 // Activate the document
  60.                 varDoc.Activate();
  61.  
  62.                 //error code here
  63.                 object oUpperHeadingLevel = "1";
  64.                 object oLowerHeadingLevel = "1";
  65.                 object oTOCTableID = varDoc.Tables[1].ID.ToString();
  66.                 varDoc.TablesOfContents.Add(rngBKMarkSelection, ref varTrueValue, ref oUpperHeadingLevel,
  67.                                                 ref oLowerHeadingLevel, ref varMissing, ref oTOCTableID, ref varTrueValue,
  68.                                                 ref varTrueValue, ref varMissing, ref varTrueValue, ref varTrueValue, ref varTrueValue);
  69.                 //error code here
  70.  
  71.                 //THE LOCATION WHERE THE FILE NEEDS TO BE SAVED
  72.                 object oSaveAsFile = destinationFileName;
  73.                 varDoc.SaveAs(
  74.                     ref oSaveAsFile, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
  75.                     ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
  76.                     ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
  77.  
  78.                 //CLOSING THE FILE
  79.                 varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
  80.                 //QUITTING THE APPLICATION
  81.                 varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
  82.             }
  83.             catch (Exception varE)
  84.             {
  85.                 MessageBox.Show("Error:\n" + varE.Message, "Error message");
  86.             }
  87.         }
Jul 24 '07 #1
6 2342
kenobewan
4,871 Recognized Expert Specialist
What is the line number?
Jul 24 '07 #2
vincent90152900
50 New Member
What is the line number?
62 to 69
Thank you very much.
Jul 24 '07 #3
radcaesar
759 Recognized Expert Contributor
Debug line by line and find the exact line number where the exception was thrown using F11.
Jul 24 '07 #4
vincent90152900
50 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. private void SelectWantedData(string fileName, string startBookmark, string endBookmark)
  2.         {
  3.             try
  4.             {
  5.                 // Declaring the object variables we will need later
  6.                 object varFileName = fileName;
  7.                 object varFalseValue = false;
  8.                 object varTrueValue = true;
  9.                 object varMissing = Type.Missing;
  10.                 // Create a reference to MS Word application
  11.                 Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
  12.  
  13.                 // Creates a reference to a word document
  14.                 Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(
  15.                     ref varFileName, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing,
  16.                     ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
  17.                     ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
  18.                     ref varMissing);
  19.                 // Activate the document
  20.                 varDoc.Activate();
  21.  
  22.                 string test = varDoc.Bookmarks.Count.ToString();
  23.  
  24.                 //BOOK MARK FOR START OF SELECTION
  25.                 object oBookmarkStart = startBookmark;
  26.                 object oRngoBookMarkStart = varDoc.Bookmarks.get_Item(ref oBookmarkStart).Range.End;
  27.  
  28.                 //BOOK MARK FOR END OF SELECTION
  29.                 object oBookmarkEnd = endBookmark;
  30.                 object oRngoBookMarkEnd = varDoc.Bookmarks.get_Item(ref oBookmarkEnd).Range.Start;
  31.  
  32.                 //SETTING THE RANGE ON THE BOOKMARK BETWEEN TWO BOOKMARKS
  33.                 Word.Range rngBKMarkSelection = varDoc.Range(ref oRngoBookMarkStart, ref oRngoBookMarkEnd);
  34.  
  35.                 //SELECTING THE TEXT
  36.                 rngBKMarkSelection.Select();
  37.                 rngBKMarkSelection.Copy();
  38.  
  39.                 //CLOSING THE FILE
  40.                 varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
  41.                 //QUITTING THE APPLICATION
  42.                 varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
  43.             }
  44.             catch (Exception varE)
  45.             {
  46.                 MessageBox.Show("Error:\n" + varE.Message, "Error message");
  47.             }
  48.         }     
  49.         private void InsertDataIntoTable(string destinationFileName)
  50.         {
  51.             try
  52.             {
  53.                 // Declaring the object variables we will need later
  54.                 object varFileName = destinationFileName;
  55.                 object varFalseValue = false;
  56.                 object varTrueValue = true;
  57.                 object varMissing = Type.Missing;
  58.                 // Create a reference to MS Word application
  59.                 Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
  60.  
  61.                 // Creates a reference to a word document
  62.                 Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(
  63.                     ref varFileName, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing,
  64.                     ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
  65.                     ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
  66.                     ref varMissing);
  67.                 // Activate the document
  68.                 varDoc.Activate();
  69.  
  70.                 //paste data                
  71.                 Word.Cell cell = varDoc.Tables[1].Cell(1, 1);
  72.                 cell.Range.Paste(); 
  73.  
  74.                 //THE LOCATION WHERE THE FILE NEEDS TO BE SAVED
  75.                 object oSaveAsFile = destinationFileName;
  76.                 varDoc.SaveAs(
  77.                     ref oSaveAsFile, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
  78.                     ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
  79.                     ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
  80.  
  81.                 //CLOSING THE FILE
  82.                 varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
  83.                 //QUITTING THE APPLICATION
  84.                 varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
  85.             }
  86.             catch (Exception varE)
  87.             {
  88.                 MessageBox.Show("Error:\n" + varE.Message, "Error message");
  89.             }
  90.         }
Jul 25 '07 #5
RoninZA
78 New Member
You really should consider NOT using objects for every single variable you use.
Jul 25 '07 #6
vincent90152900
50 New Member
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.
Jul 26 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
10435
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...
2
5319
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:
16
17004
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...
16
3867
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/
1
2919
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;
3
6328
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...
28
18511
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.
2
16658
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.
5
1730
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?
1
14167
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
0
8382
marktang
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...
0
8297
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,...
0
8816
Oralloy
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...
0
8717
jinu1996
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...
1
8498
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,...
0
4150
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...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.