PorscheGT2
Programmer
Hello All,
I've been tasked by my manager to create a macro that would copy a range of cells from MS Excel to MS Word. So basically when it gets transferred to Word, it becomes a table. Here's the code:
Application.ScreenUpdating = False
Dim appWD As Word.Application, docWD As Word.Document, appXL As Excel.Application
Range("A1:M68").Select
Selection.Copy
Set appWD = CreateObject("Word.Application.8")
With appWD
.ScreenUpdating = False
.Visible = True
.Documents.Add
.WindowState = wdWindowStateMaximize
End With
Set docWD = appWD.ActiveDocument
With docWD.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(0.4)
.BottomMargin = InchesToPoints(0.4)
.LeftMargin = InchesToPoints(1.25)
.RightMargin = InchesToPoints(1.25)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(8.5)
.PageHeight = InchesToPoints(11)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
End With
With appWD
.Selection.Paste
.Selection.WholeStory
.Selection.Font.Size = 7
.Selection.HomeKey Unit:=wdStory
.ScreenUpdating = True
End With
Set appWD = Nothing
Set docWD = Nothing
Set appXL = Nothing
End Sub
This code works well when you don't have MS Word open and if you're running it the first time. But when I run it again, regardless of whether I close Word or not, it gives me a Run Time Error, something like, "RCP server not found!" or something like that. And this error comes up every other time I run the macro. I know that the reason for this is that there's something like a "residual" instance that gets left behind when I close Word. How do you check for that? And how do you create a condition that would open an existing instance of Word when it's there, and create one if there's none? Any help would be appreciated.
Thanks!
I've been tasked by my manager to create a macro that would copy a range of cells from MS Excel to MS Word. So basically when it gets transferred to Word, it becomes a table. Here's the code:
Application.ScreenUpdating = False
Dim appWD As Word.Application, docWD As Word.Document, appXL As Excel.Application
Range("A1:M68").Select
Selection.Copy
Set appWD = CreateObject("Word.Application.8")
With appWD
.ScreenUpdating = False
.Visible = True
.Documents.Add
.WindowState = wdWindowStateMaximize
End With
Set docWD = appWD.ActiveDocument
With docWD.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(0.4)
.BottomMargin = InchesToPoints(0.4)
.LeftMargin = InchesToPoints(1.25)
.RightMargin = InchesToPoints(1.25)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(8.5)
.PageHeight = InchesToPoints(11)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
End With
With appWD
.Selection.Paste
.Selection.WholeStory
.Selection.Font.Size = 7
.Selection.HomeKey Unit:=wdStory
.ScreenUpdating = True
End With
Set appWD = Nothing
Set docWD = Nothing
Set appXL = Nothing
End Sub
This code works well when you don't have MS Word open and if you're running it the first time. But when I run it again, regardless of whether I close Word or not, it gives me a Run Time Error, something like, "RCP server not found!" or something like that. And this error comes up every other time I run the macro. I know that the reason for this is that there's something like a "residual" instance that gets left behind when I close Word. How do you check for that? And how do you create a condition that would open an existing instance of Word when it's there, and create one if there's none? Any help would be appreciated.
Thanks!