Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-10-2016, 02:36 AM
gmayor's Avatar
gmayor gmayor is offline Macro to check the existence of a word docx file and create a new word file with specific content. Windows 10 Macro to check the existence of a word docx file and create a new word file with specific content. Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
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 ofgmayor has much to be proud of
Default

Perhaps the following, though together with your other macro it should give you enough information to modify it to your needs.



Code:
Option Explicit

Sub Macro1()
Const strpath As String = "C:\Path\"
Const BAAR As String = "BAAR"
Const MARCA As String = "marca"
Dim strName As String
Dim strMessage As String
Dim fso As Object
Dim oDoc As Document

    strMessage = "Raport de verif pt. dosar " & BAAR & vbCr & _
                 "In atentia d-lui " & strName & "," & vbCr & _
                 "Urmare a verificarii dosarului " & BAAR & " auto pagubit marca " & MARCA & _
                 "va transmitem atasat raportul de verificare." & vbCr & _
                 "Rog confirmati primirea." & vbCr & _
                 "Cu stima," & vbCr & vbCr & _
                 "Mihai STAICU"

    Set fso = CreateObject("Scripting.FileSystemObject")
    Select Case True
        Case fso.FileExists(strpath & "Mail AU.docx")
            Set oDoc = Documents.Add(Template:=strpath & "Mail AU.docx")
            strName = "Adrian Uta"
            oDoc.Range.Text = strMessage
            oDoc.SaveAs2 FileName:=strpath & "AU.docx", addtorecentfiles:=False

        Case fso.FileExists(strpath & "Mail EN.docx")
            Set oDoc = Documents.Add(Template:=strpath & "Mail EN.docx")
            strName = "Emilia Negoita"
            oDoc.Range.Text = strMessage
            oDoc.SaveAs2 FileName:=strpath & "EN.docx", addtorecentfiles:=False
    End Select
lbl_Exit:
    Set fso = Nothing
    Set oDoc = Nothing
    Exit Sub
End Sub
__________________
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
  #2  
Old 11-10-2016, 04:04 AM
staicumihai staicumihai is offline Macro to check the existence of a word docx file and create a new word file with specific content. Windows 7 32bit Macro to check the existence of a word docx file and create a new word file with specific content. Office 2010 32bit
Advanced Beginner
Macro to check the existence of a word docx file and create a new word file with specific content.
 
Join Date: Dec 2015
Posts: 33
staicumihai is on a distinguished road
Default

Thanks for your reply!
This is the macro I have now based on your macros:


Code:
Option Explicit
Sub Rename_Folder()
Dim oFSO As Object
Dim oFolder As Object
Dim oSubFolder As Object
Dim i As Integer
Dim strpath As String
Dim strSubFolderPath As String
Dim strSubFolderNewName As String
Dim fisword As String
Dim FOLDER As String
Dim primit As String
Dim cinci As String
Dim cinci1 As String
Dim raport As String
Dim BAAR As String
Dim nr As String
Dim MARCA As String
Dim data As String
Dim anexa1 As String
Dim anexa5 As String
Dim AUDATEX As String
Dim nr1 As String
Dim marca1 As String
Dim anexa4 As String
Dim strName As String
Dim fso As Object
Dim oDoc As Document
Dim strName1 As String
Dim strmessage As String

Const strDrive As String = "D:\MIHAI\DOSARE\BAAR\"

raport = Trim(ActiveDocument.BuiltInDocumentProperties("Title").Value)
BAAR = Replace(Trim(ActiveDocument.BuiltInDocumentProperties("keywords").Value), "/", ".") & Chr(32)
nr = Replace(Trim(ActiveDocument.BuiltInDocumentProperties("Company").Value), Chr(150), "")
MARCA = ActiveDocument.BuiltInDocumentProperties("Comments").Value
nr1 = Replace(Trim(ActiveDocument.BuiltInDocumentProperties("Company").Value), Chr(150), "-")
marca1 = MARCA & ", " & nr1
data = ActiveDocument.BuiltInDocumentProperties("Content status").Value
cinci = Right(Replace(Trim(ActiveDocument.BuiltInDocumentProperties("keywords").Value), "/", ".") & Chr(32), 6)
cinci1 = Replace(cinci, " ", "")
primit = "BAAR-Dosarxxx" & "_" & cinci1 & "-" & data & " Mihai"
fisword = "R" & raport & "_" & BAAR & nr & " " & MARCA
FOLDER = "BAAR-Dosarxxx" & raport & "_" & cinci1 & " " & nr & " " & MARCA & "-" & data & " Mihai FIN"
anexa1 = "Anexa4_R" & raport & "_" & "DevizAudatex " & nr
anexa5 = "Anexa5_R" & raport & "_" & "Evaluare auto " & nr

