-
Here's a couple of macro's i picked up that may help you understand what you are doing! (thats 2.5% for me too....need the cash Bob!)
The two example macros below demonstrates how you can send information to Word
(e.g. creating a new document) and how you can retrieve information from Word
(e.g. reading information from a document).
Note! Read and edit the example code before you try to execute it in your own project!
[vba]The two example macros below demonstrates how you can send information to Word
(e.g. creating a new document) and how you can retrieve information from Word
(e.g. reading information from a document).
Note! Read and edit the example code before you try to execute it in your own project!
Sub CreateNewWordDoc()
' to test this code, paste it into an Excel module
' add a reference to the Word-library
' create a new folder named C:\Foldername or edit the filnames in the code
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim i As Integer
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add ' create a new document
' or
'Set wrdDoc = wrdApp.Documents.Open("C:\Foldername\Filename.doc")
' open an existing document
' example word operations
With wrdDoc
For i = 1 To 100
.Content.InsertAfter "Here is a example test line #" & i
.Content.InsertParagraphAfter
Next i
If Dir("C:\Foldername\MyNewWordDoc.doc") <> "" Then
Kill "C:\Foldername\MyNewWordDoc.doc"
End If
.SaveAs ("C:\Foldername\MyNewWordDoc.doc")
.Close ' close the document
End With
wrdApp.Quit ' close the Word application
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub
Sub OpenAndReadWordDoc()
' assumes that the previous procedure has been executed
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim tString As String, tRange As Word.Range
Dim p As Long, r As Long
Workbooks.Add ' create a new workbook
With Range("A1")
.Formula = "Word Document Contents:"
.Font.Bold = True
.Font.Size = 14
.Offset(1, 0).Select
End With
r = 3 ' startrow for the copied text from the Word document
Set wrdApp = CreateObject("Word.Application")
'wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("C:\Foldername\MyNewWordDoc.doc")
' example word operations
With wrdDoc
For p = 1 To .Paragraphs.Count
Set tRange = .Range(Start:=.Paragraphs(p).Range.Start, _
End:=.Paragraphs(p).Range.End)
tString = tRange.Text
tString = Left(tString, Len(tString) - 1)
' exclude the paragraph-mark
' check if the text has the content you want
If InStr(1, tString, "1") > 0 Then
' fill into active worksheet
ActiveSheet.Range("A" & r).Formula = tString
r = r + 1
End If
Next p
.Close ' close the document
End With
wrdApp.Quit ' close the Word application
Set wrdDoc = Nothing
Set wrdApp = Nothing
ActiveWorkbook.Saved = True
End Sub
[/vba]Regards,Simon
-
You've hit the same problem as me.
-
Well will soon be payday!.......isn't 10% a little low?
-
I was referring to how your post got messed up. Did you sort that, or was it an admin?
-
I posted the code but it posted it as a single line!, so i went to edit back to my workbook copy n paste again...then it was ok?
Wierd as i did no action differently!
-
That's what I am getting all the time, but often I can't even fix it.
-
Do you use smart indent?, i'm just looking for a common factor!
anyway i'm off to bed for now!
-
-
This works partially:
[VBA]
Set wd = CreateObject("Word.Application")
wd.Visible = True
On Error Resume Next
Set wdDoc = wrdApp.Documents.Open("H:\@Business_Reporting_Today\References\Business Reporting Today.doc")
If Err Then
wd.Documents.Open ("H:\@Business_Reporting_Today\References\Business Reporting Today.doc")
Else
wdDoc.Activate
End If[/VBA]
but what error code shoud I set it to on the line
[VBA]If Err Then
[/VBA]
In Excel I have it set to 9
-
[VBA]If Err.Number = 429 Then
[/VBA]this is the error code if it's not already loaded!
-
How do I find out the error code numbers? So I don't have to ask again?
-
THank you for your help but that isn't working:
[VBA]Sub Instructions()
Set wd = CreateObject("Word.Application")
wd.Visible = True
On Error Resume Next
Set wdDoc = wrdApp.Documents.Open("H:\@Business_Reporting_Today\References\Business Reporting Today.doc")
If Not Err.Number = 429 Then
wd.Documents.Open ("H:\@Business_Reporting_Today\References\Business Reporting Today.doc")
Else
wdDoc.Activate
End If
End Sub[/VBA]
I also tried:
[VBA]Sub Instructions()
Set wd = CreateObject("Word.Application")
wd.Visible = True
On Error Resume Next
Set wdDoc = wrdApp.Documents.Open("H:\@Business_Reporting_Today\References\Business Reporting Today.doc")
If Err.Number = 429 Then
wd.Documents.Open ("H:\@Business_Reporting_Today\References\Business Reporting Today.doc")
Else
wdDoc.Activate
End If
End Sub[/VBA]
-
I have propagated your error but you will get problems if you create a Word instance and set a variable called wd but refer to wrdApp later on.
ALWAYS USE OPTION EXPLICIT!
[vba]
Set wd = CreateObject("Word.Application")
wd.Visible = True
On Error Resume Next
Set wdDoc = wrdApp.Documents.Open("H:\@Business_Reporting_Today\References\Business Reporting Today.doc")
On Error GoTo 0
If Not wdDoc Is Nothing Then
wdDoc.Activate
Else
MsgBox "File not found"
End If
[/vba]
-
This is what I have now:
[VBA]Sub Instructions()
Dim wdDoc As Object
Dim wd As Object
Set wd = CreateObject("Word.Application")
wd.Visible = True
On Error Resume Next
Set wdDoc = wd.Documents("H:\@Business_Reporting_Today\References\Business Reporting Today.doc")
On Error GoTo 0
If Not wdDoc Is Nothing Then
wdDoc.Activate
Else
wd.Documents.Open ("H:\@Business_Reporting_Today\References\Business Reporting Today.doc")
End If
End Sub[/VBA]
unfortunately, if it is open already it is telling me it is locked for editing
-
Try this
[vba]
Function IsFileOpen(FileName As String)
Dim iFilenum As Long
Dim iErr As Long
On Error Resume Next
iFilenum = FreeFile()
Open FileName For Input Lock Read As #iFilenum
Close iFilenum
iErr = Err
On Error GoTo 0
Select Case iErr
Case 0: IsFileOpen = False
Case 70: IsFileOpen = True
Case Else: Error iErr
End Select
End Function
Sub Instructions()
Dim wdDoc As Object
Dim wd As Object
Set wd = CreateObject("Word.Application")
wd.Visible = True
If Not IsFileOpen("H:\@Business_Reporting_Today\References\Business Reporting Today.doc") Then
wd.Documents.Open ("H:\@Business_Reporting_Today\References\Business Reporting Today.doc")
wdDoc.Activate
Else
MsgBox "File is already open"
End If
End Sub
[/vba]