Reputation: 607
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 error:
"Object doesn't support this property"
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
Upvotes: 6
Views: 91532
Reputation: 351
I know this is a very old thread, but it still comes up a the top of Google. The simplest syntax is:
Dim DestPath As String
DestPath = "C:\Testing\MyFile.docx"
CreateObject("Shell.Application").Open (DestPath)
Upvotes: 0
Reputation: 161
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"
'Word session creation
Set WordApp = CreateObject("Word.Application")
'word will be closed while running
WordApp.Visible = False
'open the .doc file
Set WordDoc = WordApp.Documents.Open(File)
And to close the application:
WordDoc.Close
WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing
I hope it can help you.
Upvotes: 16
Reputation: 41
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.
Upvotes: 4
Reputation: 21
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
Upvotes: 2