none
vb.net export data to ms word programmatically... RRS feed

Answers

  • Hi Aleksbey,

     

    In  a VB Word add-in created with VSTO it would look like this:

     

            Dim wApp As Word = Globals.ThisAddIn.Application

            With wApp.Selection.Range

                .Font.Name = "Arial"

                .Font.Bold = True

            End With

     

    In C# I think it would look something like this (I write in VB and used a free online converter to create the C# code, so I can’t attest to its accuracy):

     

    Word wApp = Globals.ThisAddIn.Application;

    var _with1 = wApp.Selection.Range;

    _with1.Font.Name = "Arial";

    _with1.Font.Bold = true;

     

    Hope this helps

     


    Regards
    Thursday, April 14, 2011 1:27 PM
  • Hi Aleksby

    Sorry for the delayed answer...

    <<p0.Range.Text = "Name" & ":" & lv.CheckedItems(i).subitems(0).text ' this items must be font.regular... >>

    What do you mean by "font.regular". There is no such property as "regular" in Word.

    As far as I can tell, your code should be working. Can you describe in more detail how it's NOT working? For example, your code is setting the content of p0 to have Arial 10, bold. What are you actually getting?

    Also, it's very inefficient for you to apply the same formatting over and over and over. It would make a lot more sense for you to add a STYLE to the document, set the style definition with the formatting you want to use, then assign the style to the document/paragraphs/ranges.

    Or, if the entire document should have this formatting, simply change the definition of the Normal style and the text will adapt this formatting automatically...


    Cindy Meister, VSTO/Word MVP
    • Proposed as answer by Bruce Song Tuesday, April 26, 2011 2:24 AM
    • Marked as answer by Bruce Song Tuesday, May 10, 2011 11:30 AM
    Sunday, April 24, 2011 6:19 AM
    Moderator

