PDA

View Full Version : Transfer data from Word to Excel



dinozgb
09-07-2012, 05:09 AM
Hi!
Word file (attach !), located on the network. At each closing Word file, it is necessary to transfer the data into an Excel spreadsheet!
Can you suggest a macro for Word that after closing the word document, directly transferred data in an Excel spreadsheet, taking into account the previous records in an Excel spreadsheet!
Thanks and regards!

macropod
09-09-2012, 05:25 AM
IMHO it's impractical to try to do this from Word. At the very least, it would require logic to permit the updating only when the user says so - not automatically when the file is closed. Doing it automatically when the file is closed would result in entries being added to the workbook even if the document was simply opened for reading or it's updating was incomplete. You'd also need logic to tell Word which file(s) not to treat this way - otherwise it might try to process every file you open.

Here is a macro you can add to the Excel workbook and run from there.
Sub InsertFormfieldResults()
Application.ScreenUpdating = False
Dim lRow As Long, i As Long, StrFile As String
Dim xlWkSht As Worksheet
Set xlWkSht = ActiveSheet
Dim wdApp As New Word.Application
Dim wdDoc As Word.Document
wdApp.Visible = True
With wdApp.Dialogs(wdDialogFileOpen)
If .Show = -1 Then
StrFile = .Name
End If
End With
If StrFile = "" Then GoTo NoFile
lRow = xlWkSht.Cells.SpecialCells(xlCellTypeLastCell).Row + 1
Set wdDoc = wdApp.Documents.Open(Filename:=StrFile, AddToRecentFiles:=False)
With wdDoc
For i = 1 To .FormFields.Count
xlWkSht.Cells(lRow, i).Value = .FormFields(i).Result
Next
.Close SaveChanges:=False
End With
NoFile:
wdApp.Quit
Set wdApp = Nothing
Application.ScreenUpdating = True
End Sub

dinozgb
09-10-2012, 12:17 AM
Hi!
Thanks for the code!
I added at end of the trigger for the initiation of saving data in a pre-defined Excel spreadsheet!
Can you help me with coding for the trigger!
Excel file name is the same as a word file in the same directory!
thanks

macropod
09-10-2012, 12:23 AM
You're evidently not paying attention: the code is for Excel, which means (as I advised in my last post) you add it to the Excel workbook and run it from there.

Aside from that, your attached document has a 'CommandButton1_Click' sub, which belongs in a userform, but there is no userform for it. In any event, for the reasons I've already given, it is unwise to try to do this from Word.

dinozgb
09-10-2012, 02:50 AM
The problem is that excel tables must be available to anyone except an administrator, a word document is the official document!
Excel tables should not be the driving force, rather than just monitoring!
Is there some other way, when data is true in any form, transfer them to an Excel spreadsheet, a button that will trigger the copy or something else?
greeting:(

macropod
09-10-2012, 02:55 AM
The problem is that excel tables must be available to anyone except an administrator
How is that relevant to whether the macro is in an Excel workbook? If necessary, it doesn't even have to be in the same workbook as the data - provided the data workbook is the active one when the macro is run.