• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

User form entry into a word file

game_federer

New Member
Hi all,
I want to create a word doc like an air plane ticket using data I'm getting from a user form.
I have already created the user form.
Wanted help in creation of the word doc.

Your help will be sincerely appreciated.

Thanks.
 
Hi,
I did go through this solution. Is there any way to achieve this without opening word?
For eg. I have Data on a spreadsheet like Amulya(Name) and AI989(flight)

Now, I want to create a word doc about this like with a some font like:

Hi Amulya,

We are pleased to inform that your flight AI989 has been confirmed etc etc etc.

Thanks.


I hope you're getting where I'm going. Your help will be greatly appreciated.(Newbie here)
 
I am not conversant with Word Object Model as such so I can't tell if you can do it without opening word. Easier would be to open an invisible instance of word and write into it.

I am attaching example of my suggestion. Like you I am also newbie when it comes to Ms-Word and therefore what's used may not be best practice for Word VBA. If it suits you then you can adopt code inside form code. Hope this puts you on track. Good luck!

Code:
Option Explicit
Private Sub cmdProcess_Click()
Dim wdApp As Object 'Word.Application
Dim wdDoc As Object 'Word.Document
Dim wdSel As Object 'Word.Selection
Dim lngLastRow As Long, i As Long

lngLastRow = Range("A" & Rows.Count).End(xlUp).Row
If lngLastRow < 5 Then Exit Sub

Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True

Dim strPassengerName As String, strFlightName As String, strMessage1 As String, strMessage2 As String
For i = 5 To lngLastRow
    Set wdDoc = wdApp.Documents.Add
    strPassengerName = Range("A" & i).Value
    strFlightName = Range("B" & i).Value
    strMessage1 = "Hi #NAME#"
    strMessage2 = "We are pleased to inform you that your flight #FLIGHT# has been confirmed."
   
    wdDoc.Range.Font.Name = "Arial"
    Set wdSel = wdApp.Selection
    wdSel.TypeText Replace(strMessage1, "#NAME#", strPassengerName, 1, 1, vbBinaryCompare)
    wdSel.TypeText vbCrLf
    wdSel.TypeText Replace(strMessage2, "#FLIGHT#", strFlightName, 1, 1, vbBinaryCompare)
    wdSel.TypeText vbCrLf
    wdSel.TypeText "Regards,"
    Set wdSel = Nothing
   
    wdDoc.SaveAs Range("C" & i).Value & "\" & strPassengerName & "-" & strFlightName & ".docx"
    wdDoc.Close
Next i

wdApp.Quit
Set wdSel = Nothing
Set wdApp = Nothing
End Sub
 

Attachments

  • ExcelToWord.xlsm
    19.5 KB · Views: 4
Back
Top