All replies

  • I understand your problem... u want to this

     

    Left Title (Bold, Arial)  (Tabbed) :  String (Regular, Calibri).... I understand but i dont know solve.

    Wednesday, April 13, 2011 8:45 AM
  • I understand your problem... u want to this

     

    Left Title (Bold, Arial)  (Tabbed) :  String (Regular, Calibri).... I understand but i dont know solve.

    Thanx LuthieRTR, all i want, to use diffterent font style in same paragraph when creating word document...
    Wednesday, April 13, 2011 7:10 PM
  • Hi ALEKSBEY,

    This forum is dedicated to Windows Forms technology. I'm moving this thread from Base "Windows Forms" to " Visual Studio Tools for Office " forum, since this issue is VSTO. There are more VSTO experts in that forum.

    Best Regards,


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, April 14, 2011 6:08 AM
  • Hi Aleksbey

    I'm going to move your question yet again, to the Word for Developers forum. VSTO is a specific technology that runs in-process with the Word application and I get the impression that's not what you're using...

    Before we can offer optimized suggestions, we need to know which version(s) of Word you're targeting with this?

    <<all i want, to use diffterent font style in same paragraph when creating word document...>>

    This involves working with Word RANGES. Essentially, you insert content into a Range that gets the same formatting, do the formatting, then move the Range target to the end and repeat. There are various ways to accomplish this. I use:

    Dim rng as Word.Range = wordDoc.Content
    rng.Text = "the new text"
    rng.Font.Bold = -1 'true
    rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
    rng.Text = "more text"
    rng.Font.Bold = 0
    rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
    rng.Text = "end of text" & vbCr 'new para
    rng.Font.Italic = -1

     


    Cindy Meister, VSTO/Word MVP
    Thursday, April 14, 2011 12:56 PM
    Moderator
  • Hi Aleksbey,

     

    In  a VB Word add-in created with VSTO it would look like this:

     

            Dim wApp As Word = Globals.ThisAddIn.Application

            With wApp.Selection.Range

                .Font.Name = "Arial"

                .Font.Bold = True

            End With

     

    In C# I think it would look something like this (I write in VB and used a free online converter to create the C# code, so I can’t attest to its accuracy):

     

    Word wApp = Globals.ThisAddIn.Application;

    var _with1 = wApp.Selection.Range;

    _with1.Font.Name = "Arial";

    _with1.Font.Bold = true;

     

    Hope this helps

     


    Regards
    Thursday, April 14, 2011 1:27 PM


  •  

     

    Hi Aleksbey

    I'm going to move your question yet again, to the Word for Developers forum. VSTO is a specific technology that runs in-process with the Word application and I get the impression that's not what you're using...

    Before we can offer optimized suggestions, we need to know which version(s) of Word you're targeting with this?

    <<all i want, to use diffterent font style in same paragraph when creating word document...>>

    This involves working with Word RANGES. Essentially, you insert content into a Range that gets the same formatting, do the formatting, then move the Range target to the end and repeat. There are various ways to accomplish this. I use:

    Dim rng as Word.Range = wordDoc.Content
    rng.Text = "the new text"
    rng.Font.Bold = -1 'true
    rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
    rng.Text = "more text"
    rng.Font.Bold = 0
    rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
    rng.Text = "end of text" & vbCr 'new para
    rng.Font.Italic = -1

     


    Cindy Meister, VSTO/Word MVP

    Thanx Cindy. Firstly i am sorry to writing wrong place my msg.

     

    I cant understand "range" thing...

     

    I use some codes for creating word doc... with helps this forum.

     

    My word appl... 2007. my code here:

    Imports Microsoft.Office.Interop.Word
    Imports System.Runtime.InteropServices 'I dont know why i use this.

    Private Sub xporting()

    Dim wrd As Microsoft.Office.Interop.Word.Application
    Dim docs As Microsoft.Office.Interop.Word.Document
    Dim p0 As Microsoft.Office.Interop.Word.Paragraph
    Dim p1 As Microsoft.Office.Interop.Word.Paragraph
    Dim p2 As Microsoft.Office.Interop.Word.Paragraph

    wrd = CreateObject("Word.Application")
    wrd.Visible = True
    docs = wrd.Documents.Add
    docs.PageSetup.LeftMargin = 30
    docs.PageSetup.RightMargin = 30
    docs.PageSetup.TopMargin = 30
    docs.ShowGrammaticalErrors = False

    For i = 0 To lv.CheckedItems.Count - 1

    p0 = docs.Content.Paragraphs.Add
    p0.Range.Text = "Name" & ":" & lv.CheckedItems(i).subitems(0).text <br/>  p0.Range.Font.Bold = True
    p0.Range.Font.Size = 10
    p0.Range.Font.Name = "Arial"
    p0.Format.Alignment = WdParagraphAlignment.wdAlignParagraphCenter
    p0.Format.SpaceAfter = 0
    p0.Range.InsertParagraphAfter()

    p1 = docs.Content.Paragraphs.Add
    p1.Range.Text = "SurName" & ":" & lv.CheckedItems(i).subitems(1).text
    p1.Range.Font.Bold = True
    p1.Range.Font.Size = 10
    p1.Range.Font.Name = "Arial"
    p1.Format.Alignment = WdParagraphAlignment.wdAlignParagraphCenter
    p1.Format.SpaceAfter = 0
    p1.Range.InsertParagraphAfter()

    p2 = docs.Content.Paragraphs.Add
    p2.Range.Text = "Level" & ":" & lv.CheckedItems(i).subitems(3).text
    p2.Range.Font.Bold = True
    p2.Range.Font.Size = 10
    p2.Range.Font.Name = "Arial"
    p2.Format.Alignment = WdParagraphAlignment.wdAlignParagraphCenter
    p2.Format.SpaceAfter = 0
    p2.Range.InsertParagraphAfter()
    End Sub

     p0.Range.Text = "Name" & ":" & lv.CheckedItems(i).subitems(0).text ' this items must be font.regular... 

    I cant handle this.... I want to use indent but, i cant manage anyway... So i used tab char, for the aligment.. like this

     p0.Range.Text = "Name" & TABCHR & ":" & lv.CheckedItems(i).subitems(0).text ' this items must be font.regular... 

    I am so confused.


    Thursday, April 14, 2011 6:12 PM
  • Dim rng as Word.Range = wordDoc.Content
    rng.Text = "the new text"
    rng.Font.Bold = -1 'true
    rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
    rng.Text = "more text"
    rng.Font.Bold = 0
    rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
    rng.Text = "end of text" & vbCr 'new para
    rng.Font.Italic = -1

     

    This is not solve my problem. :S Why it is so complicated?


    Monday, April 18, 2011 6:05 PM
  • Hi Aleksby

    Sorry for the delayed answer...

    <<p0.Range.Text = "Name" & ":" & lv.CheckedItems(i).subitems(0).text ' this items must be font.regular... >>

    What do you mean by "font.regular". There is no such property as "regular" in Word.

    As far as I can tell, your code should be working. Can you describe in more detail how it's NOT working? For example, your code is setting the content of p0 to have Arial 10, bold. What are you actually getting?

    Also, it's very inefficient for you to apply the same formatting over and over and over. It would make a lot more sense for you to add a STYLE to the document, set the style definition with the formatting you want to use, then assign the style to the document/paragraphs/ranges.

    Or, if the entire document should have this formatting, simply change the definition of the Normal style and the text will adapt this formatting automatically...


    Cindy Meister, VSTO/Word MVP
    • Proposed as answer by Bruce Song Tuesday, April 26, 2011 2:24 AM
    • Marked as answer by Bruce Song Tuesday, May 10, 2011 11:30 AM
    Sunday, April 24, 2011 6:19 AM
    Moderator