I'm hoping you guys can help me with a simple loop problem. I have some VB code that takes a particular range of cells and copies and pastes them into a Word doc as a picture. The users have the ability to show and hide rows as needed in the worksheet. I need to just pick up the first 45 visible rows and copy/paste those into my doc. Then pick up the next 45 visible rows and copy/paste those and so on until it gets to row 329, then it should stop.
Below is what I have without the looping. I'm a real newbie, so be gently with me.
Below is what I have without the looping. I'm a real newbie, so be gently with me.
Code:
Dim source As Range
Dim wdApp As Object
Dim wdDoc As Object
'Create a hidden instance of Word
Set wdApp = CreateObject("Word.Application")
'Add a document.
Set wdDoc = wdApp.Documents.Add
With ActiveSheet
Set source = .Range("A289:F329").SpecialCells(xlCellTypeVisible)
source.Copy
End With
'name the .doc file from a cell on the sheet
strname = ThisWorkbook.Sheets("Quote Letter").Range("E14").Value & " Quote Letter"
With wdDoc
wdDoc.PageSetup.LeftMargin = "0.5"
wdDoc.PageSetup.RightMargin = "0.5"
wdDoc.PageSetup.TopMargin = "0.75"
wdDoc.PageSetup.BottomMargin = "0.75"
'Paste the source-range into the active document as a picture
wdApp.Selection.Range.PasteSpecial Link:=False, DataType:=3, _
Placement:=wdInLine, DisplayAsIcon:=False
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter
End With
With ActiveSheet
Set source = .Range("A70:F287").SpecialCells(xlCellTypeVisible)
source.Copy
End With
With wdDoc
wdDoc.PageSetup.LeftMargin = "0.5"
wdDoc.PageSetup.RightMargin = "0.5"
wdDoc.PageSetup.TopMargin = "0.75"
wdDoc.PageSetup.BottomMargin = "0.75"
wdApp.Selection.Range.PasteSpecial Link:=False, DataType:=3, _
Placement:=wdInLine, DisplayAsIcon:=False
wdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
End With
With ActiveSheet
Set source = .Range("A1:F69").SpecialCells(xlCellTypeVisible)
source.Copy
End With
With wdDoc
wdDoc.PageSetup.LeftMargin = "0.5"
wdDoc.PageSetup.RightMargin = "0.5"
wdDoc.PageSetup.TopMargin = "0.75"
wdDoc.PageSetup.BottomMargin = "0.75"
wdApp.Selection.Range.PasteSpecial Link:=False, DataType:=3, _
Placement:=wdInLine, DisplayAsIcon:=False
End With
With wdDoc
'Save & Close the document
.SaveAs ThisWorkbook.Path & "\" & strname & ".doc"
.Close
End With
'Close the hidden instance of Word
wdApp.Quit
Application.CutCopyMode = False