- Joined
- Sep 8, 2011
- Messages
- 4
- Reaction score
- 0
Hi,
Really new at this VBA stuff. Having a heck of a time transferring custom field data to a Word Template. I have a custom form based on the Contacts form. The first tab is called "Build Sheet" and it has a text box on it called "usrName"
Sub cmdPrint_Click()
Set oWordApp = CreateObject("Word.Application")
If oWordApp Is Nothing Then
MsgBox "Couldn't start Word."
Else
Dim oWordApp
Dim oWordDoc
Dim bolPrintBackground
msgbox "Open Doc"
Set oDoc = oWordApp.Documents.Add("c:\Templates\BuildSheet.dot")
msgbox "Field 1: Find"
strusrName = Item.UserProperties.Find(usrName)
msgbox "Field 1: Result"
oDoc.FormFields("dName").Result = strusrName
msgbox "Field 2"
strusrCWID = Item.UserProperties.Find(usrCWID)
oDoc.FormFields("dCWID").Result = strusrCWID
msgbox "Begin Print"
bolPrintBackground = oWordApp.Options.PrintBackground
oWordApp.Options.PrintBackground = False
oDoc.PrintOut
oWordApp.Options.PrintBackground = bolPrintBackground
Const wdDoNotSaveChanges = 0
oDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
oWordApp.Quit
Set oDoc = Nothing
Set oWordApp = Nothing
End If
End Sub
So I got this code from a website and I've been trying different things to get the data moved over to the word template. I added the msgbox as a sort of debug thing. The script runs until the bolded part.then stops. If I replace Item.UserProperties.Find(usrName) with "Hello" the script runs past this point but encounters the same issue in the next Item.UserProperties command. If I replace that one also, the script completes and the DOT prints with the text I chose.
What am I missing? I have a sinking feeling it's ridiculously easy....
Really new at this VBA stuff. Having a heck of a time transferring custom field data to a Word Template. I have a custom form based on the Contacts form. The first tab is called "Build Sheet" and it has a text box on it called "usrName"
Sub cmdPrint_Click()
Set oWordApp = CreateObject("Word.Application")
If oWordApp Is Nothing Then
MsgBox "Couldn't start Word."
Else
Dim oWordApp
Dim oWordDoc
Dim bolPrintBackground
msgbox "Open Doc"
Set oDoc = oWordApp.Documents.Add("c:\Templates\BuildSheet.dot")
msgbox "Field 1: Find"
strusrName = Item.UserProperties.Find(usrName)
msgbox "Field 1: Result"
oDoc.FormFields("dName").Result = strusrName
msgbox "Field 2"
strusrCWID = Item.UserProperties.Find(usrCWID)
oDoc.FormFields("dCWID").Result = strusrCWID
msgbox "Begin Print"
bolPrintBackground = oWordApp.Options.PrintBackground
oWordApp.Options.PrintBackground = False
oDoc.PrintOut
oWordApp.Options.PrintBackground = bolPrintBackground
Const wdDoNotSaveChanges = 0
oDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
oWordApp.Quit
Set oDoc = Nothing
Set oWordApp = Nothing
End If
End Sub
So I got this code from a website and I've been trying different things to get the data moved over to the word template. I added the msgbox as a sort of debug thing. The script runs until the bolded part.then stops. If I replace Item.UserProperties.Find(usrName) with "Hello" the script runs past this point but encounters the same issue in the next Item.UserProperties command. If I replace that one also, the script completes and the DOT prints with the text I chose.
What am I missing? I have a sinking feeling it's ridiculously easy....