Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 181
Default Open file(2) from file(1) and then close file(1)

I need to open a file(2) when I open file(1) and then close file(1)
File(1) name can change so I need variables.

sequence will be
Open File(1)
Open File(2)... I do it by the following macro:

Public LaunchFile As String 'in this public variable I want to keep
file(1) name
Private Sub Workbook_Open()
Dim lDir, lFile, lFileMtr As String
lDir = Sheets("launch").Range("B2") & "\" 'direction of my file to open
lFile = Sheets("launch").Range("B3") & ".xls" 'file name to openm
lFileMtr = lDir & lFile 'complete string of file(2)
LaunchFile = ThisWorkbook.Name 'filename file(1)
Workbooks.Open Filename:=lFileMtr, ReadOnly:=True
End Sub

then on file(2) I have
Private Sub Workbook_Open()
msgbox LaunchFile 'I did it to be sure value stays but it is blank so
next line fail.
workbooks(LaunchFile).close false
End Sub

Any idea where am I failing?



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Open file(2) from file(1) and then close file(1)

IIRC, public variables are valid within a project, not from project to
project.
--
HTH,

Barb Reinhardt



"Alberto Ast" wrote:

I need to open a file(2) when I open file(1) and then close file(1)
File(1) name can change so I need variables.

sequence will be
Open File(1)
Open File(2)... I do it by the following macro:

Public LaunchFile As String 'in this public variable I want to keep
file(1) name
Private Sub Workbook_Open()
Dim lDir, lFile, lFileMtr As String
lDir = Sheets("launch").Range("B2") & "\" 'direction of my file to open
lFile = Sheets("launch").Range("B3") & ".xls" 'file name to openm
lFileMtr = lDir & lFile 'complete string of file(2)
LaunchFile = ThisWorkbook.Name 'filename file(1)
Workbooks.Open Filename:=lFileMtr, ReadOnly:=True
End Sub

then on file(2) I have
Private Sub Workbook_Open()
msgbox LaunchFile 'I did it to be sure value stays but it is blank so
next line fail.
workbooks(LaunchFile).close false
End Sub

Any idea where am I failing?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Open file(2) from file(1) and then close file(1)

Then why the hell did the the good folk at Microsoft ever use the term
'public' ?
--
Ken
"Using Dbase dialects since 82"
"Started with Visicalc in the same year"


"Barb Reinhardt" wrote:

IIRC, public variables are valid within a project, not from project to
project.
--
HTH,

Barb Reinhardt



"Alberto Ast" wrote:

I need to open a file(2) when I open file(1) and then close file(1)
File(1) name can change so I need variables.

sequence will be
Open File(1)
Open File(2)... I do it by the following macro:

Public LaunchFile As String 'in this public variable I want to keep
file(1) name
Private Sub Workbook_Open()
Dim lDir, lFile, lFileMtr As String
lDir = Sheets("launch").Range("B2") & "\" 'direction of my file to open
lFile = Sheets("launch").Range("B3") & ".xls" 'file name to openm
lFileMtr = lDir & lFile 'complete string of file(2)
LaunchFile = ThisWorkbook.Name 'filename file(1)
Workbooks.Open Filename:=lFileMtr, ReadOnly:=True
End Sub

then on file(2) I have
Private Sub Workbook_Open()
msgbox LaunchFile 'I did it to be sure value stays but it is blank so
next line fail.
workbooks(LaunchFile).close false
End Sub

Any idea where am I failing?



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Open file(2) from file(1) and then close file(1)

Barb was partially right/ Here is the statement from VBA help file.

Variables declared using the Public statement are available to all
procedures in all modules in all applications unless Option Private Module
is in effect; in which case, the variables are public only within the
project in which they reside.

Caution The Public statement can't be used in a class module to declare a
fixed-length string variable.


"K_Macd" <kmacdonald "A_T" activ8 ''D O T'' net [ST OP] au wrote in message
...
Then why the hell did the the good folk at Microsoft ever use the term
'public' ?
--
Ken
"Using Dbase dialects since 82"
"Started with Visicalc in the same year"


"Barb Reinhardt" wrote:

IIRC, public variables are valid within a project, not from project to
project.
--
HTH,

Barb Reinhardt



"Alberto Ast" wrote:

I need to open a file(2) when I open file(1) and then close file(1)
File(1) name can change so I need variables.

sequence will be
Open File(1)
Open File(2)... I do it by the following macro:

