Export Long Text from Access to Word (1 Viewer)

andy1968

Registered User.
Local time
Today, 03:30
Joined
May 9, 2018
Messages
131
I have code that will export data on an Access form to a Word template with text box fields.


All data exports fine, except a long text field. The Word text box remains blank.


The Word text box is "txtPDescription" and the Access field is "ProjectDescription".


I've tried a number of solutions, and searched for a couple of hours and cannot find a solution that I can understand well enough to use.


Here is the code I am using:


Code:
Private Sub cmdPrintWord_Click()
Dim appword As Word.Application
Dim doc As Word.Document
Dim path As String

path = Me.QCPlanPath

On Error Resume Next
Error.Clear
Set appword = GetObject(, "word.application")
If Err.Number <> 0 Then
Set appword = New Word.Application
appword.Visible = True
End If
Set doc = appword.Documents.Open(path, , True)
With doc
    .FormFields("txtProject").Result = Me.[Project Name]
    .FormFields("txtContractNumber").Result = Me.Contract_Number
    .FormFields("txtPreparedBy").Result = Me.[Prepared By]
    .FormFields("txtReviewedBy").Result = Me.[Reviewed By]
    .FormFields("txtCost").Result = Me.Cost
    .FormFields("txtDuration").Result = Me.Duration
    .FormFields("txtFedNumber").Result = Me.Federal_Project_Number
End With
With doc
.FormFields("txtPDescription").Result = "****"
Selection.GoTo what:=wdGoToBookmark, Name:="txtPDescription"
Selection.Collapse
Selection.MoveRight wdCharacter, 1
Selection.TypeText ([Forms]![ProjectDescription]![Field])
Selection.GoTo what:=wdGoToBookmark, Name:="txtPDescription"
' Remove unique characters from Word_Form_Field_Name form field.
With Selection.Find
.Execute findtext:="*", replacewith:="", Replace:=wdReplaceAll
End With
End With


' get all the permits from the subform
Dim rs As Recordset: Set rs = Me.frmQCPlanPermits.Form.RecordsetClone
With rs
    .MoveLast
    If Not .EOF Then
        .MoveLast
        .MoveFirst
    End If
End With

Dim idx As Integer
For idx = 1 To rs.RecordCount
    With doc.Tables(4)
        .Cell(idx, 2).Range.Text = Nz(rs!Permit)
        .Cell(idx, 3).Range.Text = Nz(rs!Agency)
        If rs.AbsolutePosition <> rs.RecordCount - 1 Then .Columns(1).Cells.Add
    End With
    rs.MoveNext
Next idx
    
appword.Visible = True
appword.Activate
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:30
Joined
Oct 29, 2018
Messages
21,467
Hi. Unless somebody here knows what to do with that (I don't), you might consider posting some demo files for us to play with, and maybe we can help you figure it out. Just a thought...
 

andy1968

Registered User.
Local time
Today, 03:30
Joined
May 9, 2018
Messages
131
Can do. Give me a couple of minutes.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:30
Joined
Oct 29, 2018
Messages
21,467
Can do. Give me a couple of minutes.
Hi. Thanks. I don't claim to know everything but do enjoy challenges and don't mind tinkering to see if I can figure it out. Cheers!
 

andy1968

Registered User.
Local time
Today, 03:30
Joined
May 9, 2018
Messages
131
Here is a copy of the database and the Word template.


The offending text box is on top of page 5.


The data base is a "work in progress" as is the word document.


The file path for the word template will need to be changed on the form to match your saved location.
 

Attachments

  • QCPlan for sharing.zip
    132.5 KB · Views: 603

theDBguy

I’m here to help
Staff member
Local time
Today, 03:30
Joined
Oct 29, 2018
Messages
21,467
Here is a copy of the database and the Word template.


The offending text box is on top of page 5.


The data base is a "work in progress" as is the word document.


The file path for the word template will need to be changed on the form to match your saved location.
Thanks. I’ll take a look when I get home and let you know, even if somebody beats me to it.
 

andy1968

Registered User.
Local time
Today, 03:30
Joined
May 9, 2018
Messages
131
Thanks! I really appreciate all the help you've given me on here!
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:30
Joined
Oct 29, 2018
Messages
21,467
Thanks! I really appreciate all the help you've given me on here!
Hi. I don't do a lot of Word automation, but are you sure you attached the correct Word document earlier? If so, how can I see the bookmarks? I only see a one page document with one bookmark called Text1, but you said to look on Page 5.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:30
Joined
Oct 29, 2018
Messages
21,467
Here is a copy of the database and the Word template.

The offending text box is on top of page 5.

The data base is a "work in progress" as is the word document.

The file path for the word template will need to be changed on the form to match your saved location.
So, what I did was put a break on the line just before the code starts to fill in the data. Then, I went to the Immediate Window and entered the following line of code:
Code:
appword.Documents(1).Bookmarks(1).Range.Fields(1).Result.Text = Me.[Project Description]
This seems to have filled-in the bookmark with all the data from the current record on the form. Is this what you mean?
 

andy1968

Registered User.
Local time
Today, 03:30
Joined
May 9, 2018
Messages
131
Yeah, I see I did send the incorrect word document. Sorry about that.



Here is the correct one as well as a video showing how to get the bookmark names.
 

Attachments

  • QC Template.zip
    651.7 KB · Views: 578

andy1968

Registered User.
Local time
Today, 03:30
Joined
May 9, 2018
Messages
131
OK, with help from a co-worker, we got the export of the long text field to work.


Code:
With doc
    .FormFields("txtProject").Result = Me.[Project Name]
    .FormFields("txtContractNumber").Result = Me.Contract_Number
    .FormFields("txtPreparedBy").Result = Me.[Prepared By]
    .FormFields("txtReviewedBy").Result = Me.[Reviewed By]
    .FormFields("txtCost").Result = Me.Cost
    .FormFields("txtDuration").Result = Me.Duration
    .FormFields("txtFedNumber").Result = Me.Federal_Project_Number
    .Bookmarks("txtPDescription").Range.Fields(1).Result.Text = Me.ProjectDescription
End With
The last line of the with statement is the key.



When the length of the text field was over 255 characters, the transfer would not work. Now it does.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:30
Joined
Oct 29, 2018
Messages
21,467
OK, with help from a co-worker, we got the export of the long text field to work.


Code:
With doc
    .FormFields("txtProject").Result = Me.[Project Name]
    .FormFields("txtContractNumber").Result = Me.Contract_Number
    .FormFields("txtPreparedBy").Result = Me.[Prepared By]
    .FormFields("txtReviewedBy").Result = Me.[Reviewed By]
    .FormFields("txtCost").Result = Me.Cost
    .FormFields("txtDuration").Result = Me.Duration
    .FormFields("txtFedNumber").Result = Me.Federal_Project_Number
    .Bookmarks("txtPDescription").Range.Fields(1).Result.Text = Me.ProjectDescription
End With
The last line of the with statement is the key.



When the length of the text field was over 255 characters, the transfer would not work. Now it does.
Hi. Congratulations! Glad to hear you got it sorted out. Looks like you used the same code I posted earlier. Good luck with your project.
 

Users who are viewing this thread

Top Bottom