Consulting

Page 3 of 3 FirstFirst 1 2 3
Results 41 to 55 of 55

Thread: Turn on Resources on Install

  1. #41
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    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
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  2. #42
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You've hit the same problem as me.

  3. #43
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Well will soon be payday!.......isn't 10% a little low?
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  4. #44
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I was referring to how your post got messed up. Did you sort that, or was it an admin?

  5. #45
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    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!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  6. #46
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That's what I am getting all the time, but often I can't even fix it.

  7. #47
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Do you use smart indent?, i'm just looking for a common factor!

    anyway i'm off to bed for now!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  8. #48
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    No, never do.

  9. #49
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    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

  10. #50
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    [VBA]If Err.Number = 429 Then
    [/VBA]this is the error code if it's not already loaded!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  11. #51
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    How do I find out the error code numbers? So I don't have to ask again?

  12. #52
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    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]

  13. #53
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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]
    Last edited by Bob Phillips; 03-27-2007 at 10:34 AM.

  14. #54
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    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

  15. #55
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •