tca_VB
09-18-2008, 12:29 PM
I've searched around and cannot find an answer...
In VBA code within Excel, I want to open a word template and set a form field value in that document to a field in excel.
here's what I've tried...
Sub OpenPINDoc()
'
' Open PIN Doc Macro for New TN Client
Dim xlApp As Object
Dim wdApp As Object
Dim xlString As String
Dim catString As String
Dim storString As String
Dim dotString As String
Dim pinString As String
Sheets("Doc_Index").Select
storString = Sheets("Doc_Index").Range("B2")
dotString = Sheets("Doc_Index").Range("C2")
pinString = storString & dotString
'Open Word Instance
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
'Open Template as Document
wdApp.Documents.Add Template:=(pinString)
wdApp.Activate
'Prefill Data
With wdApp.ActiveDocument
.FormFields("docCategory").Result = ThisWorkbook.Sheets("DocIndex").Range("F1").Value
End With
'Clear
Set xlApp = Nothing
Set wdApp = Nothing
End Sub
I have also tried to select form field and copy paste using:
'copy from excel
ThisWorkbook.Sheets("Doc_Index").Range("F1").Copy
wdApp.Selection.Goto What:=wdGoToBookmark, Name:=docCategory
wdApp.Selection.Paste
The second will past the value into the word doc, but at the top of the page and not the selected form field.
Thanks for the help.
In VBA code within Excel, I want to open a word template and set a form field value in that document to a field in excel.
here's what I've tried...
Sub OpenPINDoc()
'
' Open PIN Doc Macro for New TN Client
Dim xlApp As Object
Dim wdApp As Object
Dim xlString As String
Dim catString As String
Dim storString As String
Dim dotString As String
Dim pinString As String
Sheets("Doc_Index").Select
storString = Sheets("Doc_Index").Range("B2")
dotString = Sheets("Doc_Index").Range("C2")
pinString = storString & dotString
'Open Word Instance
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
'Open Template as Document
wdApp.Documents.Add Template:=(pinString)
wdApp.Activate
'Prefill Data
With wdApp.ActiveDocument
.FormFields("docCategory").Result = ThisWorkbook.Sheets("DocIndex").Range("F1").Value
End With
'Clear
Set xlApp = Nothing
Set wdApp = Nothing
End Sub
I have also tried to select form field and copy paste using:
'copy from excel
ThisWorkbook.Sheets("Doc_Index").Range("F1").Copy
wdApp.Selection.Goto What:=wdGoToBookmark, Name:=docCategory
wdApp.Selection.Paste
The second will past the value into the word doc, but at the top of the page and not the selected form field.
Thanks for the help.