Public LaunchFile As String 'in this public variable I want to keep
file(1) name
Private Sub Workbook_Open()
Dim lDir, lFile, lFileMtr As String
lDir = Sheets("launch").Range("B2") & "\" 'direction of my file
to open
lFile = Sheets("launch").Range("B3") & ".xls" 'file name to openm
lFileMtr = lDir & lFile 'complete string of file(2)
LaunchFile = ThisWorkbook.Name 'filename file(1)
Workbooks.Open Filename:=lFileMtr, ReadOnly:=True
End Sub

then on file(2) I have
Private Sub Workbook_Open()
msgbox LaunchFile 'I did it to be sure value stays but it is blank
so
next line fail.
workbooks(LaunchFile).close false
End Sub

Any idea where am I failing?





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Open file(2) from file(1) and then close file(1)

I got the same results. The variable was empty when the second workbook
opened. So, if you go to the VBA help file for "Option Private Module", the
very last sentence in the last parragraph says you cannot reference across
projects in Visual Basic. Now that confuses me, the way it is written into
that paragraph. There is no menu selection for the Option Private Module,
it is VBA access only.


"K_Macd" <kmacdonald "A_T" activ8 ''D O T'' net [ST OP] au wrote in message
...
Then why the hell did the the good folk at Microsoft ever use the term
'public' ?
--
Ken
"Using Dbase dialects since 82"
"Started with Visicalc in the same year"


"Barb Reinhardt" wrote:

IIRC, public variables are valid within a project, not from project to
project.
--
HTH,

Barb Reinhardt



"Alberto Ast" wrote:

I need to open a file(2) when I open file(1) and then close file(1)
File(1) name can change so I need variables.

sequence will be
Open File(1)
Open File(2)... I do it by the following macro:

Public LaunchFile As String 'in this public variable I want to keep
file(1) name
Private Sub Workbook_Open()
Dim lDir, lFile, lFileMtr As String
lDir = Sheets("launch").Range("B2") & "\" 'direction of my file
to open
lFile = Sheets("launch").Range("B3") & ".xls" 'file name to openm
lFileMtr = lDir & lFile 'complete string of file(2)
LaunchFile = ThisWorkbook.Name 'filename file(1)
Workbooks.Open Filename:=lFileMtr, ReadOnly:=True
End Sub

then on file(2) I have
Private Sub Workbook_Open()
msgbox LaunchFile 'I did it to be sure value stays but it is blank
so
next line fail.
workbooks(LaunchFile).close false
End Sub

Any idea where am I failing?







  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 181
Default Open file(2) from file(1) and then close file(1)

Thanks you all for your inputs, K_Macd made me laugh...
I was able to get away with what I needed... actually was a series of three
files. I did the following and it worked fine

Public ElectronicFile, LaunchFile As String
Public wElectronicFile, wLaunchFile As Workbook

Private Sub Workbook_Open()
Dim DevDir, DevFile, DevFileMtr As String
Dim lDir, lFile, lFileMtr As String
Application.ScreenUpdating = False

ElectronicFile = ThisWorkbook.Name

lDir = Sheets("Electronic").Range("B2") & "\"
lFile = Sheets("Electronic").Range("B3") & ".xls"
lFileMtr = lDir & lFile

Workbooks.Open Filename:=lFileMtr, ReadOnly:=True
Set wLaunchFile = Workbooks(lFile)

DevDir = wLaunchFile.Sheets("Launch").Range("B2") & "\"
DevFile = wLaunchFile.Sheets("Launch").Range("B3") & ".xls"
DevFileMtr = DevDir & DevFile

Workbooks.Open Filename:=DevFileMtr, ReadOnly:=True

wLaunchFile.Close False
Workbooks(ElectronicFile).Close False
Application.ScreenUpdating = True

End Sub


"JLGWhiz" wrote:

I got the same results. The variable was empty when the second workbook
opened. So, if you go to the VBA help file for "Option Private Module", the
very last sentence in the last parragraph says you cannot reference across
projects in Visual Basic. Now that confuses me, the way it is written into
that paragraph. There is no menu selection for the Option Private Module,
it is VBA access only.


"K_Macd" <kmacdonald "A_T" activ8 ''D O T'' net [ST OP] au wrote in message
...
Then why the hell did the the good folk at Microsoft ever use the term
'public' ?
--
Ken
"Using Dbase dialects since 82"
"Started with Visicalc in the same year"


"Barb Reinhardt" wrote:

IIRC, public variables are valid within a project, not from project to
project.
--
HTH,

Barb Reinhardt



"Alberto Ast" wrote:

I need to open a file(2) when I open file(1) and then close file(1)
File(1) name can change so I need variables.

sequence will be
Open File(1)
Open File(2)... I do it by the following macro:

