Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-07-2014, 06:55 PM
RonNCmale RonNCmale is offline Extract form fields to Word Document Windows XP Extract form fields to Word Document Office 2003
Novice
Extract form fields to Word Document
 
Join Date: Sep 2012
Posts: 15
RonNCmale is on a distinguished road
Default Extract form fields to Word Document

The following code works, but I need to tweak this to do the following.


1.Word documents that are in folder are protected w/o password, need this macro to unprotect and then retrieve data.
2. Would like macro to be able to retrieve a form field by bookmark name
instead of all form fields.

Any help would be appreciated.



Code:
Dim mydoc As Document
Dim target As Document
Dim i As Long

'let user select a path
With Dialogs(wdDialogCopyFile)
If .Display() <> -1 Then Exit Sub
MyPath = .Directory
End With

'strip quotation marks from path
Set target = Documents.Add
If Len(MyPath) = 0 Then Exit Sub

If Asc(MyPath) = 34 Then
MyPath = Mid$(MyPath, 2, Len(MyPath) - 2)
End If
'get files from the selected path
'and insert them into the doc
 MyName = Dir$(MyPath & "*.*")
 Do While MyName <> ""
 Set mydoc = Documents.Open(MyPath & MyName)
For i = 1 To mydoc.FormFields.Count - 1
 target.Range.InsertAfter mydoc.FormFields(i).Result & vbTab
 Next i
target.Range.InsertAfter mydoc.FormFields(i).Result & vbCr
 mydoc.Close wdDoNotSaveChanges
 MyName = Dir$
 Loop
Reply With Quote
  #2  
Old 01-08-2014, 01:59 PM
macropod's Avatar
macropod macropod is offline Extract form fields to Word Document Windows 7 32bit Extract form fields to Word Document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Here's a simple demo of how you can loop through a specified set of formfields:
Code:
Sub Demo()
Dim StrFlds As String, i As Long
StrFlds = "FmFld1,FmFld2,FmFld3"
With ActiveDocument.Range
  For i = 0 To UBound(Split(StrFlds, ","))
    MsgBox .FormFields(Split(StrFlds, ",")(i)).Result
  Next
End With
End Sub
Simply change "FmFld1,FmFld2,FmFld3" to whatever formfield names you use, in the order in which you want to extract them.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 01-08-2014, 02:31 PM
RonNCmale RonNCmale is offline Extract form fields to Word Document Windows 7 64bit Extract form fields to Word Document Office 2003
Novice
Extract form fields to Word Document
 
Join Date: Sep 2012
Posts: 15
RonNCmale is on a distinguished road
Default

I'm back to square one. I thought the above code would work for me. I'm trying to open multiple protected word documents in the same folder and extract a certain text form field by name to a word template. This code opens a new word document.

Any assistance or pointing me in the right direction would be great.

Thanks again.
Reply With Quote
  #4  
Old 01-08-2014, 02:43 PM
macropod's Avatar
macropod macropod is offline Extract form fields to Word Document Windows 7 32bit Extract form fields to Word Document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The following macro allows you to browse to a folder containing the documents you want to process, then process them automatically.
Code:
Sub ProcessDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdSrcDoc As Document, wdTgtDoc As Document
strFolder = GetFolder
If strFolder = "" Then Exit Sub
Set wdTgtDoc = ActiveDocument
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  Set wdSrcDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
  With wdSrcDoc
    'Do your processing here
    '
    .Close SaveChanges:=False
  End With
  strFile = Dir()
Wend
Set wdSrcDoc = Nothing: Set wdTgtDoc = Nothing
Application.ScreenUpdating = True
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
If you run the code from your target document, all you'll need to add is the code to extract the data & update the target (where indicated by the comments in the code above). For example, the processing code for a single formfield might be something like:
Code:
wdTgtDoc.Range.InsertAfter .FormFields("MyField").Result & vbCr
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 01-08-2014, 11:43 PM
RonNCmale RonNCmale is offline Extract form fields to Word Document Windows 7 64bit Extract form fields to Word Document Office 2003
Novice
Extract form fields to Word Document
 
Join Date: Sep 2012
Posts: 15
RonNCmale is on a distinguished road
Default

First of all, I thank you Macropod for all your help, But I can't for the life of me find out where to placed the following code.

Code:
wdTgtDoc.Range.InsertAfter .FormFields("MyField").Result & vbCr
I entered in where it says
With wdSrcDoc
'Do your processing here, '


but not knowing how to code, it evidently needs more. I keep getting an error code.
Reply With Quote
  #6  
Old 01-08-2014, 11:57 PM
macropod's Avatar
macropod macropod is offline Extract form fields to Word Document Windows 7 32bit Extract form fields to Word Document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You entered the code in the correct place, but did you change "MyField" to whatever your own field's name is?

What was the error message?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 01-09-2014, 12:00 AM
RonNCmale RonNCmale is offline Extract form fields to Word Document Windows 7 64bit Extract form fields to Word Document Office 2003
Novice
Extract form fields to Word Document
 
Join Date: Sep 2012
Posts: 15
RonNCmale is on a distinguished road
Default

Yes, I actually named it MyField to test

On the word template I have a text formfield that for the bookmark name; I named it "MyField".

Code:
Sub ProcessDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdSrcDoc As Document, wdTgtDoc As Document
Dim StrFlds As String, i As Long
StrFlds = "Form1,Form2,Form3"
strFolder = GetFolder
If strFolder = "" Then Exit Sub
Set wdTgtDoc = ActiveDocument
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  Set wdSrcDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
  With wdSrcDoc
    wdTgtDoc.Range.InsertAfter .FormFields("MyField").Result & vbCr
    .Close SaveChanges:=False
  End With
  strFile = Dir()
