Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I can't open Word from Excel macro (office xp). If I use this code, it will stop on line Set wdDoc = wordapp.Documents.Open(polozka.ShortPath) and program freezes. If I use Set wdDoc = GetObject(polozka.ShortPath) instead of this line, program stops here With wdDoc.Selection with "Object doesn't support this property..." error

Dim wordapp As Word.Application
Dim wdDoc As Word.Document

Set fso = CreateObject("Scripting.FileSystemObject")
Set files = fso.GetFolder("C:\path").Files       
Set wordapp = CreateObject("Word.Application")
For Each polozka In files
    Set wdDoc = wordapp.Documents.Open(polozka.ShortPath)
    wordapp.Visible = True
    With wdDoc.Selection
        .HomeKey Unit:=6
        .Find.Text = "Název (typ):"
        .Find.Wrap = wdFindContinue
        ...
    End With
    ...
    wordapp.Quit
    Set wordapp = Nothing    
Next
share|improve this question
add comment

3 Answers

you have to declare your variable as object like below

Dim Paragraphe As Object, WordApp As Object, WordDoc As Object

and to use the doc:

File= "D:\path"
    'creationsession Word
    Set WordApp = CreateObject("Word.Application")
    'word ll be close to run
    WordApp.Visible = False
    'open the file .doc
    Set WordDoc = WordApp.Documents.Open(File)

and to close the applcation

WordDoc.Close
    WordApp.Quit
    Set WordDoc = Nothing
    Set WordApp = Nothing

i hope they can help you

share|improve this answer
 
thanks, now I can open document in first way, but still program stops here With wdDoc.Selection with "Object doesn't support this property or method" error –  koubin May 9 '13 at 7:38
 
I found solution. Change this row With wdDoc.Selection to this With wordapp.Selection solved it. But still I don't know, why I can't work with wdDoc object –  koubin May 9 '13 at 15:27
add comment
Sub substitute()  
'  
' substitute Macro 
' 
' Note: In Excel VBA, in tools -> references: Enable Microsoft Word 12.0 0bject  
'  
Dim FindStr As String  
Dim ReplaceStr As String  
Dim path_src As String  
Dim path_dest As String  

' Define word object  
Dim WA As Object  

Dim cs As Worksheet  
Dim Idx As Integer  

' Data worksheet "Data" col A find text, Col B replace text  
Set cs = ActiveWorkbook.Worksheets("Data")  

Set WA = CreateObject("Word.Application")  

WA.Visible = True  

path_src = "C:\Temp\data.docx"  
path_dest = "C:\Temp\data_out.docx"  

WA.documents.Open (path_src)  

' Set word object active  
WA.Activate  

' Optional, use Idx to limit loop iterations  
Idx = 1  
Do While ((Len(cs.Cells(Idx, 1).Value) > 1) And (Idx < 100))  
'  
  FindStr = cs.Cells(Idx, 1).Value  
  ReplaceStr = cs.Cells(Idx, 2).Value  
  With WA  
    .Selection.HomeKey Unit:=wdStory  
    .Selection.Find.ClearFormatting  
    .Selection.Find.Replacement.ClearFormatting  
    With .Selection.Find  
        .Text = FindStr  
        .Replacement.Text = ReplaceStr  
        .Forward = True  
        .Wrap = wdFindAsk  
        .Format = False  
        .MatchCase = False  
        .MatchWholeWord = False  
        .MatchWildcards = False  
        .MatchSoundsLike = False  
        .MatchAllWordForms = False  
    End With  

    .Selection.Find.Execute Replace:=wdReplaceAll  
   End With  
   Idx = Idx + 1  
Loop  
WA.Application.ActiveDocument.SaveAs path_dest  
WA.documents.Close  

Set WA = Nothing  

End Sub  
share|improve this answer
 
Please, format your code in a correct way. Read the SO markdown online help. –  Lorenzo Donati Sep 21 '13 at 10:53
add comment

I had a similar problem with excel not recognizing the word.application command and other word Objects. If you want those objects to be recognized by excel you will need to select Tools>References... in the Visual Basic editor. When you select References a window will populate, go down through the list until you find Microsoft Word x.0 Object Library. Select the check box, this will allow excel to now recognize word commands. You can also change the priority level of that Object Library to make it easier to find next time.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.