PC Review


Reply
Thread Tools Rate Thread

Create a reference to a workbook which is already open

 
 
a.yearsley@gmail.com
Guest
Posts: n/a
 
      19th Apr 2007
I am looking to create a reference to a workbook which is already
open. I have used the code below but this throws the error '32813':
name conflicts with existing module, project or object library'. The
project that I am trying to reference has a unique name.

Sub Create_Reference()
Dim Path1 As String
Path1 = ThisWorkbook.Worksheets("Timesheet").Range("T2").Text &
"Data File NEW.xls"
Application.VBE.ActiveVBProject.References.AddFromFile Path1
End Sub



What I am really trying to do is have the workbook that I am
referencing opened as a Read Only copy.


Any help on this would be appreciated

 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      19th Apr 2007
Why not just open it readonly?

Dim Wkbk as workbook
dim myFilename as string
myfilename = ThisWorkbook.Worksheets("Timesheet").Range("T2").Text _
& "Data File NEW.xls"

set wkbk = workbooks.open(filename:=myfilename,readonly:=true)



(E-Mail Removed) wrote:
>
> I am looking to create a reference to a workbook which is already
> open. I have used the code below but this throws the error '32813':
> name conflicts with existing module, project or object library'. The
> project that I am trying to reference has a unique name.
>
> Sub Create_Reference()
> Dim Path1 As String
> Path1 = ThisWorkbook.Worksheets("Timesheet").Range("T2").Text &
> "Data File NEW.xls"
> Application.VBE.ActiveVBProject.References.AddFromFile Path1
> End Sub
>
> What I am really trying to do is have the workbook that I am
> referencing opened as a Read Only copy.
>
> Any help on this would be appreciated


--

Dave Peterson
 
Reply With Quote
 
a.yearsley@gmail.com
Guest
Posts: n/a
 
      19th Apr 2007
Yea I open the workbook as a Read Only document, then try to set the
reference to it. But because it is already open it throws the error.

If I set the reference to it while it is still closed then it
automatically opens it in read/write format ... but i need it to be in
read only.

 
Reply With Quote
 
NickHK
Guest
Posts: n/a
 
      19th Apr 2007
Doesn't Dave's solution work ? You set the reference when you open it
read-only, not before or after.

set wkbk = workbooks.open(filename:=myfilename,readonly:=true)

NickHK

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Yea I open the workbook as a Read Only document, then try to set the
> reference to it. But because it is already open it throws the error.
>
> If I set the reference to it while it is still closed then it
> automatically opens it in read/write format ... but i need it to be in
> read only.
>



 
Reply With Quote
 
a.yearsley@gmail.com
Guest
Posts: n/a
 
      19th Apr 2007
Unfortunately Dave's solution doesn't do quite what I am wanting ...
perhaps i didnt explain myself properly

Just to clarify what i wish to do is:
a) have wrkbk1 open
b) from wrkbk1 VBA code create a reference to another workbook
'wrkbk2' which must be opened as a Read Only item.
I want to create a refence .. which you could manually
create by
Tools > References > Browse (from the VB editor)

The way I was going about it was first, open a read only version of
wrkbk2 (as Dave's code does) and then try to create the reference.


A further note: my original code seems to work when i debug it ... it
throws an error but then works. But when it comes to run-time it just
throws the error.



On Apr 19, 2:48 pm, "NickHK" <TungChe...@Invalid.com> wrote:
> Doesn't Dave's solution work ? You set the reference when you open it
> read-only, not before or after.
>
> set wkbk = workbooks.open(filename:=myfilename,readonly:=true)
>
> NickHK
>
> <a.years...@gmail.com> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
> > Yea I open the workbook as a Read Only document, then try to set the
> > reference to it. But because it is already open it throws the error.

>
> > If I set the reference to it while it is still closed then it
> > automatically opens it in read/write format ... but i need it to be in
> > read only.- Hide quoted text -

>
> - Show quoted text -



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      19th Apr 2007
This skinnied down version worked ok for me when I ran it:

Option Explicit
Sub testme()
Dim myFileName As String
Dim wkbk As Workbook
myFileName = "C:\my documents\excel\book1.xls"
Set wkbk = Workbooks.Open(Filename:=myFileName, ReadOnly:=True)
ThisWorkbook.VBProject.References.AddFromFile Filename:=myFileName
End Sub

Book1.xls had a project name of Book1 (changed from VBAProject).
Book1.xls was not marked readonly (in windows), but opened readonly.

And I had Tools|Macro|security|Trusted Publishers tab
Trust access to Visual Basic Project checked

If you can't get it working, you may want to share more information--what is the
error you receive when you run the code.

(E-Mail Removed) wrote:
>
> Unfortunately Dave's solution doesn't do quite what I am wanting ...
> perhaps i didnt explain myself properly
>
> Just to clarify what i wish to do is:
> a) have wrkbk1 open
> b) from wrkbk1 VBA code create a reference to another workbook
> 'wrkbk2' which must be opened as a Read Only item.
> I want to create a refence .. which you could manually
> create by
> Tools > References > Browse (from the VB editor)
>
> The way I was going about it was first, open a read only version of
> wrkbk2 (as Dave's code does) and then try to create the reference.
>
> A further note: my original code seems to work when i debug it ... it
> throws an error but then works. But when it comes to run-time it just
> throws the error.
>
> On Apr 19, 2:48 pm, "NickHK" <TungChe...@Invalid.com> wrote:
> > Doesn't Dave's solution work ? You set the reference when you open it
> > read-only, not before or after.
> >
> > set wkbk = workbooks.open(filename:=myfilename,readonly:=true)
> >
> > NickHK
> >
> > <a.years...@gmail.com> wrote in message
> >
> > news:(E-Mail Removed)...
> >
> >
> >
> > > Yea I open the workbook as a Read Only document, then try to set the
> > > reference to it. But because it is already open it throws the error.

> >
> > > If I set the reference to it while it is still closed then it
> > > automatically opens it in read/write format ... but i need it to be in
> > > read only.- Hide quoted text -

> >
> > - Show quoted text -


--

Dave Peterson
 
Reply With Quote
 
a.yearsley@gmail.com
Guest
Posts: n/a
 
      19th Apr 2007
Thanks a lot for your help.

For some reason using
ThisWorkbook.VBProject.References.AddFromFile
Filename:=myFileName
to create the reference doesnt throw any errors in runtime. Wheras my
code which instead used the line
Application.VBE.ActiveVBProject.References.AddFromFile Path1
throws the error
'32813: name conflicts with existing module, project or object
library
even though no other code has changed.


Thanks again



On Apr 20, 1:11 am, Dave Peterson <peter...@verizonXSPAM.net> wrote:
> This skinnied down version worked ok for me when I ran it:
>
> Option Explicit
> Sub testme()
> Dim myFileName As String
> Dim wkbk As Workbook
> myFileName = "C:\my documents\excel\book1.xls"
> Set wkbk = Workbooks.Open(Filename:=myFileName, ReadOnly:=True)
> ThisWorkbook.VBProject.References.AddFromFile Filename:=myFileName
> End Sub
>
> Book1.xls had a project name of Book1 (changed from VBAProject).
> Book1.xls was not marked readonly (in windows), but opened readonly.
>
> And I had Tools|Macro|security|Trusted Publishers tab
> Trust access to Visual Basic Project checked
>
> If you can't get it working, you may want to share more information--what is the
> error you receive when you run the code.
>
>
>
>
>
> a.years...@gmail.com wrote:
>
> > Unfortunately Dave's solution doesn't do quite what I am wanting ...
> > perhaps i didnt explain myself properly

>
> > Just to clarify what i wish to do is:
> > a) have wrkbk1 open
> > b) from wrkbk1 VBA code create a reference to another workbook
> > 'wrkbk2' which must be opened as a Read Only item.
> > I want to create a refence .. which you could manually
> > create by
> > Tools > References > Browse (from the VB editor)

>
> > The way I was going about it was first, open a read only version of
> > wrkbk2 (as Dave's code does) and then try to create the reference.

>
> > A further note: my original code seems to work when i debug it ... it
> > throws an error but then works. But when it comes to run-time it just
> > throws the error.

>
> > On Apr 19, 2:48 pm, "NickHK" <TungChe...@Invalid.com> wrote:
> > > Doesn't Dave's solution work ? You set the reference when you open it
> > > read-only, not before or after.

>
> > > set wkbk = workbooks.open(filename:=myfilename,readonly:=true)

>
> > > NickHK

>
> > > <a.years...@gmail.com> wrote in message

>
> > >news:(E-Mail Removed)...

>
> > > > Yea I open the workbook as a Read Only document, then try to set the
> > > > reference to it. But because it is already open it throws the error.

>
> > > > If I set the reference to it while it is still closed then it
> > > > automatically opens it in read/write format ... but i need it to be in
> > > > read only.- Hide quoted text -

>
> > > - Show quoted text -

>
> --
>
> Dave Peterson- Hide quoted text -
>
> - Show quoted text -



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      19th Apr 2007
I got that error in my simple testing, too. I figured that it was bad test
workbooks (multiple workbooks having the same project name). I didn't change
the code to overcome the error. I just figured I'd use the project that I
wanted.

Does this mean that you have it working?

(E-Mail Removed) wrote:
>
> Thanks a lot for your help.
>
> For some reason using
> ThisWorkbook.VBProject.References.AddFromFile
> Filename:=myFileName
> to create the reference doesnt throw any errors in runtime. Wheras my
> code which instead used the line
> Application.VBE.ActiveVBProject.References.AddFromFile Path1
> throws the error
> '32813: name conflicts with existing module, project or object
> library
> even though no other code has changed.
>
> Thanks again
>
> On Apr 20, 1:11 am, Dave Peterson <peter...@verizonXSPAM.net> wrote:
> > This skinnied down version worked ok for me when I ran it:
> >
> > Option Explicit
> > Sub testme()
> > Dim myFileName As String
> > Dim wkbk As Workbook
> > myFileName = "C:\my documents\excel\book1.xls"
> > Set wkbk = Workbooks.Open(Filename:=myFileName, ReadOnly:=True)
> > ThisWorkbook.VBProject.References.AddFromFile Filename:=myFileName
> > End Sub
> >
> > Book1.xls had a project name of Book1 (changed from VBAProject).
> > Book1.xls was not marked readonly (in windows), but opened readonly.
> >
> > And I had Tools|Macro|security|Trusted Publishers tab
> > Trust access to Visual Basic Project checked
> >
> > If you can't get it working, you may want to share more information--what is the
> > error you receive when you run the code.
> >
> >
> >
> >
> >
> > a.years...@gmail.com wrote:
> >
> > > Unfortunately Dave's solution doesn't do quite what I am wanting ...
> > > perhaps i didnt explain myself properly

> >
> > > Just to clarify what i wish to do is:
> > > a) have wrkbk1 open
> > > b) from wrkbk1 VBA code create a reference to another workbook
> > > 'wrkbk2' which must be opened as a Read Only item.
> > > I want to create a refence .. which you could manually
> > > create by
> > > Tools > References > Browse (from the VB editor)

> >
> > > The way I was going about it was first, open a read only version of
> > > wrkbk2 (as Dave's code does) and then try to create the reference.

> >
> > > A further note: my original code seems to work when i debug it ... it
> > > throws an error but then works. But when it comes to run-time it just
> > > throws the error.

> >
> > > On Apr 19, 2:48 pm, "NickHK" <TungChe...@Invalid.com> wrote:
> > > > Doesn't Dave's solution work ? You set the reference when you open it
> > > > read-only, not before or after.

> >
> > > > set wkbk = workbooks.open(filename:=myfilename,readonly:=true)

> >
> > > > NickHK

> >
> > > > <a.years...@gmail.com> wrote in message

> >
> > > >news:(E-Mail Removed)...

> >
> > > > > Yea I open the workbook as a Read Only document, then try to set the
> > > > > reference to it. But because it is already open it throws the error.

> >
> > > > > If I set the reference to it while it is still closed then it
> > > > > automatically opens it in read/write format ... but i need it to be in
> > > > > read only.- Hide quoted text -

> >
> > > > - Show quoted text -

> >
> > --
> >
> > Dave Peterson- Hide quoted text -
> >
> > - Show quoted text -


--

Dave Peterson
 
Reply With Quote
 
a.yearsley@gmail.com
Guest
Posts: n/a
 
      19th Apr 2007
yea i do.

thanks for your help

On Apr 20, 9:59 am, Dave Peterson <peter...@verizonXSPAM.net> wrote:
> I got that error in my simple testing, too. I figured that it was bad test
> workbooks (multiple workbooks having the same project name). I didn't change
> the code to overcome the error. I just figured I'd use the project that I
> wanted.
>
> Does this mean that you have it working?
>
>
>
>
>
> a.years...@gmail.com wrote:
>
> > Thanks a lot for your help.

>
> > For some reason using
> > ThisWorkbook.VBProject.References.AddFromFile
> > Filename:=myFileName
> > to create the reference doesnt throw any errors in runtime. Wheras my
> > code which instead used the line
> > Application.VBE.ActiveVBProject.References.AddFromFile Path1
> > throws the error
> > '32813: name conflicts with existing module, project or object
> > library
> > even though no other code has changed.

>
> > Thanks again

>
> > On Apr 20, 1:11 am, Dave Peterson <peter...@verizonXSPAM.net> wrote:
> > > This skinnied down version worked ok for me when I ran it:

>
> > > Option Explicit
> > > Sub testme()
> > > Dim myFileName As String
> > > Dim wkbk As Workbook
> > > myFileName = "C:\my documents\excel\book1.xls"
> > > Set wkbk = Workbooks.Open(Filename:=myFileName, ReadOnly:=True)
> > > ThisWorkbook.VBProject.References.AddFromFile Filename:=myFileName
> > > End Sub

>
> > > Book1.xls had a project name of Book1 (changed from VBAProject).
> > > Book1.xls was not marked readonly (in windows), but opened readonly.

>
> > > And I had Tools|Macro|security|Trusted Publishers tab
> > > Trust access to Visual Basic Project checked

>
> > > If you can't get it working, you may want to share more information--what is the
> > > error you receive when you run the code.

>
> > > a.years...@gmail.com wrote:

>
> > > > Unfortunately Dave's solution doesn't do quite what I am wanting ...
> > > > perhaps i didnt explain myself properly

>
> > > > Just to clarify what i wish to do is:
> > > > a) have wrkbk1 open
> > > > b) from wrkbk1 VBA code create a reference to another workbook
> > > > 'wrkbk2' which must be opened as a Read Only item.
> > > > I want to create a refence .. which you could manually
> > > > create by
> > > > Tools > References > Browse (from the VB editor)

