none
Export to Word Template

    Question


  • I am trying to export to word Document (whcih I have been successful)
    BUT now want to export to a word doc template.  Something that more resembles a report. 
    Add some text and then specifiy space holders for the attribute vales that are being passed to it...

    Right now I am simply grabbing all the records from a datasource and placing them in Word...

    Anyone knwo how I can modify this to place the attributes into a Word Template (a mail merge template) that will
    allow me to position the attrbiutes into a report type form....


        Private Sub GenerateSpreadSheetWord(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Dim rowIndex As Integer = 1
            Dim coulmnIndex As Integer = 1
            Dim i As Integer = 1

            If AutomationFactory.IsAvailable Then
                Dim word As Object = AutomationFactory.CreateObject("Word.Application")
                word.Visible = True
                word.Documents.Add()
                word.Selection.TypeText("Data")
                word.Selection.TypeText(String.Format("Exported at: {0}", Date.Today.ToShortDateString()))
                word.Selection.TypeParagraph()

                For Each customer As CoreBusinessNewTwo In dataGridBusiness.ItemsSource
                    word.Selection.TypeText(String.Format("{0} " & vbVerticalTab & "SquareFeet: {1}" & vbVerticalTab, customer.BusinessType, customer.BldgSqFt & vbNewLine))
                Next customer
            End If
        End Sub


    1. Do I create the STRING different, Where it will house Varables?

    2. Do I have to create the template in advance?  How do I do this?

    Thanks

    Thursday, December 15, 2011 2:36 PM

All replies

  • Instead of pushing the values to the word doc as I have set up, which simply places the values in a word doc...Can I somehow use them to create a template doc.

    THen run the query again to populate this newly created template.

    Anyone do this before? Thoughts? Suggestions?

    How to merge the attributes in the datasource to set up the template?  And how to point to the new template?  Maybe an option when click to create template, modify template, run in existing template?

    Thursday, December 15, 2011 3:44 PM
  • You could use bookmarks which we have done successfully for some time.

    See: http://support.microsoft.com/kb/316383

    We have now moved to OpenXML but marking up a word template and table with bookmarks for substitution works fine.

    Monday, December 19, 2011 4:32 PM
  • Xpert360.....can you help me understand that a littel better...

    you use the ability to export to word as shown in the example but go to an existing template with bookmarks set up to act as variables to place specific data in the word doc?

    Can you exapling a bit more in plain english whats going on.....

    Also just trying to test this and #6 does not work...are they talking about declaring WORD in the Imports at the top?

    • Add the following code to the top of Form1.vb:
      Imports Word = Microsoft.Office.Interop.Word

    Imports System
    Imports System.Collections
    Imports Microsoft.VisualBasic
    Imports System.Windows.Controls.Primitives
    Imports System.Windows.Data
    Imports System.IO

    Imports Word = Microsoft.Office.Interop.Word

    I get and error with the IMPORT Above

    THANKS

    Tuesday, January 24, 2012 12:02 PM
  • I read somewhere that I shoudl install the 2007 Microsoft Office System Update: Redistributable Primary Interop Assemblies.

    I went the site and noticed on the bottom to remove 3 files to uninstall...I looked at one and did a search on my computer and it was there....si I assume its installed?

    Also noting that I am running Windows 7 64 bit.....its this running on 32 bit for some reason and cant find it?

    http://www.microsoft.com/download/en/details.aspx?id=18346

    o2007pia.msi is on my computer....but I still get the error on the IMPORT above...am I missing a reference?  Please help

    Tuesday, January 24, 2012 5:21 PM
  • I double clicked teh msi file and installed it...BUT when I restart Visual Studio I dont see Microsoft Word Object Library....

    Anyone out there done this before?  Would be nice to get the example above working...then I can migrate into my data...

    When I start typeing Imports Word = Microsoft.   The prompt does not give me the Office as an option...Only Internal, Visual Basic, Win32, and Windows

    Thoughts?  I have to be missing something here....missing reference, something not installed, 64 bit vs 32 bit??????

    I am leaning towards a 32 bit vs 64 bit issue here BUT NO IDEA....mayeb looking in "Program Files" folder vs "Program Files (x86)"

    Wednesday, January 25, 2012 10:31 AM
  • ON A SIDE NOTE: I have foudn this option....ANYONE USE THIS OPTION BEFORE...

    WHERE CAN I FIND MORE INFO ON IT...how to insert pictures, graphs etc

    http://msdn.microsoft.com/en-us/gg699646

    Looking at the word portion I am perplexed on how to Force a Page Break....Anyone out there might have some input on this...

    HOW TO ADD A PAGE BREAK IN THE WORD EXAMPLE ABOVE.....(IT Explains word, excel outlook)  THanks

    Wednesday, January 25, 2012 11:29 AM
  • Think I got it... but looking for an answer on my last post question on where to find more info on the link I posted...on more options to control in the word creation....

    How can I add images? Add hyperlinks? graphs? etc  Where can I find more info on other parameters and other things that I can do to the word document to control its format, text size etc...

    In my last post the example:

    Where can I find more examples on    .visiable, .documents, .selection etc...where can I find all these options?  Where can I find a list showing all the parameters I can set?          

                    word.Visible = True
                    word.Documents.Add()
                    word.Selection.TypeText("Bookmarks")
                    word.Selection.TypeText(String.Format("Exported at: {0}",Date.Today.ToShortDateString()))

    word.Selection.InsertBreak()

    BUT INTERESTED IN THE EXAMPLE YOU GAVE AS WELL....hoping to hear back on that...the example you showed I can see how to change teh font size etc...I cant seem to figure that out in the example I gave...

    1. trying to figure out why I cant get your example working

    2. trying to figure out to add images, change font size, graphs etc in my example..looking for more info

    Wednesday, January 25, 2012 11:31 AM
  • This is what I have now and its working....But I want to do the 4 things below

    BUT I WANT TO ADD:
    1. Change the text size (14), type (aerial), style (bold)
    2. Add an Image
    3. Add a Hyperlink
    4. Add a Table


        Private Sub GenerateSpreadSheetWord(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Dim rowIndex As Integer = 1
            Dim coulmnIndex As Integer = 1
            Dim i As Integer = 1

            If AutomationFactory.IsAvailable Then

                Dim word As Object = AutomationFactory.CreateObject("Word.Application")
                word.Visible = True
                word.Documents.Add()

                ' This is header infromation that will only be placed on the first page.
                word.Selection.TypeText("Data")
                word.Selection.TypeText(String.Format("Exported at: {0}", Date.Today.ToShortDateString()))
                word.Selection.TypeText(vbNewLine)

                For Each customer As CoreBusinessNewTwo In dataGridBusiness.ItemsSource
                    word.Selection.TypeText("Acutal Data: " & vbNewLine)
                    word.Selection.TypeText("-----------------------------------------------------------------------------------" & vbNewLine)
                    word.Selection.TypeText(String.Format("{0} " & vbVerticalTab & "SquareFeet: {1}" & vbVerticalTab, customer.BusinessType, customer.BldgSqFt & vbNewLine))

                    ' Create Paragraph with varaibles from each record inserted into it.
                    word.Selection.TypeText(String.Format("{0}" & vbNewLine & "An automobile, {1}, motor car or car is.", customer.BusinessName, Date.Today.ToShortDateString()))

                    ' Insert Page Break after each individual record returned from the data grid.
                    word.Selection.InsertBreak()

                Next customer

            End If
        End Sub
     

    I tried something like this but get errors: Type 'word.paragraph' is not defined.

       Dim oPara1 As word.Paragraph

       'Insert a paragraph at the beginning of the document.
       oPara1 = word.Content.Paragraphs.Add
       oPara1.Range.Text = "Heading 1"
       oPara1.Range.Font.Bold = True
       oPara1.Format.SpaceAfter = 24    '24 pt spacing after paragraph.
       oPara1.Range.InsertParagraphAfter()

    Wednesday, January 25, 2012 4:07 PM
  • Anyone out there help with this? Please? 

    I think I woudl be good with the last bit of code I shown if I could add a picture, table, and change the font types, sizes etc...

    What is the "Selection" in this statement "word.Selection.TypeText"  Is it a parameter of the defined word.application?  Where can I find out more parameters and other things that I can change?  As stated above...

    PLEASE HELP!!!...THanks

    Thursday, January 26, 2012 9:28 AM
  • I am looking at this now.  I have created numerous Word addins using VBA and I am looking at doing the frontend in Silverlight.  My addins creates word docs from templates and uses bookmarks and other keywords to insert text, pictures and graphs.  I am thinking that the object model that VBA uses has got to mirror the one used in Silverlight apps.  For example the code for inserting a picture is:

          Selection.InlineShapes.AddPicture fileName:=outJPGFile, LinkToFile:=False, _
          SaveWithDocument:=True
          Selection.InsertCaption Label:="Figure", TitleAutoText:="", _
          Title:=" - " & tType, Position:=wdCaptionPositionBelow ', ExcludeLabel:=0

    In my case users can insert any number of pics so I resample/resize the pics for 96dpi otherwise the docs becomes too large.  I use the command line program ImageMajick and call it from the addin.

    If I can't figure out how to do this I thought about making a Word addin that acts like a server and creates the word doc and passes it back to the Silverlight app. 

    You can use Word record macro feature to see how Word does certain tasks.  This may help you.

    Wednesday, February 01, 2012 11:03 PM
  • Thank for your reply...one question in addition the picture...
    How can I change the style, size, type...if I add this text.....I want to make it Bold, Size 24, CaliforniaFB

    ' Create Paragraph with varaibles from each record inserted into it.
         word.Selection.TypeText(String.Format("{0}" & vbNewLine & "An automobile, {1}, motor car or car is.", customer.BusinessName, Date.Today.ToShortDateString()))

    In your example above....you saying this works? I assume that you import the image intothe application??? Where do you tell it the path adn name?

    I tried to add this to my silverlight app....

       word.selection.InlineShapes.AddPicture(fileName:=outJPGFile, LinkToFile:=False, SaveWithDocument:=True)
       word.selection.InsertCaption(Label:="Figure", TitleAutoText:="", _
          Title:=" - " & tType, Position:=wdCaptionPositionBelow) ', ExcludeLabel:=0

    I get 3 errors...blue squiggly lines....saying they are not declared...
    Errors:
    1. fileName:=outJPGFile
    2. tType
    3. Position:=wdCaptionPositionBelow

    I know if you get this to work it would be very very helpful adn would be interested with anything else you can add...

     

    I AM HOPING MY POST HAVE NOT BEEN CONFUSING.....I have shown two different ways to accomplish this....

    1. the above is one....where I am trying to find out how to change the font style, size, type

    2. the other is with bookmarks as seen here http://support.microsoft.com/kb/316383.....I cant get past the interop....I am gettign errros (forum Entries 3,4,5,6)

    CAN YOU SHED LIGHT ON BOTH?

     


     

    Thursday, February 02, 2012 9:09 AM
  • In the macro I sent you it was an example and a copy of my code.  You probably don't get access to predefined constants in Word VBA.  To get constant values I find it easiest to breakpoint in the Word VBA code and inspect the variables.  I recommend getting familiar with writing Word macros.  A good way to do this is record a macro and look at the code.

    ouJPGFile = "C:\pics\myPic1.jpg"  ' Location of jpg File

    tType = "My Caption Text"  ' Caption text

    wdCaptionPositionBelow = 1

    I recorded this macro.  You only need to include what you want to change.

    Sub Macro1()
    '
    ' Macro1 Macro
    ' Macro recorded 2/2/2012 by bill
    '
      With Selection.Font
        .Name = "Californian FB"
        .Size = 14
        .Bold = True
        .Italic = False
        .Underline = wdUnderlineNone
        .UnderlineColor = wdColorAutomatic
        .StrikeThrough = False
        .DoubleStrikeThrough = False
        .Outline = False
        .Emboss = False
        .Shadow = False
        .Hidden = False
        .SmallCaps = False
        .AllCaps = False
        .Color = wdColorAutomatic
        .Engrave = False
        .Superscript = False
        .Subscript = False
        .Spacing = 0
        .Scaling = 100
        .Position = 0
        .Kerning = 0
        .Animation = wdAnimationNone
      End With
    End Sub

    wdUnderlineNone = 0
    wdColorAutomatic = -16777216
    wdAnimationNone = 0

    False = 0
    True = 1 or -1 (not sure on this)

    You probably can't do With so you need to spell out each one completely.

    Selection.Font.Name = "Californian FB"
    Selection.Font.Size = 14
    Selection.Font.Bold = True



    Thursday, February 02, 2012 10:01 AM
  • I forgot to mention I recommend never changing fonts/size/bold etc in code.  Setup a template.  It will be setup with the correct fonts, font size, bold, bookmarks, header/footer, margins, styles, etc... you want.  Create a new document in code based on the template.  This saves you from constantly changing code.  Users can modify the template to suit their needs. 

    Thursday, February 02, 2012 4:03 PM
  • I am havin a hard time doing this...and understadn for that matter....I assume you set up a word doc with book marks right, adding in all the text you want...

    Then pass it values from Silverlight?  I was trying the example the guy put a couple posts in but am getting errors on the Interop

     

    Thursday, February 02, 2012 5:10 PM
  • I have done nothing in Silverlight  yet with regard to Word.  I am explaining Word VBA.  Like I said I setup a Word template (.dotx) with everything defined including bookmarks.  I create a Word doc (.docx) based on the template.  I am no help on the errors right now.  I'll check back when I get further along.

    Thursday, February 02, 2012 5:24 PM
  • This is what I am doing right now...I can get the font to change etc...

    Although this does not use word templates and bookmarks....As I stated I tried the
    example that the guy posted early in th is post...I am gettign errors on the "Imports Word = Microsoft.Office.Interop.Word"

    I dont really knwo how to proceed with the template and bookmarks...BUT WOULD LOVE TO LEARN...I really need this reporting capabilities...
    the report I am printing will be a bunch of Text say 2 pages worth...
    THEN say I have 3 records....I need to loop through each adn create a report for each...As seen below in my For Each Customer loop...

    That Make sense

    IF I ADD THIS TO THE BELOW CODE I GET ERRORS:

    Dim outJPGFile As String = "S:\Data\Images\JPG\OneWaySign.jpg"
    word.selection.InlineShapes.AddPicture(fileName:=outJPGFile, LinkToFile:=False, SaveWithDocument:=True)

    ERRORS on the line above
    Named Parameters are not supported by the automation layer.  Use positional parameters instead

     


        Private Sub GenerateSpreadSheetWord(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Dim rowIndex As Integer = 1
            Dim coulmnIndex As Integer = 1
            Dim i As Integer = 1


            If AutomationFactory.IsAvailable Then

                Dim word As Object = AutomationFactory.CreateObject("Word.Application")

                word.Visible = True
                word.Documents.Add()

                ' This is header infromation that will only be placed on the first page.
                word.Selection.TypeText("Data")
                word.Selection.TypeText(String.Format("Exported at: {0}", Date.Today.ToShortDateString()))
                'word.Selection.TypeParagraph()
                word.Selection.TypeText(vbNewLine)

                Dim outJPGFile As String = "S:\Data\Images\JPG\OneWaySign.jpg"
                Dim tType As String = "My Caption Text"
                Dim wdCaptionPositionBelow = 1


                For Each customer As CoreBusinessNewTwo In dataGridBusiness.ItemsSource

                    word.Selection.Font.Name = "CaliforniaFB"
                    word.Selection.Font.Size = 20
                    word.Selection.Font.Bold = True
                    word.Selection.TypeText("Acutal Data: " & vbNewLine)
                    word.Selection.TypeText("-----------------------------------------------------------------------------------" & vbNewLine)

                    word.Selection.Font.Name = "Calibri"
                    word.Selection.Font.Size = 8
                    word.Selection.Font.Bold = True
                    word.Selection.Font.Italic = True
                    ' Create Paragraph with varaibles from each record inserted into it.
                    word.Selection.TypeText(String.Format("{0}" & vbNewLine & "An automobile, {1}, motor car or car is a wheeled motor vehicle used for transporting passengers, which also carries its own engine or motor. The numbers are increasing rapidly, especially in China and India.", customer.BusinessName, Date.Today.ToShortDateString()))

                   word.selection.InlineShapes.AddPicture(fileName:=outJPGFile, LinkToFile:=False, SaveWithDocument:=True)   

                    ' Insert Page Break after each individual record returned from the data grid.
                    word.Selection.InsertBreak()

                Next customer

                ' This is footer infromation that will only be placed on the last page.
                word.Selection.TypeText("LAST PAGE")
                word.Selection.TypeText(String.Format("Exported at: {0}", Date.Today.ToShortDateString()))

            End If

        End Sub

    Thursday, February 02, 2012 5:25 PM
  • was able to get the image in like this..
    With the False And true representing LinToFile and SaveWithDocument respectively

    Dim outJPGFile As String = "S:\Images\JPG\OneWaySign.jpg"
    word.selection.InlineShapes.AddPicture(outJPGFile, False, True)

    HOW TO POSITION LEFT, CENTER, RIGHT BELOW

    Dim wdAlighParagraphLeft = 0
    Dim wdAlighParagraphCenter = 1
    Dim wdAlighParagraphRight = 2

    word.Selection.TypeParagraph()
    word.Selection.ParagraphFormat.Alignment = wdAlighParagraphCenter
    word.selection.InlineShapes.AddPicture(outJPGFile, False, True)

    Friday, February 03, 2012 9:03 AM
  • I use tables to place pictures and text.  I find it more reliable than other methods.  Place picture in one cell and text in another cell. You can use bookmarks to place picture/text. 

    Create a template with the table and bookmarks.  When you create the document base it on the template.

    word.Documents.Add("C:\MyWordTemplates\MyFirstTemplate.dotx")

    This is by far the best and esiest approach.

    Friday, February 03, 2012 9:44 AM
  • If read through this post you can see there are two approaches being discussed...its very confusing....

    1. One is simply formating the word page in VB

    2. The other is creating a template and using book marks...

    The later is what I CANNOT GET GOING...does anyone have an example?  If you look at my ealrier posts I cannot get the

    Imports Word = Microsoft.Office.Interop.Word to work....I hear its easy but cant find any good examples on how to do it...and the one example I was shown I cant get to work....

    Does anyone have code examples on how to get the BookMarks and the template to work?  A very simple example is all I need....

    Tuesday, February 07, 2012 10:01 AM
  • Hi buddy.

    Read your posts under silverlight export word doc etc. Well I am trying to insert an image thumbnail under my word doc.

    I have placed the screenshot of the webpage on the thumnail and now i want that thumnail to be exported directly to a word doc.

    The text is getting inserted under using--(under this chw is child window and icontextbox is under child window):

    word.Selection.TypeText(string.Format(chw.IconTextbox.Text));

     
    I want the thumbnail image to be part of my word doc with the above text(IconTextBox.text) .
     How this can be done, any hint??I
    It is related to what you were asking, i think so!!
    Reply ASAP!!
    Monday, April 30, 2012 1:21 AM