5 Replies - 5394 Views - Last Post: 12 July 2011 - 03:27 AM Rate Topic: -----

#1 aueddonline  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-July 11

creating a word doc in c sharp - new line /n /r

Posted 11 July 2011 - 02:45 PM

I'm trying to create a word document from my web application which grabs some data from my sql database and puts it in the document. Here's the code behind my button

            object missing = System.Reflection.Missing.Value;
            object Visible=true;
            object start1 = 0;
            object end1 = 0;

            string name = "edward";

            _Application WordApp = new Application();
            Document adoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
            Range rng = adoc.Range(ref start1, ref missing);

            try

            {              
                rng.Font.Name = "Verdana";
                rng.InsertAfter("Hello World!" + "\r" + "Something in-between" + "\r" + "Hello World!");

                object filename = @"C:\Users\edward\Documents\" + name + ".docx";
                adoc.SaveAs(ref filename, 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, ref missing);
                WordApp.Visible = true;
            }
            catch (Exception ex)
            {
                keeper = "Bugger";
            }



The current problem is I want a new line, but a single new line, the kind you'd get if you held down Shift while pressing the enter key in Word. I have tried /n and /r and they both seem to do the same.

Is This A Good Question/Topic? 0
  • +

Replies To: creating a word doc in c sharp - new line /n /r

#2 Momerath  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 994
  • View blog
  • Posts: 2,380
  • Joined: 04-October 09

Re: creating a word doc in c sharp - new line /n /r

Posted 11 July 2011 - 04:14 PM

IIRC Word treats a hard newline as a new paragraph. Have you checked your paragraph spacing?

I could be way off here, also :)
Was This Post Helpful? 1
  • +
  • -

#3 aueddonline  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-July 11

Re: creating a word doc in c sharp - new line /n /r

Posted 11 July 2011 - 04:51 PM

View PostMomerath, on 11 July 2011 - 04:14 PM, said:

IIRC Word treats a hard newline as a new paragraph. Have you checked your paragraph spacing?

I could be way off here, also :)


okay, so you're right. I took out 'Add space after paragraph' within my Normal.dot file (word template) and that has worked. This isn't really the way I wanted to do it because it's a web service and I don't want to change the template file on every PC I use but it's a start. And maybe the only solution
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,455
  • Joined: 08-June 10

Re: creating a word doc in c sharp - new line /n /r

Posted 11 July 2011 - 09:30 PM

According to another site I've read, word uses a vertical tab character for a line break. This is character 11 in ASCII, so you could get it like this:

char vt = (char)11;


Then add it to the document.

Also, you might look at upgrading to .NET 4. They added support for default/named parameters, as well as dynamic objects, so you don't have to fill in all the .Missing values yourself.
Was This Post Helpful? 2
  • +
  • -

#5 aueddonline  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-July 11

Re: creating a word doc in c sharp - new line /n /r

Posted 12 July 2011 - 03:19 AM

View PostCurtis Rutland, on 11 July 2011 - 09:30 PM, said:

According to another site I've read, word uses a vertical tab character for a line break. This is character 11 in ASCII, so you could get it like this:

char vt = (char)11;


Then add it to the document.

Also, you might look at upgrading to .NET 4. They added support for default/named parameters, as well as dynamic objects, so you don't have to fill in all the .Missing values yourself.


Excellent! This works perfectly.
Was This Post Helpful? 0
  • +
  • -

#6 aueddonline  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-July 11

Re: creating a word doc in c sharp - new line /n /r

Posted 12 July 2011 - 03:27 AM

Next thing I wanted to do was add some structure; headers, footers, tables, if anyone has a head start on that?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1