S
samanco
I have a macro used to copy a range from a spreadshhet into a word
document and formats the table, etc. I have been trying to be
consistent and using Option Explicit across the whole application. I
think have decaled all the variables in this macro but it only works
as far as the saveas statement, then the error capture kicks in and
quits the macro. However if I remove the Option Explicit declaration
then the macro completes fully including the saveAs element as
required. If I step through the macro using f8 it does not report a
variable undeclared, if I remove the error traps then still no debug
but ends without the filesave section completing? Any ideas ?
Option Explicit
Sub autoword()
Dim wdApp As Object
ThisWorkbook.Sheets("METstats").Range("b6:h123").Copy 'Change me
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If wdApp Is Nothing Then
Set wdApp = GetObject("", "Word.Application")
End If
On Error GoTo 0
With wdApp
..Documents.Add
..Visible = True
End With
With wdApp.Selection
..Paste
..PageSetup.Orientation = wdOrientLandscape
..Tables(1).Select
..Tables(1).AutoFitBehavior (wdAutoFitWindow)
..Tables(1).AutoFitBehavior (wdAutoFitWindow)
..Font.Size = 10
..Tables(1).AutoFitBehavior (wdAutoFitWindow)
..Tables(1).AutoFitBehavior (wdAutoFitWindow)
..Style = "Table Simple 1"
End With
On Error GoTo Quit ' this is where things seen to go wrong
With wdApp 'change filename
'ChangeFileOpenDirectory "C:\temp\" 'remove comment tag and edit path
as required
ActiveDocument.SaveAs Filename:="METstatsrep.doc", FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
End With
Quit:
Set wdApp = Nothing
End Sub
document and formats the table, etc. I have been trying to be
consistent and using Option Explicit across the whole application. I
think have decaled all the variables in this macro but it only works
as far as the saveas statement, then the error capture kicks in and
quits the macro. However if I remove the Option Explicit declaration
then the macro completes fully including the saveAs element as
required. If I step through the macro using f8 it does not report a
variable undeclared, if I remove the error traps then still no debug
but ends without the filesave section completing? Any ideas ?
Option Explicit
Sub autoword()
Dim wdApp As Object
ThisWorkbook.Sheets("METstats").Range("b6:h123").Copy 'Change me
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If wdApp Is Nothing Then
Set wdApp = GetObject("", "Word.Application")
End If
On Error GoTo 0
With wdApp
..Documents.Add
..Visible = True
End With
With wdApp.Selection
..Paste
..PageSetup.Orientation = wdOrientLandscape
..Tables(1).Select
..Tables(1).AutoFitBehavior (wdAutoFitWindow)
..Tables(1).AutoFitBehavior (wdAutoFitWindow)
..Font.Size = 10
..Tables(1).AutoFitBehavior (wdAutoFitWindow)
..Tables(1).AutoFitBehavior (wdAutoFitWindow)
..Style = "Table Simple 1"
End With
On Error GoTo Quit ' this is where things seen to go wrong
With wdApp 'change filename
'ChangeFileOpenDirectory "C:\temp\" 'remove comment tag and edit path
as required
ActiveDocument.SaveAs Filename:="METstatsrep.doc", FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
End With
Quit:
Set wdApp = Nothing
End Sub