View Full Version : Using word file between userForms and readonly
grandflavour
10-24-2008, 02:35 PM
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.
lucas
10-25-2008, 10:01 AM
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
grandflavour
10-26-2008, 11:20 PM
This is the open and view file
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
Heres the write to file:
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
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.
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
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
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.