Selection.Font.Bold = True
anexa4 = "Anexa4"
Selection.Font.Bold = False
  
    strpath = "D:\MIHAI\DOSARE\BAAR"
    strSubFolderNewName = strDrive & FOLDER
   Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFSO.GetFolder(strpath)
    For Each oSubFolder In oFolder.SubFolders
        If Not oSubFolder.Name Like "MICHAEL_*" Then 'Optional
            strSubFolderPath = oSubFolder.Path
            Name strSubFolderPath As strSubFolderNewName
                      Exit For
        End If 'Optional
    Next oSubFolder
Set oFSO = Nothing
Set oFolder = Nothing
Set oSubFolder = Nothing

FileCopy strDrive & "RP.vcp", strDrive & FOLDER & "\RP.vcp"
                       
strmessage = "Raport de verif pt. dosar " & BAAR & vbCr & _
                 "In atentia " & strName1 & " " & strName & "," & vbCr & _
                 "Urmare a verificarii dosarului " & BAAR & " auto pagubit marca " & MARCA & _
                 "va transmitem atasat raportul de verificare." & vbCr & _
                 "Rog confirmati primirea." & vbCr & _
                 "Cu stima," & vbCr & vbCr & _
                 "Mihai STAICU"
                 
Const STRPAT As String = strSubFolderNewName

    Set fso = CreateObject("Scripting.FileSystemObject")
    Select Case True
        Case fso.FileExists(STRPAT & "Mail AU.docx")
            Set oDoc = Documents.Add(Template:=STRPAT & "Mail AU.docx")
            strName = "Adrian Uta"
            strName1 = "d-lui"
            oDoc.Range.Text = strmessage
            oDoc.SaveAs2 FileName:=STRPAT & "AU.docx", addtorecentfiles:=False

        Case fso.FileExists(STRPAT & "Mail CT.docx")
            Set oDoc = Documents.Add(Template:=STRPAT & "Mail EN.docx")
            strName = "Cristi Turcu"
            strName1 = "d-lui"
            oDoc.Range.Text = strmessage
            oDoc.SaveAs2 FileName:=STRPAT & "CT.docx", addtorecentfiles:=False
         
        Case fso.FileExists(STRPAT & "Mail EN.docx")
            Set oDoc = Documents.Add(Template:=STRPAT & "Mail EN.docx")
            strName = "Emilia Negoita"
            strName1 = "d-nei"
            oDoc.Range.Text = strmessage
            oDoc.SaveAs2 FileName:=STRPAT & "EN.docx", addtorecentfiles:=False
                            
            
    End Select
lbl_Exit:
    Set fso = Nothing
    Set oDoc = Nothing
    Exit Sub
End Sub
The problem is at the red line of the code. The only way it will work is if I use a constant but I need the path to be strSubFolderNewName (because here is the initial docx file Mail AU.docx, Mail CT.docx or Mail EN.docx). Somehow I need to turn strSubFolderNewName into a constant that I can use.

So how can I assign that to a constant ?

Last edited by staicumihai; 11-10-2016 at 09:32 PM.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to highlight repeated words in word file and extract into excel file aabri Word VBA 1 06-14-2015 07:20 AM
Macro to check the existence of a word docx file and create a new word file with specific content. Word 2007 , when I save a .doc or .docx file the file type is showing "Empty" Tomc29 Word 9 06-10-2015 03:04 AM
How to create a MS word file that automatically logs something specific inside other WORD files? meys Word VBA 1 01-04-2015 05:22 AM
Macro to check the existence of a word docx file and create a new word file with specific content. Macro to create new word doc and save the file using String found in the document VBNation Word VBA 2 02-08-2013 07:14 AM
Macro to check the existence of a word docx file and create a new word file with specific content. Word Macro That Checks a Check Box Form Field When File Print is Executed DKerne Word VBA 4 06-09-2011 11:54 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:43 AM.


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