D_Marcel
05-17-2016, 11:31 AM
Dears,
Greetings!
I'm facing a very tricky situation related to the MS Word, since I don't know any VBA object to this application. I'm in a system implementation project and I have to perform more than two hundred test scenarios, each one properly evidenced and documented. I have all these scenarios organized in a Worksheet and the idea is to create one page in Word to each row in Excel. Combining my skills in VBA to Excel and what I found in the forums, I got this:
Sub Create_Tests_Document()
Dim LV_WordApp As Object
Dim LV_RowsCounter As Integer
Dim LV_RowIndex As Integer
LV_RowsCounter = ActiveSheet.UsedRange.Rows.Count
Set LV_WordApp = CreateObject("Word.Application")
LV_WordApp.Visible = True
LV_WordApp.Documents.Open "C:\Users\douglasmmm\desktop\MyFile.docx"
For LV_RowIndex = 2 To LV_RowsCounter
LV_WordApp.Selection.GoTo What:=wdGoToPage, Name:=LV_RowIndex - 1
LV_WordApp.ActiveDocument.Bookmarks("\Page").Range.Copy
LV_WordApp.Selection.GoTo What:=wdGoToPage, Name:=LV_RowIndex - 1
LV_WordApp.Selection.Paste
With LV_WordApp.ActiveDocument.Tables(LV_RowIndex - 1)
.Cell(2, 1).Range.Text = Cells(LV_RowIndex, 2).Value
.Cell(2, 2).Range.Text = Cells(LV_RowIndex, 10).Value
End With
Next LV_RowIndex
End Sub
But it's not working properly because, although the code is designed to copy the last page, only the first page is copied, so I
can't update the table of the page.
What I need is:
1. Copy the Last Page;
2. Paste the Copied Page after the Last Page;
3. Edit the table of this last page with the number and the description of the scenario, according to the value of the variable LV_RowIndex in the For Each Next structure.
Can someone please help me?
Thanks a lot!
Greetings!
I'm facing a very tricky situation related to the MS Word, since I don't know any VBA object to this application. I'm in a system implementation project and I have to perform more than two hundred test scenarios, each one properly evidenced and documented. I have all these scenarios organized in a Worksheet and the idea is to create one page in Word to each row in Excel. Combining my skills in VBA to Excel and what I found in the forums, I got this:
Sub Create_Tests_Document()
Dim LV_WordApp As Object
Dim LV_RowsCounter As Integer
Dim LV_RowIndex As Integer
LV_RowsCounter = ActiveSheet.UsedRange.Rows.Count
Set LV_WordApp = CreateObject("Word.Application")
LV_WordApp.Visible = True
LV_WordApp.Documents.Open "C:\Users\douglasmmm\desktop\MyFile.docx"
For LV_RowIndex = 2 To LV_RowsCounter
LV_WordApp.Selection.GoTo What:=wdGoToPage, Name:=LV_RowIndex - 1
LV_WordApp.ActiveDocument.Bookmarks("\Page").Range.Copy
LV_WordApp.Selection.GoTo What:=wdGoToPage, Name:=LV_RowIndex - 1
LV_WordApp.Selection.Paste
With LV_WordApp.ActiveDocument.Tables(LV_RowIndex - 1)
.Cell(2, 1).Range.Text = Cells(LV_RowIndex, 2).Value
.Cell(2, 2).Range.Text = Cells(LV_RowIndex, 10).Value
End With
Next LV_RowIndex
End Sub
But it's not working properly because, although the code is designed to copy the last page, only the first page is copied, so I
can't update the table of the page.
What I need is:
1. Copy the Last Page;
2. Paste the Copied Page after the Last Page;
3. Edit the table of this last page with the number and the description of the scenario, according to the value of the variable LV_RowIndex in the For Each Next structure.
Can someone please help me?
Thanks a lot!