mantooth29
08-06-2012, 02:10 PM
I am automating Excel from Word, but am running into issues when trying to name a range that my For Each Next loop needs to run through.
I have tried fully qualifying the range 'xlattach' in many different ways, but get a variety of errors from "Method of... Global failed" to Application Defined Error.
Can someone tell me the proper way to set an excel range from a different application?
This line of code is the offender
Set xlattach = Excel.Application.ActiveWorkbook.Worksheets("Output").Range(xlattach)
Here is the full code
Sub Attach()
Dim xl As Excel.Application, wb As Excel.Workbook, xlattach As Excel.Range, xlfilepath As Excel.Range
Dim a As String
Set xl = CreateObject("Excel.Application")
xl.Visible = True
Set xlattach = Excel.Application.ActiveWorkbook.Worksheets("Output").Range(xlattach)
'--gave up on naming range in VBA, switched to a name in the workbook
'Set xlattach = wb.Range(xlattach, xlattach.End(xlDown))
Set wb = xl.Workbooks.Open("C:\Mail Merge Testing\-TestFile.xls")
With Selection
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Doc2??"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute
End With
'-- The Select with below was a test
With wb.Worksheets("Output").Range("Bk2")
.Select
End With
'-- End Test
For Each xlfilepath In xlattach
a = xlfilepath.Text
With ActiveDocument
Selection.InlineShapes.AddOLEObject ClassType:="AcroExch.Document.7", _
FileName:="C:\Mail Merge Testing\PDFs\" & a & ".pdf", LinkToFile:=False, _
DisplayAsIcon:=True, IconFileName:= _
"C:\Windows\Installer\_PDFFile.ico" _
, IconIndex:=0, IconLabel:=a & ".pdf"
End With
Next xlfilepath
End With
End Sub
I am pretty good with Excel VBA but am finding that communicating between two or more applications is a whole new challenge.
I have tried fully qualifying the range 'xlattach' in many different ways, but get a variety of errors from "Method of... Global failed" to Application Defined Error.
Can someone tell me the proper way to set an excel range from a different application?
This line of code is the offender
Set xlattach = Excel.Application.ActiveWorkbook.Worksheets("Output").Range(xlattach)
Here is the full code
Sub Attach()
Dim xl As Excel.Application, wb As Excel.Workbook, xlattach As Excel.Range, xlfilepath As Excel.Range
Dim a As String
Set xl = CreateObject("Excel.Application")
xl.Visible = True
Set xlattach = Excel.Application.ActiveWorkbook.Worksheets("Output").Range(xlattach)
'--gave up on naming range in VBA, switched to a name in the workbook
'Set xlattach = wb.Range(xlattach, xlattach.End(xlDown))
Set wb = xl.Workbooks.Open("C:\Mail Merge Testing\-TestFile.xls")
With Selection
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Doc2??"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute
End With
'-- The Select with below was a test
With wb.Worksheets("Output").Range("Bk2")
.Select
End With
'-- End Test
For Each xlfilepath In xlattach
a = xlfilepath.Text
With ActiveDocument
Selection.InlineShapes.AddOLEObject ClassType:="AcroExch.Document.7", _
FileName:="C:\Mail Merge Testing\PDFs\" & a & ".pdf", LinkToFile:=False, _
DisplayAsIcon:=True, IconFileName:= _
"C:\Windows\Installer\_PDFFile.ico" _
, IconIndex:=0, IconLabel:=a & ".pdf"
End With
Next xlfilepath
End With
End Sub
I am pretty good with Excel VBA but am finding that communicating between two or more applications is a whole new challenge.