From Excel to Word - empty docs?

Jaymond Flurrie

Well-known Member
Joined
Sep 22, 2008
Messages
919
Office Version
  1. 365
Platform
  1. Windows
I have an Excel file that uses VBA to print stuff into Word. It works otherwise well, but when Word is already running when I run my program, it creates empty files. If I use CTRL+ALT+DEL to kill Word processes from background before running my file, it works just like I want.

Can I avoid this "already existing Word messing things"-problem somehow?

Here's the cleaned code I use right now:
Code:
Sub TeeDocTiedostot()
    Dim appWD As Word.Application
    Dim wdRngTable As Word.Range   'create a range variable
    Dim j As Integer
    Dim i As Integer
    Dim strDocTiedosto As String
    Dim strKansionNimi As String
    
    'Käytetään kansion nimenä tätä hetkeä
    strKansionNimi = Format(Now, "dd-mm-yy-hh-mm-ss")
    
    'Tehdään uusi kansio, jonne Word-tiedostot tulostetaan
    MkDir ThisWorkbook.Path & Application.PathSeparator & strKansionNimi
    
    'Avataan ja näytetään Word
    Set appWD = CreateObject("Word.Application.14")
    appWD.Visible = False

    'Käydään palautteen saajat läpi
    For j = LBound(vPalautteet, 1) To UBound(vPalautteet, 1) - 1
        
        'Käske Wordia tekemään uusi dokumentti
        appWD.Documents.Add
    
        'Haetaan Wordista oikea kohta
        Set wdRngTable = ActiveDocument.Content
        wdRngTable.Collapse Direction:=wdCollapseEnd
        
        With ActiveDocument
            'Ja luodaan uusi taulu
            .Tables.Add wdRngTable, 1, 2
            
            With .Tables(1)
                .PreferredWidth = InchesToPoints(10#)
                .Range.Font.Size = 10
                .Range.Font.Name = "Arial"
                .Style = "Table Grid"
            End With
        End With
        
        'Rakennetaan tiedostonimi
        strDocTiedosto = ThisWorkbook.Path & Application.PathSeparator & strKansionNimi & Application.PathSeparator & j & ".doc"
        
        'Tallennetaan tiedosto
        appWD.ActiveDocument.SaveAs (strDocTiedosto)
        
        'Suljetaan tämä dokumentti
        appWD.ActiveDocument.Close
    Next j
    
    'Suljetaan word
    appWD.Quit
    Set appWD = Nothing
End Sub

Do I start or end the use of Word in some bad way when I do like this? What's a better way to handle things?
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,216,586
Messages
6,131,582
Members
449,656
Latest member
pavankumar1421

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