Public LaunchFile As String 'in this public variable I want to keep
file(1) name
Private Sub Workbook_Open()
Dim lDir, lFile, lFileMtr As String
lDir = Sheets("launch").Range("B2") & "\" 'direction of my file
to open
lFile = Sheets("launch").Range("B3") & ".xls" 'file name to openm
lFileMtr = lDir & lFile 'complete string of file(2)
LaunchFile = ThisWorkbook.Name 'filename file(1)
Workbooks.Open Filename:=lFileMtr, ReadOnly:=True
End Sub

then on file(2) I have
Private Sub Workbook_Open()
msgbox LaunchFile 'I did it to be sure value stays but it is blank
so
next line fail.
workbooks(LaunchFile).close false
End Sub

Any idea where am I failing?





.

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Open file(2) from file(1) and then close file(1)

Hi Alberto, Thanks for posting back with your solution. That helps others
with similar problems.



"Alberto Ast" wrote in message
...
Thanks you all for your inputs, K_Macd made me laugh...
I was able to get away with what I needed... actually was a series of
three
files. I did the following and it worked fine

Public ElectronicFile, LaunchFile As String
Public wElectronicFile, wLaunchFile As Workbook

Private Sub Workbook_Open()
Dim DevDir, DevFile, DevFileMtr As String
Dim lDir, lFile, lFileMtr As String
Application.ScreenUpdating = False

ElectronicFile = ThisWorkbook.Name

lDir = Sheets("Electronic").Range("B2") & "\"
lFile = Sheets("Electronic").Range("B3") & ".xls"
lFileMtr = lDir & lFile

Workbooks.Open Filename:=lFileMtr, ReadOnly:=True
Set wLaunchFile = Workbooks(lFile)

DevDir = wLaunchFile.Sheets("Launch").Range("B2") & "\"
DevFile = wLaunchFile.Sheets("Launch").Range("B3") & ".xls"
DevFileMtr = DevDir & DevFile

Workbooks.Open Filename:=DevFileMtr, ReadOnly:=True

wLaunchFile.Close False
Workbooks(ElectronicFile).Close False
Application.ScreenUpdating = True

End Sub


"JLGWhiz" wrote:

I got the same results. The variable was empty when the second workbook
opened. So, if you go to the VBA help file for "Option Private Module",
the
very last sentence in the last parragraph says you cannot reference
across
projects in Visual Basic. Now that confuses me, the way it is written
into
that paragraph. There is no menu selection for the Option Private
Module,
it is VBA access only.


"K_Macd" <kmacdonald "A_T" activ8 ''D O T'' net [ST OP] au wrote in
message
...
Then why the hell did the the good folk at Microsoft ever use the term
'public' ?
--
Ken
"Using Dbase dialects since 82"
"Started with Visicalc in the same year"


"Barb Reinhardt" wrote:

IIRC, public variables are valid within a project, not from project to
project.
--
HTH,

Barb Reinhardt



"Alberto Ast" wrote:

I need to open a file(2) when I open file(1) and then close file(1)
File(1) name can change so I need variables.

sequence will be
Open File(1)
Open File(2)... I do it by the following macro:

Public LaunchFile As String 'in this public variable I want to
keep
file(1) name
Private Sub Workbook_Open()
Dim lDir, lFile, lFileMtr As String
lDir = Sheets("launch").Range("B2") & "\" 'direction of my
file
to open
lFile = Sheets("launch").Range("B3") & ".xls" 'file name to
openm
lFileMtr = lDir & lFile 'complete string of file(2)
LaunchFile = ThisWorkbook.Name 'filename file(1)
Workbooks.Open Filename:=lFileMtr, ReadOnly:=True
End Sub

then on file(2) I have
Private Sub Workbook_Open()
msgbox LaunchFile 'I did it to be sure value stays but it is
blank
so
next line fail.
workbooks(LaunchFile).close false
End Sub

Any idea where am I failing?





.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Close & save a file at set time only IF the file is open Clivey_UK[_5_] Excel Programming 2 May 1st 06 06:19 PM
Close Current File Then Open New file Steve Roberts Excel Programming 1 March 31st 05 02:08 AM
OPen and close file Alvin Hansen[_2_] Excel Programming 5 January 25th 05 01:01 PM
VBA - on a button event, open another closed file, post changes, close file Fio Excel Programming 0 March 1st 04 01:08 PM
Automate open file, update links, run macro, close and save file Geoff[_7_] Excel Programming 2 August 26th 03 10:13 PM


All times are GMT +1. The time now is 01:20 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"