copy from excel to word, than error 91 on save

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.

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
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Why are you using ActiveDocument instead if the Word document wdDoc you refer to throughout the rest of the code?
 
Upvote 0
thank you...
Code:
wdDoc.SaveAs Filename:=ThisWorkbook.Path & "\copyfromexcel"

one more with this code:
It will copy an paste on word as an table (values from excel). can be selected but the attributes from table (font, size, etc ..)can not be change.
an better code how will look like?


this one is not working
Code:
        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
 
Upvote 0
Try:

Code:
    With wdDoc.Tables(i).Range
        .Font.Bold = True
        .Font.Italic = False
        .Font.Name = "Arial"
        .Font.Size = "20"
    End With
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,787
Members
452,942
Latest member
VijayNewtoExcel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top