Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-24-2017, 03:22 AM
vicmar vicmar is offline Insert Filename automatically when open Windows 7 64bit Insert Filename automatically when open Office 2010 64bit
Novice
Insert Filename automatically when open
 
Join Date: Aug 2017
Posts: 24
vicmar is on a distinguished road
Default Insert Filename automatically when open

I have a word file that reference number depend on filename.

Reference number format: RS-A589-STIS-00001-CV-00


Filename format: 00001-STIS-CV-00

The problem is when i used insert-quick parts-field-filename it will show this 00001-STIS-CV-00 what i what is this format RS-A589-STIS-00001-CV-00. Is this possible?

Thank you!
Reply With Quote
  #2  
Old 08-24-2017, 03:44 AM
gmayor's Avatar
gmayor gmayor is offline Insert Filename automatically when open Windows 10 Insert Filename automatically when open Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

How about the following. Display varRef with a docvariable field.

Code:
Dim strRef As String
Dim vRef As Variant
    With ActiveDocument
        strRef = Left(.Name, InStrRev(.Name, ".") - 1)
        vRef = Split(strRef, "-")
        strRef = "RS-A589-" & vRef(1) & "-" & vRef(0) & "-" & vRef(2) & "-" & vRef(3)
        .Variables("varRef").Value = strRef
        .Fields.Update
    End With
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 08-24-2017, 03:54 AM
vicmar vicmar is offline Insert Filename automatically when open Windows 7 64bit Insert Filename automatically when open Office 2010 64bit
Novice
Insert Filename automatically when open
 
Join Date: Aug 2017
Posts: 24
vicmar is on a distinguished road
Default

Thank you for your reply

how can i put this formula? I will make AutoOpen then insert your formula? how can i call it in the word file?

I figured it already and it works like a charm...

Thank you very much
Reply With Quote
  #4  
Old 08-30-2017, 12:39 AM
vicmar vicmar is offline Insert Filename automatically when open Windows 7 64bit Insert Filename automatically when open Office 2010 64bit
Novice
Insert Filename automatically when open
 
Join Date: Aug 2017
Posts: 24
vicmar is on a distinguished road
Default

hi gmayor,

is there a way of inserting certain filename in word?
Example: KR-A359-STR-AA-00001-SHT 01 OF 01-00
What i want is only the STR-AA-00001-SHT 01 OF 01 or SHT 01 OF 01 or 00

Thank you
Reply With Quote
  #5  
Old 08-30-2017, 04:02 AM
vicmar vicmar is offline Insert Filename automatically when open Windows 7 64bit Insert Filename automatically when open Office 2010 64bit
Novice
Insert Filename automatically when open
 
Join Date: Aug 2017
Posts: 24
vicmar is on a distinguished road
Default

Hi gmayor

I figure it already but the problem is when i save as and change the filename its not updating.
This the code I used:
Code:
Sub AutoOpen()
Dim strRef, strDisp, strSht, strRev As String
Dim vRef, vDisp, vSht, vRev As Variant
    With ActiveDocument
        strRef = Left(.Name, InStrRev(.Name, ".") - 1)
        vRef = Split(strRef, "-")
        strRef = "KR-A359-STR-" & vRef(1) & "-" & vRef(2) & "-" & vRef(3) & "-" & vRef(4)
        .Variables("varRef").Value = strRef
        .Fields.Update
    End With
    With ActiveDocument
        strDisp = Left(.Name, InStrRev(.Name, ".") - 1)
        vDisp = Split(strDisp, "-")
        strDisp = "KR-A359-STR-" & vDisp(1)
        .Variables("varDisp").Value = strDisp
        .Fields.Update
    End With
    With ActiveDocument
        strSht = Left(.Name, InStrRev(.Name, ".") - 1)
        vSht = Split(strSht, "-")
        strSht = vSht(3) & "-" & vSht(4)
        .Variables("varSht").Value = strSht
        .Fields.Update
    End With
    With ActiveDocument
        strRev = Left(.Name, InStrRev(.Name, ".") - 1)
        vRev = Split(strRev, "-")
        strRev = "STR-" & vRev(1) & "-" & vRev(2) & "-" & vRev(3)
        .Variables("varRev").Value = strRev
        .Fields.Update
    End With
End Sub
Reply With Quote
  #6  
Old 08-30-2017, 05:21 AM
gmayor's Avatar
gmayor gmayor is offline Insert Filename automatically when open Windows 10 Insert Filename automatically when open Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

I am no longer sure what it is that you are trying to do. Earlier you said the filename was
00001-STIS-CV-00
That being the case your code cannot possibly work e.g.
Take the line
Code:
strRef = "KR-A359-STR-" & vRef(1) & "-" & vRef(2) & "-" & vRef(3) & "-" & vRef(4)
That line cannot work as vRef(4) does not exist. I guess it should be
Code:
strRef = "KR-A359-STR-" & vRef(0) & "-" & vRef(1) & "-" & vRef(2) & "-" & vRef(3)
vRef(0) is 00001
vRef(1) is
STIS
vRef(2) is CV
vRef(3) is 00
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 08-30-2017, 09:30 AM
vicmar vicmar is offline Insert Filename automatically when open Windows 7 64bit Insert Filename automatically when open Office 2010 64bit
Novice
Insert Filename automatically when open
 
Join Date: Aug 2017
Posts: 24
vicmar is on a distinguished road
Default

Sorry about that the reference and code is working except when save as for new document. This different but i think the code is the same. Im trying to use in my other form the Filename for that is
KR-A359-STR-00001-SHT 01 OF 01-00 and i need to extract that filename into four groups.
1. STR-00001-SHT 01 OF 01-00
2. KR-A359-STR-
3. SHT 01 OF 01
4. 00

and the code i used is the one i post but there is a problem whenever i save as for a new document and change the filename it will not reflect automatically in the new document.

Thank you for your reply.
Reply With Quote
  #8  
Old 08-31-2017, 01:39 AM
gmayor's Avatar
gmayor gmayor is offline Insert Filename automatically when open Windows 10 Insert Filename automatically when open Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

I still have no idea what it is that you are trying to do but if you are trying to split
KR-A359-STR-00001-SHT 01 OF 01-00
at the hyphens, there are four elements (0 to 3 not 1 to 4)
If you are trying to display the filename in a filename field then you need to update the field after saving the document then save that change.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #9  
Old 08-31-2017, 02:49 AM
vicmar vicmar is offline Insert Filename automatically when open Windows 7 64bit Insert Filename automatically when open Office 2010 64bit
Novice
Insert Filename automatically when open
 
Join Date: Aug 2017
Posts: 24
vicmar is on a distinguished road
Default

what do you mean by "update the field after saving the document then save that change"

I will make another declaration or is there a additional code?

Thank you
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert Filename automatically when open How to insert Wildcards into Word 2011 Mac such as: \$Filename\ peterspiano Word 4 06-29-2015 05:13 AM
Excel connot open file (filename) xlsx dengeorge6 Excel 0 12-12-2014 01:36 AM
Insert Filename automatically when open A macro that can insert FILENAME, sendkeys CTRL ALT T, paste clipboard, and nextline kyjac85 Word VBA 13 09-20-2012 05:00 PM
Insert Filename automatically when open How to insert current date into default filename ? czomberzdaniela Word 1 12-27-2011 07:18 PM
Macro for automatically adding filename without .doc RPM7 Word VBA 0 04-29-2010 01:43 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:47 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft