Consulting

Results 1 to 4 of 4

Thread: Using word file between userForms and readonly

  1. #1

    Using word file between userForms and readonly

    Hello everyone.

    I have two problems at the moment.

    1. Firstly I am using multiple user forms, 1 for reading from the word file to excel and 1 for writing to the word file (same word file). I have searched but cant find a way to use the same word file properly without opening multiple instances of the word file.

    2. Next I'm having an issue with the file being "read only" by the Administrator. I checked the file properties and the file doesn't have read only ticked and therefore I don't see why the file always comes up with read only.

    Any help greatly appreciated.

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Since you offer no code or example to work from, about all I can do is offer an example for you to look at. Maybe you can find some solutions from Malcolms KB entry:

    http://vbaexpress.com/kb/getarticle.php?kb_id=184
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    This is the open and view file

    [VBA]
    Dim wrdApp As Object
    Public wrdDoc As Object

    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = False
    Set wrdDoc = wrdApp.Documents.Open("C:\****\wordfile.doc")

    Set rnge = wrdDoc.Range(Start:=wrdDoc.Bookmarks(bm).Range.Start, _
    End:=wrdDoc.Bookmarks(bm + 1).Range.End)
    rtext.Text = rnge.Text
    [/VBA]

    Heres the write to file:

    [VBA]
    Dim wrdApp As Object
    Dim wrdDoc As Object
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = False
    Set wrdDoc = wrdApp.Documents.Open("C:\****\wordfile.doc")
    .....
    .....
    .....
    .....
    wrdDoc.Bookmarks.Add Name:=bookmarkname, _
    Range:=wrdDoc.Bookmarks("\endofdoc").Range
    wrdDoc.Bookmarks(bookmarkname).Range.Text = bookmarktext
    wrdDoc.Save
    wrdApp.Exit
    [/VBA]

    They are two different forms. It wont let me save as it says the file is read only when it properties it says its not read only.

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hey there,

    As to your first question (creating multiple instances), we can use GetObject to see if the app exists before creating an instance.

    W/O example files (1 wb and 1 doc) its not really clear as to what the 'big picture' is. Thus - the below covers checking for an instance, but are still inefficient, as in my opinion, there's no reason to keep creating an instance once ya got one going. I would think more in terms of Getting/Creating Word once, and, if it wasn't running prior to your project, kill it before close. If it was running prior, then I'd make sure it was visible at close.

    As to Read-Only, you don't say what type of environment this is in. For example, I have run into glitches at work (on a LAN) wherein the installation was less than stellar. Anyways, have you (a) tried opening the Word doc manually and tried saving changes, and/or (b) Have you taken a copy of the wb and doc home where you're assured that its not a LAN / Tech type issue?

    Hope this helps,

    Mark

    [vba]Option Explicit
    Dim objWORD As Object
    Dim objDOC As Object
    Sub Word_OpenRead()

    '// Change to in-line error handling, to see if GetObject fails. //
    On Error Resume Next
    Set objWORD = GetObject(Class:="Word.Application")

    If Err.Number > 0 Then
    '// If there was an error getting the object, it didn't exist (that is, //
    '// Word wasn't already running), so we need to create it. //
    Set objWORD = CreateObject(Class:="Word.Application")
    Err.Clear
    End If

    '//Reset error handling//
    On Error GoTo 0

    objWORD.Application.Visible = False

    '// substitute your file's path here//
    Set objDOC = objWORD.Documents.Open(Filename:=ThisWorkbook.Path & "\wordfile.doc")

    '// sub remainder of code, as w/o example file, bookmarks, "bm" etc are unclear //
    objWORD.Application.Visible = True

    Set objWORD = Nothing
    Set objDOC = Nothing
    End Sub

    Sub Word_Write()

    On Error Resume Next
    Set objWORD = GetObject(Class:="Word.Application")

    If Err.Number > 0 Then
    Set objWORD = CreateObject(Class:="Word.Application")
    Err.Clear
    End If

    On Error GoTo 0
    objWORD.Application.Visible = False

    '// substitute your file's path here//
    Set objDOC = objWORD.Documents.Open(Filename:=ThisWorkbook.Path & "\wordfile.doc")

    '// sub remainder of code...//
    objWORD.Application.Visible = True
    objDOC.Application.Selection.TypeText "TEST"
    objDOC.Save
    '// Use .Quit instead of ".Exit"
    objWORD.Quit
    Set objWORD = Nothing
    Set objDOC = Nothing
    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
  •