Solved

Word form field codes printing out rather then their text values.

Posted on 2013-12-16
10
737 Views
Last Modified: 2014-01-02
Using the Visual Studio 2010, I have a VB application that exports data from the application,
to a Microsoft 2010 Word template.
In the template I have created tags using the Word's Form Filed tool.  
Example:  <<Letter Date>>
The app successfully exports data and fills the above form field with today's
date.  But when I print the document the <<Letter Date>> is printed rather
then the date.
Things I've tried:
In the Microsoft Word options ->Advanced-> under "Show document content",
I have ensured that the "Show field codes instead of their values" is un-clicked.
Also in Word options under "Print"
I've ensured that the "Print field codes instead of their values" is un-clicked.

But when I print the Document, the field codes <<Letter Date>> still prints rather
then it's contents, todays date.
0
Comment
Question by:N2435
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
10 Comments
 

Author Comment

by:N2435
ID: 39724628
thanks
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39724633
If the field looks like <<this>>  then it is probably a mail merge field. Mail merge fields are used on mail merge main documents. Mail merge can be used to produce outputs where the fields are replaced by text from a datasource.

The output can be another document, printer, email or fax.

Can you post an example, please?
0
 
LVL 40
ID: 39724662
This is how it goes in 2007, so I suppose it does the same in 2010:

Go in the Options : Office button, Word Options in the lower right.

Advanced tab... Print section

Uncheck Print field codes instead of their values
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

Author Comment

by:N2435
ID: 39724749
Please see attached word doc that has a copy of my code and
a word doc that has one form field.

Please note in my original post:
Things I've tried:
In the Microsoft Word options ->Advanced-> under "Show document content",
I have ensured that the "Show field codes instead of their values" is un-clicked.
Also in Word options under "Print"
I've ensured that the "Print field codes instead of their values" is un-clicked.

Thanks
Public-Function-createNewLetter.docx
TEST01.docx
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39724975
Intermediate report.

What you are seeing is not the field code, but the original text in the Form Field that your code is trying to replace.

The code is in .Net, so it will take me a while to test. Meanwhile here are a couple of points to note.

Your sample document is unprotected. For Form Fields to work properly the document should be protected for filling in forms. Hopefully your actual document is so protected.

Secondly you are going through a bit of code to copy the original document. It would be easier to treat the original as a template and to use the Documents.Add. This will create a new document. Otherwise there is a slight danger of confusing the new document with the original.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 39725142
This version of your code works OK and prints out the date as requested. Mainly I have created an new document using the .Add method and now print it in the code.
Option Explicit On
Public Class Form1

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        MessageBox.Show("Result=" & createNewLetter("ttt"))
    End Sub
    Public Function createNewLetter(ByVal type As String) As Boolean
        Dim retVal As Boolean = True
        'Dim intresponse As Integer
        Dim title As String = "Creating New Letter"
        Dim todaysDate As String
        Dim sourceDir As String = "I:\Allwork\ee\28319166\"
        Dim targetDir As String = "I:\Allwork\ee\28319166\"
        Dim templateNm As String = "test01.docx"
        Dim docNewName As String = "NewDoc.docx"
        Dim fullPathNMfrom As String
        Dim fullPathNMto As String
        Dim WordApp As Object
        Dim WordDoc As Object
        Const wdFormatXML As Integer = 11
        Try
            fullPathNMfrom = sourceDir & templateNm
            fullPathNMto = targetDir & docNewName

            WordApp = CreateObject("Word.Application")
            WordApp.Visible = True
             WordDoc = WordApp.Documents.Add(fullPathNMfrom)
            ' insert today's date Letter DT field.
            todaysDate = Date.Today.ToString("MMMM dd, yyyy")
            WordDoc.FormFields("Text1LetterDT").Result = todaysDate
            ' call function to populate rest of letter

            WordDoc.FormFields.Shaded = False
            WordDoc.Saveas(fullPathNMto, wdFormatXML)
            WordDoc.PrintOut()
            WordApp.Quit()
            Return retVal
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message & vbCrLf & vbCrLf & "openLetterTemplate", "Error", _
                           MessageBoxButtons.OK, MessageBoxIcon.Error)
            retVal = False
            'Wrapup()
            Return retVal
        End Try
    End Function
End Class

Open in new window

0
 

Author Comment

by:N2435
ID: 39736445
I have modified my code similar to the code as shown in "GrahamSkan" response.   I did not
however include the 'WordDoc.PrintOut()".

My intention is to have the Word document remain open, and have  the user :
    1)  proof read,
    2)  make any corrections.

Then if everything is good, do  the Word's FILE->Print, and then a File->Save_as .

With the modifications to my code, the application will still show the today's date on the screen.
But when I the do the FILE-> print, the <<Letter Date>> is printed rather than the date.

Also the display on the screen now shows the <<Letter Date>> , thus not allowing for a
FILE->save as of the corrections made  be the user.

Please note I am so grateful for the review of my code and the great suggestions to make
my code more efficient.

I have tried the FILE->Pint  on several  different printers, same result.    

Again thanks, and a very Merry Christmas
0
 

Author Comment

by:N2435
ID: 39752408
I have resolved my printing issues.
Steps taken:
1)      Used most of the code submitted by  GrahamSkan Posted on 12/17/2017.
2)      Ensured that the source template was a "docx" type.  (It was not in my initial post)
3)      Besides :
        a)  In the Microsoft Word options ->Advanced-> under "Show document content",
               I have ensured that the "Show field codes instead of their values" is un-clicked.
        b)  Also in Word options under "Print"
               I've ensured that the "Print field codes instead of their values" is un-clicked.

          There is another Word options that I changed:
       c) under "Display" -> "Printing options", I needed to unclick the "Update fields
            before Printing".

Thanks to all who responded to my problem, especially to GrahamSkan!

I feel this incident is solved and should be closed
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This collection of functions covers all the normal rounding methods of just about any numeric value.
Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question