S
Stephen Moon
Hello,
I have the following VBA script, but I have a problem running this one
because
I get "subscript out of range" error when I try to copy and paste the
excel chart to word. If you can help me to debug this program, I will
very much appreciate.
-Stephen Moon
Sub ExcelChart2Word()
'
' Copies an excel chart to word from a series of excel spreadsheets
' by Stephen Moon on September 8, 2003
'
Dim TextLine, DirName, PrintOrder, Message, Title, Default
Dim xlApp As Object ' Declare variable to hold the reference.
Set xlApp = CreateObject("excel.application.9")
' You may have to set Visible property to True
' if you want to see the application.
Message = "Enter a full directory path" ' Set prompt.
Title = "Input Directory Path" ' Set title.
Default = "" ' Set default.
' Display dialog box at position 100, 100.
DirName = InputBox(Message, Title, Default, 100, 100)
If DirName = "" Then
MsgBox "Directory path not specified"
Exit Sub
End If
Message = "Specify the name of the Print Order file (e.g. printorder)"
' Set prompt.
Title = "Input a print order file name" ' Set title.
Default = "" ' Set default.
' Display dialog box at position 100, 100.
filename = InputBox(Message, Title, Default, 100, 100) & ".txt"
If filename = ".txt" Then
MsgBox "There was no print order file"
Exit Sub
End If
Open DirName & "\" & filename For Input As #1 ' Open file.
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, TextLine ' Read line into variable.
Debug.Print TextLine ' Print to the Immediate window.
With Selection
.TypeText Text:="Name of the File is: " & DirName & "\" &
TextLine
.TypeParagraph
'.InlineShapes.AddPicture(filename:=DirName & "\" &
TextLine, LinkToFile:=False, SaveWithDocument:=True)
.TypeParagraph
error here=> xlApp.workbooks(DirName & "\" &
TextLine).charts("Combined Spectra").ChartArea.Copy
.PasteSpecial link:=False,
datatype:=wdPasteMetafilePicture, placement:=wdfloateovertext,
DisplayAsIcon:=False
.TypeParagraph
.TypeParagraph
.InsertCaption Label:="Figure",
TitleAutoText:="InsertCaption1", _
Title:="", Position:=wdCaptionPositionBelow
.TypeParagraph
.TypeParagraph
'.InsertBreak Type:=wdSectionBreakNextPage
.Collapse Direction:=wdCollapseEnd
End With
Loop
Close #1 ' Close file.
Selection.WholeStory
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
End Sub
I have the following VBA script, but I have a problem running this one
because
I get "subscript out of range" error when I try to copy and paste the
excel chart to word. If you can help me to debug this program, I will
very much appreciate.
-Stephen Moon
Sub ExcelChart2Word()
'
' Copies an excel chart to word from a series of excel spreadsheets
' by Stephen Moon on September 8, 2003
'
Dim TextLine, DirName, PrintOrder, Message, Title, Default
Dim xlApp As Object ' Declare variable to hold the reference.
Set xlApp = CreateObject("excel.application.9")
' You may have to set Visible property to True
' if you want to see the application.
Message = "Enter a full directory path" ' Set prompt.
Title = "Input Directory Path" ' Set title.
Default = "" ' Set default.
' Display dialog box at position 100, 100.
DirName = InputBox(Message, Title, Default, 100, 100)
If DirName = "" Then
MsgBox "Directory path not specified"
Exit Sub
End If
Message = "Specify the name of the Print Order file (e.g. printorder)"
' Set prompt.
Title = "Input a print order file name" ' Set title.
Default = "" ' Set default.
' Display dialog box at position 100, 100.
filename = InputBox(Message, Title, Default, 100, 100) & ".txt"
If filename = ".txt" Then
MsgBox "There was no print order file"
Exit Sub
End If
Open DirName & "\" & filename For Input As #1 ' Open file.
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, TextLine ' Read line into variable.
Debug.Print TextLine ' Print to the Immediate window.
With Selection
.TypeText Text:="Name of the File is: " & DirName & "\" &
TextLine
.TypeParagraph
'.InlineShapes.AddPicture(filename:=DirName & "\" &
TextLine, LinkToFile:=False, SaveWithDocument:=True)
.TypeParagraph
error here=> xlApp.workbooks(DirName & "\" &
TextLine).charts("Combined Spectra").ChartArea.Copy
.PasteSpecial link:=False,
datatype:=wdPasteMetafilePicture, placement:=wdfloateovertext,
DisplayAsIcon:=False
.TypeParagraph
.TypeParagraph
.InsertCaption Label:="Figure",
TitleAutoText:="InsertCaption1", _
Title:="", Position:=wdCaptionPositionBelow
.TypeParagraph
.TypeParagraph
'.InsertBreak Type:=wdSectionBreakNextPage
.Collapse Direction:=wdCollapseEnd
End With
Loop
Close #1 ' Close file.
Selection.WholeStory
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
End Sub