Oddball2020
Board Regular
- Joined
- May 6, 2021
- Messages
- 69
- Office Version
- 365
- Platform
- Windows
Thanks in advance for any assistance in helping correct this. I've been using this forum in my CVA reasearch for 6 yrs, and have always been able to figure out a solution from the excellent support it provides, but this is the first time I'm actually posting a question to it as dealing with Word from Excel VBA is a frustrating challenge for me.
I'm running Win 10 Enterprise and Excel & Word 2016.
I've made a VBA tool to that updates a table in an excel worksheet in a loop:
1) updating the table info from the source data worksheet (in a list w/ several columns) of rows
2) copying/pasting the table range into Word at the end paragraph
3) repeat on each iteration of the loop
4) saving/close the file as a doxc and pdf with a user defined file name (not included in my code below)
The issue I'm having is that though it works perfectly fine for the first 4 iterations in the loop, after the 5th iteration the pasting into Word gets wonky as the table format gets out of whack somehow. I find it odd that it's ok the first 4 times but not after, and there's obviously something I'm overlooking with the nusiences of dealing with Word from the Excel VBA side.
I know there's a better way than to finesse the margins in Word and the table range such that the paste into word causes the ending paragraph to be at the start/top of a new page, for the next iteration to paste to, and to force an autofit of the table into word and not cause the bottom of the table range to wrap onto the next page. I've even toyed with using bookmarks and named tables as other paste methods, but I suspect there's something else going on that I'm overlooking.
The Word docx settings are: T 0.5", B 0.21", L 0.5", R 0.23" with a footer... otherwise it's a blank page
I'm running Win 10 Enterprise and Excel & Word 2016.
I've made a VBA tool to that updates a table in an excel worksheet in a loop:
1) updating the table info from the source data worksheet (in a list w/ several columns) of rows
2) copying/pasting the table range into Word at the end paragraph
3) repeat on each iteration of the loop
4) saving/close the file as a doxc and pdf with a user defined file name (not included in my code below)
The issue I'm having is that though it works perfectly fine for the first 4 iterations in the loop, after the 5th iteration the pasting into Word gets wonky as the table format gets out of whack somehow. I find it odd that it's ok the first 4 times but not after, and there's obviously something I'm overlooking with the nusiences of dealing with Word from the Excel VBA side.
I know there's a better way than to finesse the margins in Word and the table range such that the paste into word causes the ending paragraph to be at the start/top of a new page, for the next iteration to paste to, and to force an autofit of the table into word and not cause the bottom of the table range to wrap onto the next page. I've even toyed with using bookmarks and named tables as other paste methods, but I suspect there's something else going on that I'm overlooking.
The Word docx settings are: T 0.5", B 0.21", L 0.5", R 0.23" with a footer... otherwise it's a blank page
VBA Code:
Sub Save_To_Word()
Dim wkbD As Workbook
Set wkbD = ThisWorkbook
Dim folderPath As String
folderPath = Application.ThisWorkbook.Path
Application.DisplayAlerts = False
Sheets("Summary").Activate
Dim LastRowC As Long
LastRowC = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row ‘determines length of the data list from the length of Column B
Dim i As Long
Dim MemoTemplatefolder As String
Dim objWord As Word.Application
Dim objDoc As Word.Document
'Determine if Word application is already running…. Found that it was causing errors if so
If Not (objWord Is Nothing) Then
objWord.Quit (False)
Set objWord = Nothing
End If
MemoTemplatefolder = folderPath & "\Template.docx" 'where the Word template is located
Set objWord = CreateObject("Word.Application") 'open Word application
Set objDoc = objWord.Documents.Open(MemoTemplatefolder)
objWord.Visible = True
Dim TStart
TStart = Timer
Dim PC As Long
For i = 5 To LastRowC ‘the header section is on rows 1-4… data starts in row 5+
Sheets("CDRL Template").Cells(1, 16).Value = i ‘ the table cells automatically update from the offset/match formula to display the data according to the row # this corresponds to
Sheets("CDRL Template").Calculate ‘precaution to force excel to calculate the formulas in the table content
Sheets("CDRL Template").Activate
Range(Cells(1, 1), Cells(45, 13)).Copy ‘this is the range of the template table that is automatically updated after the Cell(1 ,16) is updated on each iteration… I’m not using a named range or Table
'START Loop to save memos
If i = 1 Then
objDoc.Paragraphs(1).Range.PasteAndFormat wdFormatOriginalFormatting ‘thought forcing word to use the excel formatting would help in Word… don’t think it makes a difference
Application.CutCopyMode = False
Else 'after 1st iteration,
'objDoc.Range(objDoc.Characters.Count - 1).Paste ‘originally used to determine last paragraph
Do
DoEvents
Loop While (TStart + 1) > Timer ‘added this after I thought the macro was rushing too fast and pasting into previous paragraphs in Word; don't think it makes a difference
PC = objDoc.Paragraphs.Count 'Determines last paragraph, established after previous table paste; … given the range/size of the table, this always ends up 261 paragraphs from the previous in Word, at top of a new pg
objDoc.Paragraphs(PC).Range.PasteAndFormat wdFormatOriginalFormatting 'pastes to last paragraph
Application.CutCopyMode = False
End If
Next i
'Save/Close Word file: I removed this section of code for the forum post as there’s no issues with saving/closing the file as .doxc and .pdf
Set objDoc = Nothing
objWord.Quit
Set objWord = Nothing
Application.DisplayAlerts = True
End Sub