![]() |
#1
|
|||
|
|||
![]()
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 |
#2
|
||||
|
||||
![]()
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
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. |
#4
|
||||
|
||||
![]()
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 Code:
wdTgtDoc.Range.InsertAfter .FormFields("MyField").Result & vbCr
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]()
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 With wdSrcDoc 'Do your processing here, ' but not knowing how to code, it evidently needs more. I keep getting an error code. |
#6
|
||||
|
||||
![]()
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] |
#7
|
|||
|
|||
![]()
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 wdTgtDoc.Range.InsertAfter .FormFields("MyField").Result & vbCr Form1, Form2, and Form3 need to be in paragraph form in sequential order on the target document |
#8
|
||||
|
||||
![]()
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] |
#9
|
|||
|
|||
![]()
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 |
#10
|
||||
|
||||
![]()
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:
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] |
#11
|
|||
|
|||
![]()
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. |
#12
|
||||
|
||||
![]()
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] |
#13
|
|||
|
|||
![]()
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.
|
#14
|
||||
|
||||
![]()
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] |
#15
|
|||
|
|||
![]()
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!!! |
![]() |
|
![]() |
||||
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 |
![]() |
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 |