white_flag
Active Member
- Joined
- Mar 17, 2010
- Messages
- 331
Hello
I have the following code that will copy and paste some ranges from excel to word, then it will try to save the file in an local path.
My problem is with saving *.doc I received error 91 and I do not know to explain or to fix this line: Because from my point of view the application.word (object) it is working in background.
this is the rest of my code.
any idea what can by fix this?
I have the following code that will copy and paste some ranges from excel to word, then it will try to save the file in an local path.
My problem is with saving *.doc I received error 91 and I do not know to explain or to fix this line: Because from my point of view the application.word (object) it is working in background.
Code:
wdApp.ActiveDocument.SaveAs Filename:=ThisWorkbook.Path & "\copyfromexcel" - HERE I HAVE ERROR 91
this is the rest of my code.
any idea what can by fix this?
Code:
Option Explicit
Sub CopyWorksheetsToWord()
' requires a reference To the Word Object library:
' in the VBE select Tools, References and check the Microsoft Word X.X object library
Dim OFile As String
Dim filetoopen As String
Dim WeDone As Long
Dim tableTemp As Table
Dim rngTemp As Range
Dim i As Long
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim ws As Worksheet
Dim mydoc As String
Dim myAppl As String
On Error GoTo Err2:
Calculation:
mydoc = ThisWorkbook.Path & "\copyfromexcel.doc"
myAppl = "Word.Application"
'Thisworkbook.path
Set wdDoc = GetObject(mydoc)
With ws
With ThisWorkbook.Worksheets(Me.Controls("Combobox1").Value)
.Range("B1").Copy
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Paste
End With
With ThisWorkbook.Worksheets(Me.Controls("Combobox1").Value)
.Range(.Cells(25, 1), .Cells.SpecialCells(xlCellTypeLastCell)).Copy
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Paste
End With
Application.CutCopyMode = False
With wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range
.InsertParagraphBefore
.Collapse Direction:=wdCollapseEnd
.InsertBreak Type:=wdPageBreak
End With
End With
For i = 1 To wdDoc.Tables.Count
wdDoc.Tables(i).Select
wdDoc.Tables(i).AutoFitBehavior wdAutoFitWindow
' With Selection
' .Font.Bold = True
' .Font.Italic = False
' .Font.Name = "Arial"
' .Font.Size = "20"
' End With
Next i
wdApp.ActiveDocument.SaveAs Filename:=ThisWorkbook.Path & "\copyfromexcel" - HERE I HAVE ERROR 91
Set wdDoc = Nothing
Set wdApp = Nothing
Set tableTemp = Nothing
Set rngTemp = Nothing
Set ws = Nothing
'Reset
With Application
.StatusBar = False
.ScreenUpdating = True
End With
OFile = ActiveWorkbook.Name
Exit Sub
Err2:
If Err.Number <> 0 Then
Select Case Err.Number
Case 432
With Application
.ScreenUpdating = False
.StatusBar = "Creating new document..."
End With
Set wdApp = New Word.Application
Set wdDoc = wdApp.Documents.Add
wdApp.Visible = True
ActiveDocument.SaveAs ThisWorkbook.Path & "\copyfromexcel"
GoTo Calculation:
Case Else
MsgBox Err.Number
End Select
End If
End Sub
Private Sub CommandButton3_Click()
CopyWorksheetsToWord
End Sub
Sub UserForm_Initialize()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
Select Case ws.Name
Case "Index", "Calculation", "Data"
Case Else
ComboBox1.AddItem ws.Name
End Select
Next
End Sub