import text from another document, paste into form field result

  • Thread starter OTWarrior via OfficeKB.com
  • Start date
O

OTWarrior via OfficeKB.com

I have a word document with form fields that the user will enter information
into. This document is protected.
The information the user inputs into this form is also held in another
document, and I have had people "comment" on having to enter information
twice.

So I came up with the idea of importing the data from this other document
into my document. The first problem is actually importing the information
from the document itself:


************************************************
Private Sub cmdImport_Click()
Dim MyFile As String

ActiveDocument.Bookmarks("ImportTest").Select

MyFile = Dir("W:\Test Documents\")
Selection.InsertFile FileName:=MyFile & "test.doc"
End Sub

*************************************************

The above code specifically imports the contents of the file and pastes them
into the document. Whereas I would Ideally want the information to be brought
into a string value for me to manipulate.

The other issue with this code, is this is very specific, and you cannot
choose the file or directory. How would I bring open an explorer dialog box
(like when you press "insert|file...").

the full explaination of what I want to do is to import the first page from
this document, search for certain words (as the author did not use bookmarks
or form fields) and import the certain words into my chosen form fields as
the result value.

Just some help with how to use a dialog box to choose the file, and how to
bring the information in without pasting it onto the document would be very
much appreciated.

(BTW: I cannot use any databases or change the document that I want to import
from)
 
D

Doug Robbins - Word MVP

Sounds like the whole application needs an overhaul to me.

The following code, originally posted by fellow MVP, Jonathon West will
allow the user to browse to and select a file:

Public Function BrowseFile() As String
With Dialogs(wdDialogFileOpen)
If .Display <> -1 Then
BrowseFile = ""
Else
BrowseFile = WordBasic.FileNameInfo$(.Name, 1)
End If
End With
End Function

It returns the full pathname of the selected file, and a blank string if no
selection is made.

I am not sure what you mean by "how to bring the information in". That's
where I think an overhaul is probably required.
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
O

OTWarrior via OfficeKB.com

well..unfortunately I am not a microsoft developer so am unable to overhaul
the word application.

however, the code you supplied did open up the dialog box, and allow me to
choose that path to place into the document, so that is one step closer.

Now I just have to figure out how to search that document for information and
paste it where it is needed. Would a mail merge-esque command work?
 
D

Doug Robbins - Word MVP

I do not think that mailmerge is going to help you. I think you are going
to need to take a deep-dive into the world of VBA development.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
O

OTWarrior via OfficeKB.com

where would i start with that? I am unable to use visual studio at work, and
the whole point of this macro is some I don't have to have another prgram (or
even a userform).

Besides, the code for macros is vba (to my knowledge), but is just a case of
figuring out what commands I need to:

1) search for specific text
2) look "into" a document without opening it.

There is a technique that would take the information from an unopened
document and allow you to do want to want with it, called Screengrab or
screen flash (I can't remember), which would probably do it.
 
O

OTWarrior via OfficeKB.com

Sorry the term is "Text Stream" not screen grab
where would i start with that? I am unable to use visual studio at work, and
the whole point of this macro is some I don't have to have another prgram (or
even a userform).

Besides, the code for macros is vba (to my knowledge), but is just a case of
figuring out what commands I need to:

1) search for specific text
2) look "into" a document without opening it.

There is a technique that would take the information from an unopened
document and allow you to do want to want with it, called Screengrab or
screen flash (I can't remember), which would probably do it.
 
F

fumei via OfficeKB.com

You can use Application.FileSearch to go through folders (and sub folders)
and "peek" into files for specific text. OK, so you can do that.

However, I don't see how you can GET that text without opening the file.
Even using a TextStream still requires opening the file.

Dim StartLocation As String
Dim var
Dim NumberFiles As Long
StartLocation = “C:\VBAClassâ€
With Application.FileSearch
.NewSearch
.LookIn = StartLocation
.FileName = "*.doc"
.FileType = msoFileTypeAllFiles
.SearchSubFolders = True
.TextOrProperty = "Key word"
End With

NumberFiles = Application.FileSearch.Execute()
For var = 1 To NumberFiles
Documents.Open Filename:=Application.FileSearch.FoundFiles(var)
' open each found file and do stuff with each
' make sure you close them!
Next var

The above uses FileSearch to go through all .doc files in the StartLocation -
including all subfolders - looking for .doc files containing "Key word". The
array of FoundFiles includes the full path.

C:\Blah\ThisOne.doc
C:\Blah\ThisOtherOne.doc
C:\Blah\YetAnotherOne.doc
C:\Blah\SubFolderA\Some.doc
C:\Blah\SubFolderA\SomeOther.doc
C:\Blah\SubFolderA\SubSubFolderB\Whatever.doc
 

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

Top