Sub Insert_WordFile_To_sheet()
Dim oWS As Worksheet ' Worksheet Object
Dim oOLEWd As OLEObject ' OLE Word Object
Dim WordApp As Word.Application
Dim indexOfPeriod As String
Dim indexOfFileName As String
Dim FilePathNoName As String
Dim FilePath As String
Dim pageNum As Integer
Dim WordDoc As Word.Document
Dim NewFilePath As String
Dim MyModule As Object
Dim MyModuleName As String
Dim ShapeCount As Integer
MsgBox ("Please be patient while this processes.")
MsgBox ("Click OK to errors about the converter.")
pageNum = 1
FilePath = Filename
NewFilePath = Dir(FilePath)
indexOfPeriod = InStr(1, NewFilePath, ".", vbTextCompare)
indexOfFileName = InStr(1, FilePath, NewFilePath, vbTextCompare)
FilePathNoName = Mid(FilePath, 1, indexOfFileName - 1)
NewFilePath = Mid(NewFilePath, 1, indexOfPeriod - 1)
NewFilePath = NewFilePath & "_000" & pageNum & ".doc"
NewFilePath = FilePathNoName & NewFilePath
'Check if file exists, if not split word doc
If Dir(NewFilePath) = "" Then 'File doesnt exists
Set WordApp = CreateObject("Word.Application")
Set WordDoc = WordApp.Documents.Open _
(FilePath)
WordApp.Visible = True
MyModuleName = "Module1"
On Error Resume Next
Set MyModule = WordDoc.VBProject.vbComponents(MyModuleName).CodeModule
If Err.Number <> 0 Then
MsgBox ("If you get this error, verify you are using the correct word template with : " & MyModuleName)
If GetScopeBtn.Enabled = False Then
GetScopeBtn.Enabled = True
End If
'Exit Sub
End If
WordApp.Run "SplitPages"
WordApp.Quit
Set WordApp = Nothing
End If
NewFilePath = Empty
Do While pageNum > 0
NewFilePath = Dir(FilePath)
NewFilePath = Mid(NewFilePath, 1, indexOfPeriod - 1)
NewFilePath = NewFilePath & "_000" & pageNum & ".doc"
NewFilePath = FilePathNoName & NewFilePath
If Len(Dir(NewFilePath)) <= 0 Then
Exit Do
End If
Set oWS = ActiveSheet
MsgBox (NewFilePath)
'ShapeCount = ActiveSheet.Shapes.Count
'MsgBox (ShapeCount)
Set oOLEWd = oWS.OLEObjects.Add(Filename:=NewFilePath)
oOLEWd.Name = "EmbeddedWordDoc" & pageNum
oOLEWd.Width = 600
oOLEWd.Height = 560
If pageNum = 1 Then
Dim pageOneHeight As Integer
oOLEWd.CopyPicture Appearance:=xlScreen, Format:=xlPicture
Worksheets("Sheet1").Paste _
Destination:=Worksheets("Sheet1").Range("A10")
Selection.Name = "Scope1"
pageOneHeight = Worksheets("Sheet1").Shapes("Scope1").Height
Worksheets("Sheet1").Shapes("Scope1").LockAspectRatio = False
Worksheets("Sheet1").Shapes("Scope1").RelativeToOriginalSize = False
Worksheets("Sheet1").Shapes("Scope1").ScaleWidth 1.52, msoTrue
Worksheets("Sheet1").Shapes("Scope1").ScaleHeight 2.45, msoTrue
Worksheets("Sheet1").Shapes("Scope1").Line.Visible = True
Worksheets("Sheet1").Shapes("Scope1").OnAction = "Scope1_Click"
End If
If pageNum = 2 Then
oOLEWd.CopyPicture Appearance:=xlScreen, Format:=xlPicture
Worksheets("Sheet1").Paste _
Destination:=Worksheets("Sheet1").Range("A74")
Selection.Name = "Scope2"
Worksheets("Sheet1").Shapes("Scope2").LockAspectRatio = False
Worksheets("Sheet1").Shapes("Scope2").ScaleWidth 1.47, msoTrue
Worksheets("Sheet1").Shapes("Scope2").ScaleHeight 2.64, msoTrue
Worksheets("Sheet1").Shapes("Scope2").Line.Visible = True
Worksheets("Sheet1").Shapes("Scope2").OnAction = "Scope2_Click"
End If
If pageNum = 3 Then
oOLEWd.CopyPicture Appearance:=xlScreen, Format:=xlPicture
Worksheets("Sheet1").Paste _
Destination:=Worksheets("Sheet1").Range("O10")
Selection.Name = "Scope3"
Worksheets("Sheet1").Shapes("Scope3").LockAspectRatio = False
Worksheets("Sheet1").Shapes("Scope3").ScaleWidth 1.59, msoTrue
Worksheets("Sheet1").Shapes("Scope3").ScaleHeight 2.14, msoTrue
Worksheets("Sheet1").Shapes("Scope3").Line.Visible = True
Worksheets("Sheet1").Shapes("Scope3").OnAction = "Scope3_Click"
End If
If pageNum = 4 Then
oOLEWd.CopyPicture Appearance:=xlScreen, Format:=xlPicture
Worksheets("Sheet1").Paste _
Destination:=Worksheets("Sheet1").Range("O74")
Selection.Name = "Scope4"
Worksheets("Sheet1").Shapes("Scope4").LockAspectRatio = False
Worksheets("Sheet1").Shapes("Scope4").ScaleWidth 1.57, msoTrue
Worksheets("Sheet1").Shapes("Scope4").ScaleHeight 2.35, msoTrue
Worksheets("Sheet1").Shapes("Scope4").Line.Visible = True
Worksheets("Sheet1").Shapes("Scope4").OnAction = "Scope4_Click"
End If
Range("A1").Select
oOLEWd.Delete
Set oOLEWd = Nothing
pageNum = pageNum + 1
Loop
'used to deselect word object, selects cell A1 after last word object
Range("A1").Select
Set WordApp = CreateObject("Word.Application")
WordApp.Quit SaveChanges:=wdDoNotSaveChanges
pageNum = 1
Do Until pageNum = 5
NewFilePath = ""
NewFilePath = Dir(FilePath)
NewFilePath = Mid(NewFilePath, 1, indexOfPeriod - 1)
NewFilePath = NewFilePath & "_000" & pageNum & ".doc"
NewFilePath = FilePathNoName & NewFilePath
'MsgBox (NewFilePath)
If Dir(NewFilePath) <> "" Then
Kill NewFilePath
End If
pageNum = pageNum + 1
Loop
Set WordApp = Nothing
End Sub