none
Creating a word document and replace data and pdf generation RRS feed

  • Question

  • Hi Guys,

    I have the requirement where 

    1) take a word document template as a base 

    2) create instance of it and replace data inside that and SAVEAS different file file but PDF format.

    everything works fine with the below code. My requirement is to change the font dynamically (based on business rules for few elements) for the data being replaced. 

      Private Sub CreatePDF()
            Dim MyApp As New Word.Application()        

            Dim MyWordDoc As Word.Document = MyApp.Documents.Add("C:\WorkProjects\Temp\Tmp\Tmp\PDF\COPYWORD_TEMPLATE.docx")
            MyApp.Visible = False
            MyApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone

            
            For Each item As String In mDataDictionary.Keys
                Dim keyInfo As String = item
                MyWordDoc.Content.Find.Execute(FindText:=keyInfo, ReplaceWith:=mDataDictionary(keyInfo).ToString(), Replace:=Word.WdReplace.wdReplaceAll)
            Next
            

            IO.File.Delete("C:\WorkProjects\Temp\Tmp\Tmp\PDF\tmpGeneratedPDFFileFinal.pdf")

                MyWordDoc.SaveAs("C:\WorkProjects\Temp\Tmp\Tmp\PDF\tmpGeneratedPDFFileFinal.pdf",Word.WdSaveFormat.wdFormatPDF)
            MyWordDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
            MyApp.Quit()

        End Sub



    • Edited by Aryaa Friday, June 29, 2018 1:21 PM
    Friday, June 29, 2018 1:20 PM

Answers

All replies

  • Looks like you can use a Range object to get the Find and then the range will represent the found text.  At that point you could change the styling on the range before moving on to the next Find.

    https://msdn.microsoft.com/en-us/vba/word-vba/articles/find-object-word


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Proposed as answer by IronRazerz Friday, June 29, 2018 6:50 PM
    • Marked as answer by Aryaa Friday, June 29, 2018 11:53 PM
    Friday, June 29, 2018 2:01 PM
    Moderator
  •  I am taking it that you just need help with finding specific words or paragraphs in the Word document and changing the font of them.  If that is so,  you can take a look at the first link below which has examples and explanations of how to iterate through the words or paragraphs of a word document.

    Working with Word document content objects

     You can also take a look at the link below.  I showed an example for finding all instances of a specific word in the paragraph and changing them to a Bold font.

    Changing Font Within Paragraph - Word Document Created Programatically


    If you say it can`t be done then i`ll try it

    Friday, June 29, 2018 6:48 PM
  • Hi, you can probably try this out. It shows all the details that you need.

    • create a template Word file
    • replace data inside the document 
    • save file to a PDF file

    https://www.c-sharpcorner.com/uploadfile/38268a/create-word-document-from-win-form-with-users-data/


    Wednesday, September 5, 2018 2:17 AM
  • thank you, i need this
    Wednesday, September 5, 2018 3:07 AM