red102
08-08-2014, 03:35 AM
Hi,
The below code copies the contents of some fields in one word document over to corresponding fields in another, but it is reliant on the source form having a static name, and both files being in the same directory. What I would like to achieve is being able to have the destination document in any directory and when run, the code opens a explorer window allowing me to navigate to, and open my source document irrelevant of what it is called.
I’ve been experimenting with application.filedialog(msofiledialogopen) with little success….and much frustration. If this is not the best way to go, I'm open to suggestions.
Any help or guidance would be greatly appreciated. You can probably tell form the structure of my code and the question that I’m asking, that I’m only just starting with VBA :cloud9:
Sub CopyFields()
'when the log changes this has to be edited...
Dim logName As String
logName = "LOG_1.doc" ' <--------------
'defines nice names for the two documents...
Dim LogFile As Word.Document
Dim ThisREPORTFile As Word.Document
Dim filePath As String
Dim REPORTName As String
Set ThisREPORTFile = ActiveDocument
filePath = ThisREPORTFile.Path 'assumes log in same directory as report
REPORTName = ThisREPORTFile.Name
'sets up these definitions for convenience...
Dim oREPORTFields As FormFields
Dim oLogFields As FormFields
Set oREPORTFields = ThisREPORTFile.FormFields
Set oLogFields = LogFile.FormFields
'copies fields
oREPORTFields("ReportFiled1").Result = oLogFields("logFiled1").Result
oREPORTFields("ReportFiled2").Result = oLogFields("logFiled2").Result
oREPORTFields("ReportFiled3").Result = oLogFields("logFiled3").Result
oREPORTFields("ReportFiled4").Result = oLogFields("logFiled4").Result
End Sub
The below code copies the contents of some fields in one word document over to corresponding fields in another, but it is reliant on the source form having a static name, and both files being in the same directory. What I would like to achieve is being able to have the destination document in any directory and when run, the code opens a explorer window allowing me to navigate to, and open my source document irrelevant of what it is called.
I’ve been experimenting with application.filedialog(msofiledialogopen) with little success….and much frustration. If this is not the best way to go, I'm open to suggestions.
Any help or guidance would be greatly appreciated. You can probably tell form the structure of my code and the question that I’m asking, that I’m only just starting with VBA :cloud9:
Sub CopyFields()
'when the log changes this has to be edited...
Dim logName As String
logName = "LOG_1.doc" ' <--------------
'defines nice names for the two documents...
Dim LogFile As Word.Document
Dim ThisREPORTFile As Word.Document
Dim filePath As String
Dim REPORTName As String
Set ThisREPORTFile = ActiveDocument
filePath = ThisREPORTFile.Path 'assumes log in same directory as report
REPORTName = ThisREPORTFile.Name
'sets up these definitions for convenience...
Dim oREPORTFields As FormFields
Dim oLogFields As FormFields
Set oREPORTFields = ThisREPORTFile.FormFields
Set oLogFields = LogFile.FormFields
'copies fields
oREPORTFields("ReportFiled1").Result = oLogFields("logFiled1").Result
oREPORTFields("ReportFiled2").Result = oLogFields("logFiled2").Result
oREPORTFields("ReportFiled3").Result = oLogFields("logFiled3").Result
oREPORTFields("ReportFiled4").Result = oLogFields("logFiled4").Result
End Sub