PDA

View Full Version : [SOLVED:] Use string to set folder path.



staicumihai
12-30-2015, 06:58 AM
Hy everyone, I have the following problem with a VBA macro written in Microsoft Word 2010:

Option Explicit
Sub BAAR()

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
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
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 & marca & " " & nr
folder = "BAAR-Dosar" & raport & "_" & cinci1 & " " & nr & " " & marca & "-" & data
Name strDrive & primit As strDrive & folder

' the problem is at the next two lines: instead of saving the next 2 files (word and pdf) in the path strdrive & folder, the macro saves the 2 documents in path strDrive with name folder & fisword ( I need the path to be strDrive & folder and name fisword)

ActiveDocument.SaveAs2 strDrive & folder & fisword & ".docx"
ActiveDocument.ExportAsFixedFormat OutputFileName:=strDrive & folder & fisword & ".pdf", _

ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=False, _
OptimizeFor:=wdExportOptimizeForPrint, _
Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, _
IncludeDocProps:=True, _
KeepIRM:=True, _
CreateBookmarks:=wdExportCreateHeadingBookmarks, _
DocStructureTags:=True, _
BitmapMissingFonts:=True, _
UseISO19005_1:=False

lbl_Exit:
Exit Sub
End Sub


I wish everybody a Happy New Year !

Paul_Hossler
12-30-2015, 01:49 PM
See if adding ChDir helps



ChDir strDrive & folder

ActiveDocument.SaveAs2 fisword & ".docx"
ActiveDocument.ExportAsFixedFormat OutputFileName:= fisword & ".pdf",

staicumihai
01-03-2016, 08:49 PM
Hy Paul,
I tried with ChDir and same results, it saves under initial strDrive path, it doesnt take into consideration folder string after strDrive.
Thanks anyway.

gmayor
01-04-2016, 12:04 AM
The problem arises because you have not inserted a path separator (Chr(92) or "\") between the path and the filename and so the folder segment is added to the filename.
You will also need to create the folder if it doesn't exist. There is code on my web site to facilitate that.

staicumihai
01-07-2016, 09:10 PM
Thanks a lot man.