PDA

View Full Version : Problem with inserting a TXT-File



JPDO
07-27-2006, 03:09 AM
Hi,

I launch MS-word from an EXCEL-application to fill-up a Word-document that i defined as a Fill-in Form .
Filling up is OK, but at the end of the document I have to insert the content of a TXT-file and that gives me a runtime error 438.
What did I wrong?

Here goes my coding:

wdModelCLFullname = "S:\AS_400\JPDO\kwitluxfr-temp.doc"

Set wdApp = Nothing
Set wdDoc = Nothing
' ---------------------------------------
' * Open Word
' ---------------------------------------
Set wdApp = New Word.Application
Set wdDoc = New Word.Document
Set wdDoc = wdApp.Documents.Add(wdModelCLFullname, False, wdNewBlankDocument, True)
wdDoc.Activate
wdApp.Visible = True
wdDoc.Unprotect
wdDoc.FormFields("NM1").Result = NM1
wdDoc.FormFields("REFN1").Result = REFN1
' ---------------------------------------
' * Form FIELD TEXT1 is the location where i want to insert the TXT-File
' ---------------------------------------
wdDoc.FormFields("TEXT1").Select
wdDoc.FormFields("TEXT1").Delete
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Select
With Selection
.InsertFile Filename:="C:\download\KWITLUXFR.txt", Range:="", _
ConfirmConversions:=False, Link:=False, Attachment:=False
End With
MsgBox "Save Your Document now"


Thanks for any reply.

TonyJollans
07-27-2006, 03:55 AM
With Selection ....

works with the Excel Selection object.

To use the Word Selection instead, use ...

With wdApp.Selection

fumei
07-27-2006, 03:33 PM
I am wondering. Could you explain your code:' * Form FIELD TEXT1 is the location where i want to insert the TXT-File
' ---------------------------------------
wdDoc.FormFields("TEXT1").Select
wdDoc.FormFields("TEXT1").Delete
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Select1. Why do you have formfield TEXT1 when you just select it and delete it?

2. You do not need to select it in order to delete it.

3. You insert a new paragraph at the end of the document using the paragraph count. Works, but seems odd.

wdDoc.FormFields("TEXT1").Range.Delete
With wdDoc.Selection
.EndKey Unit:=wdStory
.TypeParagraph
.InsertFile Filename:="C:\download\KWITLUXFR.txt"
End With

4. Oh, and could you use the VBA tags for your code? Thanks.