>
> > > > The way I was going about it was first, open a read only version of
> > > > wrkbk2 (as Dave's code does) and then try to create the reference.

>
> > > > A further note: my original code seems to work when i debug it ... it
> > > > throws an error but then works. But when it comes to run-time it just
> > > > throws the error.

>
> > > > On Apr 19, 2:48 pm, "NickHK" <TungChe...@Invalid.com> wrote:
> > > > > Doesn't Dave's solution work ? You set the reference when you open it
> > > > > read-only, not before or after.

>
> > > > > set wkbk = workbooks.open(filename:=myfilename,readonly:=true)

>
> > > > > NickHK

>
> > > > > <a.years...@gmail.com> wrote in message

>
> > > > >news:(E-Mail Removed)...

>
> > > > > > Yea I open the workbook as a Read Only document, then try to set the
> > > > > > reference to it. But because it is already open it throws the error.

>
> > > > > > If I set the reference to it while it is still closed then it
> > > > > > automatically opens it in read/write format ... but i need it to be in
> > > > > > read only.- Hide quoted text -

>
> > > > > - Show quoted text -

>
> > > --

>
> > > Dave Peterson- Hide quoted text -

>
> > > - Show quoted text -

>
> --
>
> Dave Peterson- Hide quoted text -
>
> - Show quoted text -



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      19th Apr 2007
Good!

(E-Mail Removed) wrote:
>
> yea i do.
>
> thanks for your help
>
> On Apr 20, 9:59 am, Dave Peterson <peter...@verizonXSPAM.net> wrote:
> > I got that error in my simple testing, too. I figured that it was bad test
> > workbooks (multiple workbooks having the same project name). I didn't change
> > the code to overcome the error. I just figured I'd use the project that I
> > wanted.
> >
> > Does this mean that you have it working?
> >
> >
> >
> >
> >
> > a.years...@gmail.com wrote:
> >
> > > Thanks a lot for your help.

> >
> > > For some reason using
> > > ThisWorkbook.VBProject.References.AddFromFile
> > > Filename:=myFileName
> > > to create the reference doesnt throw any errors in runtime. Wheras my
> > > code which instead used the line
> > > Application.VBE.ActiveVBProject.References.AddFromFile Path1
> > > throws the error
> > > '32813: name conflicts with existing module, project or object
> > > library
> > > even though no other code has changed.

> >
> > > Thanks again

> >
> > > On Apr 20, 1:11 am, Dave Peterson <peter...@verizonXSPAM.net> wrote:
> > > > This skinnied down version worked ok for me when I ran it:

> >
> > > > Option Explicit
> > > > Sub testme()
> > > > Dim myFileName As String
> > > > Dim wkbk As Workbook
> > > > myFileName = "C:\my documents\excel\book1.xls"
> > > > Set wkbk = Workbooks.Open(Filename:=myFileName, ReadOnly:=True)
> > > > ThisWorkbook.VBProject.References.AddFromFile Filename:=myFileName
> > > > End Sub

> >
> > > > Book1.xls had a project name of Book1 (changed from VBAProject).
> > > > Book1.xls was not marked readonly (in windows), but opened readonly.

> >
> > > > And I had Tools|Macro|security|Trusted Publishers tab
> > > > Trust access to Visual Basic Project checked

> >
> > > > If you can't get it working, you may want to share more information--what is the
> > > > error you receive when you run the code.

> >
> > > > a.years...@gmail.com wrote:

> >
> > > > > Unfortunately Dave's solution doesn't do quite what I am wanting ...
> > > > > perhaps i didnt explain myself properly

> >
> > > > > Just to clarify what i wish to do is:
> > > > > a) have wrkbk1 open
> > > > > b) from wrkbk1 VBA code create a reference to another workbook
> > > > > 'wrkbk2' which must be opened as a Read Only item.
> > > > > I want to create a refence .. which you could manually
> > > > > create by
> > > > > Tools > References > Browse (from the VB editor)

> >
> > > > > The way I was going about it was first, open a read only version of
> > > > > wrkbk2 (as Dave's code does) and then try to create the reference.

> >
> > > > > A further note: my original code seems to work when i debug it ... it
> > > > > throws an error but then works. But when it comes to run-time it just
> > > > > throws the error.

> >
> > > > > On Apr 19, 2:48 pm, "NickHK" <TungChe...@Invalid.com> wrote:
> > > > > > Doesn't Dave's solution work ? You set the reference when you open it
> > > > > > read-only, not before or after.

> >
> > > > > > set wkbk = workbooks.open(filename:=myfilename,readonly:=true)

> >
> > > > > > NickHK

> >
> > > > > > <a.years...@gmail.com> wrote in message

> >
> > > > > >news:(E-Mail Removed)...

> >
> > > > > > > Yea I open the workbook as a Read Only document, then try to set the
> > > > > > > reference to it. But because it is already open it throws the error.

> >
> > > > > > > If I set the reference to it while it is still closed then it
> > > > > > > automatically opens it in read/write format ... but i need it to be in
> > > > > > > read only.- Hide quoted text -

> >
> > > > > > - Show quoted text -

> >
> > > > --

> >
> > > > Dave Peterson- Hide quoted text -

> >
> > > > - Show quoted text -

> >
> > --
> >
> > Dave Peterson- Hide quoted text -
> >
> > - Show quoted text -


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create new workbook and move the Macro without reference to old wb sbweid Microsoft Excel Programming 1 17th Mar 2008 05:15 PM
How do I set up a workbook to open on a cell reference every time =?Utf-8?B?Q0FITQ==?= Microsoft Excel Misc 2 30th Nov 2007 05:41 PM
Circular reference in an open workbook =?Utf-8?B?Sm9lIENsZXRjaGVy?= Microsoft Excel Programming 2 3rd Mar 2006 12:49 PM
Open an existing workbook and reference it =?Utf-8?B?VGltVA==?= Microsoft Excel Programming 1 12th Oct 2005 05:11 PM
How to create a reference using VBA to a source list in different Workbook? keithb Microsoft Excel Discussion 0 4th Aug 2005 03:43 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:35 AM.