Project directory quandry

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
28,051
Reaction score
17,804
Word.Document wordDoc = null;
string fileName = "BakeryInvoice.dotx";
string pathtest = Path.Combine(Environment.CurrentDirectory, @"Templates\", fileName);
wordDoc = wordInstance.Documents.Add(pathtest);

pathtest shows as:

"C:\\Users\\XXX\\Desktop\\Baker Test\\ChefApp\\ChefApp\\bin\\Debug\\Templates\\BakeryInvoice.dotx"

And no matter how many different ways I flog this dead horse, it keeps saying : Object reference not set to an instance of an object on this line wordDoc = wordInstance.Documents.Add(pathtest);

Any ideas? :confused:
 
First: You don't need the back slash in @"Templates\". Path.Combine will add it for you so:
Code:
Path.Combine(Environment.CurrentDirectory, "Templates", fileName);
should work.

Second: why do you declare wordDoc on the first line and only really instantiate it on the fourth? Declare the variable where you are using it, e.g:
Code:
string fileName = "BakeryInvoice.dotx";
string pathtest = Path.Combine(Environment.CurrentDirectory, @"Templates\", fileName);
Word.Document wordDoc = wordInstance.Documents.Add(pathtest);

As for your null reference on...
Code:
wordDoc = wordInstance.Documents.Add(pathtest)
...my bet would be wordInstance

Does your debugger concur?
 
First: You don't need the back slash in @"Templates\". Path.Combine will add it for you so:
Code:
Path.Combine(Environment.CurrentDirectory, "Templates", fileName);
should work.

Second: why do you declare wordDoc on the first line and only really instantiate it on the fourth? Declare the variable where you are using it, e.g:
Code:
string fileName = "BakeryInvoice.dotx";
string pathtest = Path.Combine(Environment.CurrentDirectory, @"Templates\", fileName);
Word.Document wordDoc = wordInstance.Documents.Add(pathtest);

As for your null reference on...
Code:
wordDoc = wordInstance.Documents.Add(pathtest)
...my bet would be wordInstance

Does your debugger concur?

I got it! I was trying to set this just inside the class definition as a class variable. It would seem that setting these variables can only be set within a method. I pasted that exact code into the class constructor and it's now working fine. Strange!
 
I got it! I was trying to set this just inside the class definition as a class variable. It would seem that setting these variables can only be set within a method. I pasted that exact code into the class constructor and it's now working fine. Strange!

giphy.gif
 
^

It helps also, to have create the folder structure first, before trying to reference it :p
 
Top
Sign up to the MyBroadband newsletter
X