Hi all, I have some code which transfers data in my excel database into a table in word. At the moment it does this for every row in the database.
See code:
Whilst this works great, what i would like to do is only perform one transfer, based on a certain criteria.. the "Works Order Number" as this field is unique.
I would like the user to be able to enter a works order number and then it take just this row of the database and transfer it to Word.
I'm not sure how to go about doing this, any advice would be much appreciated!
EDIT: I was thinking maybe i could have the user type the works order number in a cell, and then the code picks it up from the cell and prints that row, not sure how to go about doing this though.
See code:
VBA Code:
Set wd = CreateObject("Word.Application")
'Set worksheet
Set sh = ThisWorkbook.Sheets("Database")
iRow = 2 'row in which data starts from in database
Do While sh.Range("A" & iRow).Value <> "" 'loop through records till value is blank (end of data)
'opening word template
Set wdDOC = wd.Documents.Add("T:\mageeg\TEST DATA INSPECTION SCHEDULE Issue 3.docx")
wd.Visible = False
'code to insert values from database to bookmarks in word
wd.Selection.GoTo what:=wdGoToBookmark, Name:="PartNo"
wd.Selection.TypeText Text:=sh.Range("C" & iRow).Value
wd.Selection.GoTo what:=wdGoToBookmark, Name:="Serial"
wd.Selection.TypeText Text:=sh.Range("E" & iRow).Value
wd.Selection.GoTo what:=wdGoToBookmark, Name:="ModelNo"
wd.Selection.TypeText Text:=sh.Range("B" & iRow).Value
wd.Selection.GoTo what:=wdGoToBookmark, Name:="WorksOrderNo"
wd.Selection.TypeText Text:=sh.Range("D" & iRow).Value
'save file with new name
wdDOC.SaveAs2 (ThisWorkbook.Path & "\" & sh.Range("D" & iRow).Value & ".docx")
'close the word file
wdDOC.Close
'release memory of word doc
Set wdDOC = Nothing
iRow = iRow + 1
Loop
wd.Quit 'close MS Word
Whilst this works great, what i would like to do is only perform one transfer, based on a certain criteria.. the "Works Order Number" as this field is unique.
I would like the user to be able to enter a works order number and then it take just this row of the database and transfer it to Word.
I'm not sure how to go about doing this, any advice would be much appreciated!
EDIT: I was thinking maybe i could have the user type the works order number in a cell, and then the code picks it up from the cell and prints that row, not sure how to go about doing this though.
Last edited by a moderator: