Form Fields in Word have trailing spaces

  • Thread starter Thread starter Lynn
  • Start date Start date
L

Lynn

I am able to automate the pushing of data from an Excel spreadsheet to a Word
document that has form fields using VBA. However, the data that is populated
to its respective field pushes the form field 5 spaces to the right of the
data. How do I program the data to populate "into" the field without
incurring extra spaces. NOTE: This scenario doesn't always happen after
running the Excel VBA code.
Thanks!
 
yes, here is a sample where the bookmark (form field) text is set.
(All bookmark values are set in this manner.)

objDoc.Bookmarks("txtParagraph1").select
objWord.Selection.Text = strParagraph1

Do you need more details?
Thanks!
 
Try this for a test

Sub Test()
Dim objDoc As Document
Dim objWord As Application
Dim myFormField As FormField

Set objDoc = ThisDocument


For Each myFormField In objDoc.FormFields
Debug.Print myFormField.Name, myFormField.TextInput.Type,
myFormField.TextInput.Type

If myFormField.Type = wdFieldFormTextInput And _
myFormField.TextInput.Type = wdRegularText Then
myFormField.Result = "Hello"
End If
Next myFormField
End Sub


--
HTH,
Barb Reinhardt

If this post was helpful to you, please click YES below.
 
Thank you, I will test.
--
MD


Barb Reinhardt said:
Try this for a test

Sub Test()
Dim objDoc As Document
Dim objWord As Application
Dim myFormField As FormField

Set objDoc = ThisDocument


For Each myFormField In objDoc.FormFields
Debug.Print myFormField.Name, myFormField.TextInput.Type,
myFormField.TextInput.Type

If myFormField.Type = wdFieldFormTextInput And _
myFormField.TextInput.Type = wdRegularText Then
myFormField.Result = "Hello"
End If
Next myFormField
End Sub


--
HTH,
Barb Reinhardt

If this post was helpful to you, please click YES below.
 
Thanks again!
This works when running it from Word. There are no trailing spaces.
Hmmmm... Do I use the ".Result" instead of ".Text" when running the code
from Excel? Is this what makes the difference, perhaps?
 
Barb,
It works! Never mind about the last question.
I used this type of assignment for each field -->
objDoc.FormFields("txtParagraph1").Result = strParagraph1

Thank you so much! It was mind-boggling there for a while.
You have been most helpful! Have a great day!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top