+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    11-16-2009
    Location
    Doncaster, England
    MS-Off Ver
    Excel 2000
    Posts
    26

    Question Variable document name

    Hi all.
    I am trying to find what code I should use, at the moment I have a template file that lots of people are using so it launches under several names depending how many other blank un saved documents they have open such as
    Document 1, Document 2, etc
    The template has a command button that opens up a different template same again the name can change, Document 1, Document 2, etc the macro the goes back and forth between the documents copying and pasting bits of information.
    My question is can I replace the file name of what document to go to once they are open with the name that the system as assigned it.
    Hope this makes sense
    here is my current code
    Code:
    ChangeFileOpenDirectory "O:\In development - not to be used yet\Master Documents\Master Letter Templates\"
        Documents.Open FileName:="C1.dot", ConfirmConversions:=False, ReadOnly:= _
            False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:= _
            "", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _
            Format:=wdOpenFormatAuto
        Windows("letter loader").Activate
        ActiveDocument.Shapes("Text Box 19").Select
        Selection.WholeStory
        Selection.Copy
        Windows("C1").Activate
        If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
            ActiveWindow.Panes(2).Close
        End If
        If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
            ActivePane.View.Type = wdOutlineView Then
            ActiveWindow.ActivePane.View.Type = wdPrintView
        End If
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
        Selection.MoveDown Unit:=wdLine, Count:=1
        Selection.Paste
        ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
        Windows("letter loader").Activate
        Selection.ShapeRange.Select
        ActiveDocument.Shapes("Text Box 2").Select
        Selection.WholeStory
        Selection.Copy
        Windows("C1").Activate
        Selection.MoveDown Unit:=wdLine, Count:=53
        Selection.MoveUp Unit:=wdLine, Count:=14
        Selection.MoveDown Unit:=wdLine, Count:=1
        Selection.Paste
        ActiveWindow.ActivePane.VerticalPercentScrolled = 21
        Windows("letter loader").Activate
        Selection.ShapeRange.Select
        ActiveDocument.Shapes("Text Box 9").Select
        Selection.WholeStory
        Selection.Copy
        Windows("C1").Activate
        Selection.Paste

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,225

    Re: Variable document name

    As soon as the new document opens, immediately grab and store the new workbook name as a string, then feed that string into your "Activate" codes:
    Code:
    Option Explicit
    
    Sub Test()
    Dim Nw As String
    Application.Screenupdating = False
    
    ChangeFileOpenDirectory "O:\In development - not to be used yet\Master Documents\Master Letter Templates\"
        Documents.Open Filename:="C1.dot", ConfirmConversions:=False, ReadOnly:=False, _
            AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
            WritePasswordDocument:="", WritePasswordTemplate:="", Format:=wdOpenFormatAuto
    
    Nw = ActiveWorkbook.Name
        
        Windows("letter loader").Activate
        ActiveDocument.Shapes("Text Box 19").Select
        Selection.WholeStory
        Selection.Copy
        Windows(Nw).Activate
        If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
            ActiveWindow.Panes(2).Close
        End If
        If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
            ActivePane.View.Type = wdOutlineView Then
            ActiveWindow.ActivePane.View.Type = wdPrintView
        End If
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
        Selection.MoveDown Unit:=wdLine, Count:=1
        Selection.Paste
        ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
        Windows("letter loader").Activate
        Selection.ShapeRange.Select
        ActiveDocument.Shapes("Text Box 2").Select
        Selection.WholeStory
        Selection.Copy
        Windows(Nw).Activate
        Selection.MoveDown Unit:=wdLine, Count:=53
        Selection.MoveUp Unit:=wdLine, Count:=14
        Selection.MoveDown Unit:=wdLine, Count:=1
        Selection.Paste
        ActiveWindow.ActivePane.VerticalPercentScrolled = 21
        Windows("letter loader").Activate
        Selection.ShapeRange.Select
        ActiveDocument.Shapes("Text Box 9").Select
        Selection.WholeStory
        Selection.Copy
        Windows(Nw).Activate
        Selection.Paste
    
    Application.Screenupdating = True
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    11-16-2009
    Location
    Doncaster, England
    MS-Off Ver
    Excel 2000
    Posts
    26

    Re: Variable document name

    when I run the code it stops at
    Code:
    Nw = ActiveWorkbook.Name
    I am sorry if I am doing somthing wrong here but i cannot get it to work
    can you help please?

  4. #4
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,225

    Re: Variable document name

    I have to admit, I just noticed you were trying to open a word document template. I've ever done that, not from inside Excel.

    The technique I demonstrated is for getting the name of the "just opened Excel document" and doesn't satisfy your needs. My apologies.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  5. #5
    Registered User
    Join Date
    11-16-2009
    Location
    Doncaster, England
    MS-Off Ver
    Excel 2000
    Posts
    26

    Re: Variable document name

    sorry if i was not clear,
    i am in a word document opening a word document

  6. #6
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,225

    Re: Variable document name

    Perhaps this then:
    Code:
    Nw = ActiveDocument.Name
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0