Wend
Set wdSrcDoc = Nothing: Set wdTgtDoc = Nothing
Application.ScreenUpdating = True
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
I get a runtime error 5941 The requested member of the collection does not exist on

wdTgtDoc.Range.InsertAfter .FormFields("MyField").Result & vbCr

Form1, Form2, and Form3 need to be in paragraph form in sequential order on the target document
Reply With Quote
  #8  
Old 01-09-2014, 12:10 AM
macropod's Avatar
macropod macropod is offline Extract form fields to Word Document Windows 7 32bit Extract form fields to Word Document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The code works fine for me. Can you attach your test document (i.e. the one with the formfields) to a post (delete anything sensitive)? You do this via the paperclip symbol on the 'Go Advanced' tab.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 01-09-2014, 12:24 AM
RonNCmale RonNCmale is offline Extract form fields to Word Document Windows 7 64bit Extract form fields to Word Document Office 2003
Novice
Extract form fields to Word Document
 
Join Date: Sep 2012
Posts: 15
RonNCmale is on a distinguished road
Default

The doc. that says Copy of Memo is the target document. (I need the formfield (Form1) to be first on the target document in paragraph form, and (Form2) to be second on the target document also in paragraph form.
The Big boy doc and The Little boy doc are the documents that i'm trying to pull info from that were in a folder.

Thanks again
Attached Files
File Type: doc The Big boy.doc (23.5 KB, 9 views)
File Type: doc The little boy.doc (23.5 KB, 8 views)
File Type: doc Copy of Memo.doc (28.5 KB, 9 views)
Reply With Quote
  #10  
Old 01-09-2014, 12:38 AM
macropod's Avatar
macropod macropod is offline Extract form fields to Word Document Windows 7 32bit Extract form fields to Word Document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

OK, the main problem is that your 'MyField' reference is in the target document and not in the source documents, which have 'Form1' & 'Form2'. In post #3 you said:
Quote:
I'm trying to open multiple protected word documents in the same folder and extract a certain text form field by name to a word template.
On that basis, I provided code that worked on the assumption all of the source documents would be using the same field name and that their output was simply to go somewhere the target document (I coded for the data to be inserted sequentially, at the end of the document).

If you want to obtain data from different field names in different documents and output them all into one or more fields in the target document, the code has to be written to do that. Before going down that path, though, it would be helpful if you could clarify your aims. For example, does the transferred data need to go into a formfield, or can we perhaps simply insert it at the end of the target document as the code now does?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #11  
Old 01-09-2014, 12:42 AM
RonNCmale RonNCmale is offline Extract form fields to Word Document Windows 7 64bit Extract form fields to Word Document Office 2003
Novice
Extract form fields to Word Document
 
Join Date: Sep 2012
Posts: 15
RonNCmale is on a distinguished road
Default

simply insert it at the end of the target document as the code now does will be great. It doesn't have to be in a formfield.

The project is doing interviews, the each person will provide a statement that will be in a formfield that will be protected and in a folder. I need to extract those statements from the folder and place the retrieved info on the target document in paragraph form with form1, followed by Form2, etc.
Reply With Quote
  #12  
Old 01-09-2014, 12:45 AM
macropod's Avatar
macropod macropod is offline Extract form fields to Word Document Windows 7 32bit Extract form fields to Word Document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

OK, but what about the source document formfield names? Will they have the same name or different names in the different documents? Do they need to be inserted in any particular order?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #13  
Old 01-09-2014, 12:58 AM
RonNCmale RonNCmale is offline Extract form fields to Word Document Windows 7 64bit Extract form fields to Word Document Office 2003
Novice
Extract form fields to Word Document
 
Join Date: Sep 2012
Posts: 15
RonNCmale is on a distinguished road
Default

source documents will be the different names: example Form1, Form2 ,Form3, etc. and will need to be in that order on the target document separated in paragraph form.
Reply With Quote
  #14  
Old 01-09-2014, 01:23 AM
macropod's Avatar
macropod macropod is offline Extract form fields to Word Document Windows 7 32bit Extract form fields to Word Document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The names of the source documents is of little consequence as the macro already processes them in name order; what matters is the formfield names. If those vary, the macro would have to be coded to look for all possible names in a given document. Alternatively, if it's always the 3rd formfield, for example, then it really doesn't matter whether the formfield has a name or what that is.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #15  
Old 01-09-2014, 01:33 AM
RonNCmale RonNCmale is offline Extract form fields to Word Document Windows 7 64bit Extract form fields to Word Document Office 2003
Novice
Extract form fields to Word Document
 
Join Date: Sep 2012
Posts: 15
RonNCmale is on a distinguished road
Default

If I'm following you correctly, The formfields will have different names but will be the forth formfield on each document.

So, If im still in the ballpark, form1, formfield four, will be the first paragraph, followed by form2, formfield four, will be the second paragraph, so on and soforth.

If this is correct this will work great. Will the text from formfield 4 on form1 be placed under RE: with a space at the end of the text, followed by Form2, formfield four, with a space at the end of that text, followed by any additional forms.

Hope this make sense. I'm starting to get confussed!!!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting spreadsheet data rows as form fields in a document b3nz Word 3 03-31-2013 07:47 PM
Form Fields in Word jwm1346 Word 1 04-17-2012 07:02 PM
Extract form fields to Word Document Issues with Microsoft Word Form Fields hbforsyth Word 9 11-14-2011 04:26 PM
Calculating Form Fields in Microsoft Word wubba80 Word 1 06-25-2010 12:42 AM
Form fields in Word messed up mba Word VBA 0 02-07-2010 09:54 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:23 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft