Results 1 to 4 of 4

Thread: [RESOLVED] VB for Applications - MS Word - String.Subtring(3, 7) not working

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2022
    Posts
    2

    Resolved [RESOLVED] VB for Applications - MS Word - String.Subtring(3, 7) not working

    I am attempting to get a word file to automatically save a filename with a part of the path in the name. I am using the String.Substring method and I keep getting an "Invalid qualifier" error. The code is below:

    Code:
     Dim thisDate As String
     thisDate = Format(Now, "dddd, d MMM yyyy")
     
     Dim myPath As String
     Dim jobNum As String
     
     myPath = ActiveDocument.Path
     'myPath = "asdfghjklqwertyuiopzxcvbnm"
     MsgBox myPath
    
     'Save Substring with the Job Number to identify the job
     jobNum = myPath.Substring(3, 7)
    
     'MsgBox myPath.Substring(3, 7)
     MsgBox jobNum
     
     ActiveDocument.SaveAs2 (ActiveDocument.Path & "" & thisDate)
     
     'Application.ActiveDocument.SaveAs2 ("Test")
     'ActiveDocument.SaveAs (Application.ActiveDocument.Path)
     
    End Sub
    Any thoughts. Every example I look at implies it should work. I have even made another string to test with and I get the same error.
    Thanks for your help.
    Last edited by dday9; Apr 22nd, 2022 at 09:23 AM. Reason: Added BB Code

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: VB for Applications - MS Word - String.Subtring(3, 7) not working

    Moderator Actions: Moved to Office Development, which is for questions regarding Microsoft Office Automation. This can include VSTO, VSTA, and VBA questions.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    Re: VB for Applications - MS Word - String.Subtring(3, 7) not working

    I don't see where you are actually using the jobNum as part of the file name.

    Have you checked to see what thisDate actually contains?

    Check to see what ActiveDocument.Path & thisDate would be. Is it a legal file path and name?

    You do not need the '& "" ' since it does not do anything.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2022
    Posts
    2

    Re: VB for Applications - MS Word - String.Subtring(3, 7) not working

    Quote Originally Posted by jdc2000 View Post
    I don't see where you are actually using the jobNum as part of the file name.

    Have you checked to see what thisDate actually contains?

    Check to see what ActiveDocument.Path & thisDate would be. Is it a legal file path and name?

    You do not need the '& "" ' since it does not do anything.
    I was using jobNum before I began to troubleshoot and test the logic. I have comeback to it. The variable thisDate contains the date. The & "" is unnecessary but it previously contained a backslash, not sure why I removed it. I was able to successfully get this working using jobNum = Right(myPath, 15) in conjunction with jobNum = Left(jobNum, 7) to successfully extract the information I needed from the path variable. I don't think String.Substring works in VBA.
    Thank you for your help.

Posting Permissions

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



Click Here to Expand Forum to Full Width