I am trying to figure out how to grab the working directory and pass it as a variable via a macro.
I am running a python script from a directory that is captured with user input and will always have a results subfolder. In the python script I am running Macro1 from an Excel template that is located in a different directory. The macro loads a dbf table and then formats the it the way I want and saves the newly formatted workbook to the results folder with an xlsx extension. I am having a hard time capturing the working directory and passing that as a variable. I realize that referencing various directories is confusing but the templates are unchanging and stored in a templates(Source) folder. The directory where I execute the python script from varies depending on the user name. (Just to make things more confusing, the python script is also stored in the templates(Source) folder, not the directory where I execute it from.)
When I open up an excel template does the working directory change?
This is the section of the python script I am running that is related to my issue. It works fine.
This is my macro. I can't seem to get the path variable right.
Any and all help greatly appreciated. Excel 2007.
I am running a python script from a directory that is captured with user input and will always have a results subfolder. In the python script I am running Macro1 from an Excel template that is located in a different directory. The macro loads a dbf table and then formats the it the way I want and saves the newly formatted workbook to the results folder with an xlsx extension. I am having a hard time capturing the working directory and passing that as a variable. I realize that referencing various directories is confusing but the templates are unchanging and stored in a templates(Source) folder. The directory where I execute the python script from varies depending on the user name. (Just to make things more confusing, the python script is also stored in the templates(Source) folder, not the directory where I execute it from.)
When I open up an excel template does the working directory change?
This is the section of the python script I am running that is related to my issue. It works fine.
Code:
import os.path
import win32com.client
myDir = ("C:\\tests") 'the variable grabbed earlier in python script.
mySourceExcelFile = myDir + "\\Source\\Cats1.xlsm" 'template path is not related to myDir normally
xlApp = win32com.client.DispatchEx('Excel.Application')
xlsPath = os.path.expanduser(mySourceExcelFile)
wb = xlApp.Workbooks.Open(Filename=xlsPath)
wb.Application.DisplayAlerts = False
xlApp.Run('Macro1')
xlApp.Quit()
This is my macro. I can't seem to get the path variable right.
Code:
Sub Macro1()
'
' Macro1 Macro
'
Application.DisplayAlerts = False
' ChDir "C:\tests\Target\results"
' Workbooks.Open Filename:="C:\tests\Target\results\Cats1.dbf"
' The two commented out sentences above work but directory needs to be a variable.
' Current location will always be the Target directory.
' I want to grab current directory then open Cats1.dbf from its results subfolder.
' Bombs out at ChDir and Workbooks.Open
Dim myDir As String
Dim myResultsDir As String
Dim myResultsFile As String
myDir = CurDir()
myResultsDir = myDir & "\results"
ChDir myDir & "\results"
myResultsFile = myDir & "\results\Cats1.dbf"
Workbooks.Open Filename:=myResultsFile
' All of the below works fine when above commented VBA commands are not commented out.
' SaveAs will need to be a variable path also.
ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1", ActiveCell.SpecialCells(xlLastCell)), , xlYes).Name = _
"Table1"
Range("Table1[#All]").Select
ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleLight1"
Columns("A:A").Select
Selection.ColumnWidth = 18.43
Columns("B:B").Select
Selection.ColumnWidth = 18.43
Columns("C:C").Select
Selection.ColumnWidth = 18.43
Columns("A:C").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
' The below pathname will need to be saved as a variable also
ActiveWorkbook.SaveAs Filename:="C:\tests\Target\results\Cats1.xlsx", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
Range("Table1[#All]").Copy
Sheets.Add After:=Sheets(Sheets.Count)
Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveSheet.Paste
' ActiveWindow.Close
End Sub
Any and all help greatly appreciated. Excel 2007.