Open word doc and format

Stoven

New Member
Joined
Oct 24, 2009
Messages
33
Hello
I need to open a blank word doc, paste a spreadsheet onto it, format it and filter out some keywords. All from excel of course.
1) Access the internet web page - done
2) Open the excel ss - done
3) Open word - not done
4) Paste the ss onto word - not done
5) Format the doc - not done
6) Filter out rows containing keywords - not done

Just to get started I tried this code to open an existing word doc but it doesn't work
Code:
Private Sub CommandButton1_Click()
    Set wrdApp = CreateObject("Word.Application")
    Set wrdDoc = wrdApp.Documents.Open("C:\BShift.doc")
End Sub
It says file cannot be found. There is a file there named BShift in word 2010.
 

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.
Maybe you haven't added a Word Object reference?

Tools > Reference > Microsoft Word 12.0 Object Library > Tick it > OK

Code:
Sub a()
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
    Set wrdDoc = wrdApp.Documents.Open ("C:\BShift.doc")
    
End Sub
 
Upvote 0
Is that file in C:\?

Also, is it's extension definitely doc?

The file extension for Word 2010 documents is docx.
 
Upvote 0
Yes, I put a reference to word object library.
This code opens a blank doc
Code:
Dim wdApp As Object
    Dim wd As Object
 
    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
        Set wdApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    
    'Set wd = wdApp.Documents.Open("C:\Documents and Settings\Steve\My Documents\BShift.docx")
    Set wd = wdApp.Documents.Add
    wdApp.Visible = True
thank you adding the x opens the existing doc.
Now I need to paste the contents of a ss to word
 
Upvote 0
Next, I want to copy the entire ss. I tried this code but it brings up a pic box on the word doc. with no data
Code:
Range("A1:IV65536").CopyPicture xlScreen, xlPicture ' whole ss
    wd.Range.Paste
Does anyone know of better code to use?
 
Upvote 0
I got this code to work.
Code:
Dim WdObj As Object, fname As String
    fname = "Word"
    Set WdObj = CreateObject("Word.Application")
    WdObj.Visible = False
    Range("A:A, J:J").Select
    Selection.Copy 'Your Copy Range
    WdObj.Documents.Add
    WdObj.Selection.PasteSpecial Link:=False, _
    DataType:=wdPasteText, Placement:= _
    wdInLine, DisplayAsIcon:=False
    Application.CutCopyMode = False
 
    If fname <> "" Then 'make sure fname is not blank
        With WdObj
            .ChangeFileOpenDirectory "c:\temp" 'save Dir
            .ActiveDocument.SaveAs Filename:=fname & ".doc"
        End With
        Else:
    MsgBox ("File not saved, naming range was botched, guess again.")
    End If
 
    With WdObj
        .ActiveDocument.Close
        .Quit
    End With
    Set WdObj = Nothing
Except it copies the entire ss and I want just 2 columns.
I tried working with this line
Code:
Range("A:A, J:J").Select
but as I said it copies the whole ss.
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,749
Members
449,050
Latest member
excelknuckles

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