Re: VB local drive addressing

From: Karl E. Peterson (karl_at_mvps.org)
Date: 01/24/05


Date: Mon, 24 Jan 2005 11:22:19 -0800

Hi Kevin --

Not sure what the issue is there, because I was absolutely unable to recreate it
here. I used this code, which differs from your own only in the name of the output
file and how the autonumbering is presented to the user:

   Option Explicit

   Private Const IniFile = "\\ntcl01\public\test.txt"

   Private Property Get NextNumber() As String
       Dim temp As String
       On Error GoTo BadInput
       Open IniFile For Input As #1
       Line Input #1, temp
       Close #1
       NextNumber = temp
       Exit Property
   BadInput:
       NextNumber = "" ' default
   End Property

   Private Property Let NextNumber(myNumber As String)
       On Error GoTo BadOutput
       Open IniFile For Output As #1
       Print #1, myNumber
       Close #1
       Exit Property
   BadOutput:
       MsgBox Prompt:="Unable to store invoice number in" _
           & vbCr & IniFile & vbCr & "Check that it's available.", _
           Title:="File Error"
   End Property

   Public Sub AutoNew()
       Dim Order As Variant
       Order = NextNumber

       If Order = "" Then
           Order = 1
       Else
           Order = Order + 1
       End If

       NextNumber = Order
       MsgBox "This is order #" & Order
   End Sub

The template was stored locally, and the text file is off on an unrestricted (dumping
ground) network share. This leaves just two possibilities. Either another instance
of your template is reading the file at the exact moment you're trying to write it,
or you don't have write permissions where the file is? Do you perhaps have Create,
but not Modify, permissions in this folder?

If you're looking for tips, I would suggest you not use explicit file handles. Do
something like this, instead:

  Dim hFile As Long
  hFile = Freefile()
  Open "whatever.txt" For Input Access Read As #hFile
  Line Input #hFile, temp
  ' and so on...

Sorry... Karl

-- 
[Microsoft Basic: 1976-2001, RIP]
Kevin J Prince wrote:
> Greetings.
> Not sure what NG this question should be asked in. but perhaps if
> wrong someone can point me into the correct area please.. I have a
> problem which involves a small piece of VBA code being used in a Word
> template. This code uses a data file to hold a number, which is in
> turn opened, closed, incremented, opened, updated, then closed. The
> routine uses a reference to the computer \\main\C\numbers\number.txt.
> This file when opened, opens correctly and the information is duly
> read and incremented. But when I go to open and write the data back
> to the same \\main\C\numbers\number.txt file I get an error 75. Path
> /File access error.
> That is if I rem out the on error go to bad output.
>
> Can anyone throw any light on my addressing method please. Code is
> below, and is used in a Word document to give a one up number each
> time a template is used. The template is held on a single computer
> and is used by other computers who have to take the next computer in
> sequence.
>
> I have tried putting both the number.txt and the .Dot template into a
> local folder, changing the sharing on that folder to allow for read
> and write. And also I have put both files into the Documents & setting
> Shared area.
>
> It will not work on the computer that the files are on, never mind one
> of the computers on the network.
>
> AutoNew() is the entry point to the code.
>
>
> Private Const IniFile = "\\main\C\numbers\number.txt"
>
> Private Property Get NextNumber() As String
>     Dim temp As String
>     On Error GoTo BadInput
>     Open IniFile For Input As #1
>     Line Input #1, temp
>     Close #1
>     NextNumber = temp
>     Exit Property
> BadInput:
>     NextNumber = "" ' default
> End Property
>
> Private Property Let NextNumber(myNumber As String)
>     On Error GoTo BadOutput
>     Open IniFile For Output As #1
>     Print #1, myNumber
>     Close #1
>     Exit Property
> BadOutput:
>     MsgBox Prompt:="Unable to store invoice number in" _
>         & vbCr & IniFile & vbCr & "Check that it's available.", _
>         Title:="File Error"
> End Property
>
> Public Sub AutoNew()
>     Dim Order As Variant
>
>     Order = NextNumber
>
>     If Order = "" Then
>         Order = 1
>     Else
>         Order = Order + 1
>     End If
>
>     NextNumber = Order
>
>     ActiveDocument.Bookmarks("Order").Range.InsertBefore _
>         Format(Order, "00#")
>     ActiveDocument.SaveAs FileName:="path" & Format(Order, "00#") End
> Sub
>
> Please any indication as to my addressing method and how it needs to
> be correct greatly appreciated.
>
> Regards
> Kevin


Relevant Pages

  • RE: Create Word Docs From Excel Cell Data
    ... Dim WordObject As Object 'Word Object ... Dim SigCardPathString As String 'Path & Template ... Anyway the following Excel VBA code opens a word document with a specified ...
    (microsoft.public.excel.programming)
  • RE: Create Word Docs From Excel Cell Data
    ... It still needs the code to establish which Word Template to use based on the ... Anyway the following Excel VBA code opens a word document with a specified ... Dim strPathTemplateName As String 'Path & Template ... Set objWord = CreateObject ...
    (microsoft.public.excel.programming)
  • Re: Compile Error Named Argument Not Found Word 2007
    ... With filename Open if you have Windows in Taskbar the template on opening a new document and running its autotexec macro is not the activedocument screen - is there any code to force the new document to be the active document screen without too much effort? ... One creates a new document based on a template, but one opens an existing ... Sub NewDoc(pName As String) ... Function Path() As String ...
    (microsoft.public.word.vba.general)
  • Templaste SaveAs retains existing name
    ... I have a template called PkLstTemplate1.xlt saved in the template ... A user opens this template by clicking on a sortcut to it. ... Dim strContainerNumber As String ... Dim strContainerShipDate As String ...
    (microsoft.public.excel.programming)
  • Saveas on Template retains existing name!
    ... I have a template called PkLstTemplate1.xlt saved in the template ... A user opens this template by clicking on a sortcut to it. ... Dim strContainerNumber As String ... Dim strContainerShipDate As String ...
    (microsoft.public.excel.programming)