Read a document from a file path on a Mac computer - vba

I have a working VBA Word macro that I run on a windows computer. This macro reads a document based on a file path. The path is not recognized when I try to run the macro on a Mac computer. What is the correct way to declare the file path for Word running on a Mac? My macro reads a file that contains a table. I have a loop to copy each cell as a variable to run a process. The loop works and is not shown below because the issue is that the macro does not recognize the file path and the document with the table on a Mac computer.
Dim Variable As String 'Each cell value is copied one by one into a variable
Dim FileFullPath As String 'File path for document
Dim FileDoc As Word.Document 'Word document that contains a table
Dim Row_Counter As Integer 'Row counter for word table
Dim Col_Counter As Integer 'Column counter for word table
FileFullPath = "C:\folder\file_document.docx"
Set FileDoc = Documents.Open(FileFullPath)
Variable = FileDoc.Tables(1).Rows(Row_Counter).Cells(Col_Counter).range.Text
Note: Using Documents(1) or (2) doesn't work in this case, because my process involves going back and forth between files and I cannot control which document is opened first or last by the user. Thank you

Related

Assigning existing document to variable in vba

I want to assign an existing document to a variable which I can open and activate later. There might be several documents open when running the code (including the document containing the VBA project) so I need make sure I'm activating the right one.
The below doesn't work because I need the filename and not the file-path in the final line, but I don't know how to extract this from the path in line 2...
Dim NewQuote1 as string
Set NewQuote1 ="C:\Library\doc1.docx"
Documents.Open fileName:=NewQuote1
...
Documents(NewQuote1).Activate
I'm not up on my Word VBA, but I'm pretty sure it'll be very similar to Excel.
So:
Sub Test()
Dim wrdDoc As Document
Dim NewQuote1 As String
NewQuote1 = "C:\Library\doc1.docx"
Set wrdDoc = Documents.Open(NewQuote1)
'You can now reference the document using wrdDoc.
wrdDoc.Activate
MsgBox wrdDoc.Name
End Sub

Excel 2013 VBA Workbooks.Open opens with wrong filename

When I attempt to open a certain target .xls file using the Workbooks.Open command in an Excel VBA macro, the target Excel file opens with a "1" appended to the end of the file name. This altered filename shows up in the file window (). With each subsequent attempt (manually closing the target Excel file & re-running the script) the appended number at the end of the target filename increments by one.
The target Excel file itself does not appear to be corrupted, as I am able to open the file manually in windows explorer without any issue. Also, when I do open it this way, the filename shows up correctly (without the '1' appended to the end). Looking at the 'Type' column in the Explorer window, it shows that the target file is 'Microsoft Excel 97-2003 Worksheet', so the file extension does match with the actual file format.
Below is the code that I am using:
Public Sub Import_TD_TVs()
Dim ActiveWB As Workbook
Dim srcTDWB As Workbook
Dim wkbkStr As String
Set ActiveWB = ActiveWorkbook
wkbkStr = "X:\sites\DTS_Data_Files.xls"
Set srcTDWB = Workbooks.Open(wkbkStr, , False)
End Sub
Any assistance on this issue would be greatly appreciated!
This can happen if the target workbook was originally saved as a template (.xlt) file and then the extension manually changed (to .xls) in Windows Explorer. You can open the target workbook and do a 'save as...' to .xls file format.

Programmatically Update Linked Named Range of excel object in MS Word (2007)

First question, excuse me if this has already been solved, but I've searched thoroughly and cannot find an answer:
I have linked several named ranges into a word document. This word doc (and the related excel workbook with named ranges) is a template: it's for a coworker who will make many copies of these templates (of both the word doc and the excel workbook).
I would like to include a command button in the word doc that, when clicked, will update the sources for the linked named ranges. Specifically, I want it to set the workbook with the same name as the worddoc, as the source.
The issue is that it does not like the named range I have entered. I get the:
Run-time error '6083': Objects in this document contain links to files that cannot be found. The linked information will not be updated.`
However, I have quadrupled-checked my excel doc, the named range exists. AND, when I hit Alt+F9 in word, I clearly see the link contains the named range!
{LINK Excel.Sheet.8 C:\Users\Marc\Documents\WIP_SSS.xlsm CED \a \p}
Here is my code:
Public Sub ChangeSource()
Dim filename As Variant
Dim fieldcount As Integer
Dim x As Integer
filename = Left(Application.ActiveDocument.Name, Len(Application.ActiveDocument.Name) - 4) & "xlsm"
fieldcount = ActiveDocument.Fields.Count
For x = 1 To fieldcount
'Debug.Print ActiveDocument.Fields(x).Type
If ActiveDocument.Fields(x).Type = 56 Then
ActiveDocument.Fields(x).LinkFormat.SourceFullName = ActiveDocument.Path & "\" & _
filename & "!CED"
End If
Next x
End Sub
If I don't enter the named range at all, the macro works, but it embeds the entire excel worksheet (which I do not want it to do). Any ideas on how/ why it is not liking the named range?
Thanks,
Marc
UPDATE:
With help from Bibadia, I found a solution; in addition, I want to document some strange behavior exhibited by Word VBA:
First off, the solution code:
Public Sub ChangeSource()
Dim filename As Variant
Dim fieldcount As Integer
Dim x As Integer
filename = ThisDocument.Path & "\" & Left(Application.ActiveDocument.Name, Len(Application.ActiveDocument.Name) - 4) & "xlsm"
fieldcount = ActiveDocument.Fields.Count
For x = 1 To fieldcount
On Error Resume Next
If ActiveDocument.Fields(x).Type = 56 Then
ActiveDocument.Fields(x).Delete
End If
Next x
ActiveDocument.Bookmarks("R1").Range.InlineShapes.AddOLEObject filename:=filename & "!Range1", LinkToFile:=True
End Sub
I first deleted all type 56 fields (linked object, or more technically, "wdfieldlinked"). Then, I added OLEObjects at pre-set bookmark locations.
Interestingly, just as Bibadia noted, the key was to input the LinkToFile:=True code. It seems Word will not accept the object if it is embedded: if I remove that line, I get the error Word Cannot obtain the data for the C:\...\document!NamedRange link.
Finally, I found one other odd behavior: When trying to simply replace the link, using this code,
ActiveDocument.Fields(1).LinkFormat.SourceFullName = filepath+name & _
"!CED" 'that is the named range
it would work once, when I changed both the word document's and the excel workbook's filenames (see original message for context). So, when the new filepath+name DID NOT match the existing filepath+name, Word VBA accepted the change. However, once initially updated, if I tried to run the macro again, I would get:
run-time error '6083': Objects in this document contain links to files that cannot be found. The linked information will not be updated.
I would get this error even if I changed the named range to another named range in the same worksheet (and obviously same workbook). So it appears that Word VBA does not like "updating" filepath+name when the filepath+name does not change.
Just so anyone who didn't know (like me) now knows. Sorry for the long update, I just wanted to be thorough.
I am not completely sure of this, but it is a little too long for a comment.
As far as I know, you can only set LinkFormat.FullSourceName to the name of a file, not a fullname + subset name, which is what you are trying to do when appending the "!CED". Although you can read the subset name (CED) from OleFormat.Label, you can't modify it as it's a read-only property.
So if you actually need to modify the subset name (CED), AFAICS the only way to do it is to delete and reinsert the LINK field. If you reinsert using Fields.Add, you just specify the text of the field, so you can get the file name and Subset name right. What is slightly confusing is that if you insert a LINK using InlineShapes.AddOleObject, you can specify fullname+subset name in exactly the way that you are trying to do in your code.
However, I do not think you are trying to modify the Subset name. So let's assume that you already have a LINK field along the lines of
{ LINK Excel.SheetMacroEnabled.12 "the full pathname of a .xlsm file" CED \a f 0 \p }
Word will only be able to update that link if the path+filename is valid (i.e. there's a .xlsm at that location, the workbook has a Range Name called CED, and the Range Name is in the first Sheet. Otherwise, you have to specify a Sheet name as well, e.g.
{ LINK Excel.SheetMacroEnabled.12 "the full pathname of a .xlsm file" Sheet2!CED \a f 0 \p }
It's just a guess, but if your code is trying to connect to a Workbook where the range defined by CED is not in the first sheet, you would see the error you describe.
Further, the scope of the CED Range Name has to be either "workbook" or the name of the first sheet. Otherwise, if the scope is the first sheet but the range is actually in another sheet, or vice versa, I do not think Word can make the connection whatever subset name you provide (my guess is that Word never really caught up with Excel after Excel introduced multi-sheet workbooks).
If CED can reference sheets other than the first one, I think you will probably have to use the Excel object model to discover which sheet its Range is in, construct the appropriate Subset name, and delete/re-insert the LINK field.

Extracting certain cells from excel files in a specified directory

I am a VBA novice and I am trying to write a code that will copy the following data from multiple csv files all stored in the same directory.
I need it to open each csv file
Check IF Row 8 in columns H through CA for any cells that have the value ="TotalLMP" (Sample: Cell H8="TotalLMP")
THEN copy the value from both Row 7 and Row 19 of any columns that have that value ="TotalLMP" in Row 8 in two new columns (Sample: SINCE H8="TotalLMP", COPY H7="100" AS COLUMN A, COPY H19 = "26.437" AS COLUMN B)
THEN copy the value from cell $A$9 in a third column (Sample: COPY A9="20100101" AS COLUMN C")
after finishing loop through each csv file close and go to next
Then in the new active worksheet in the blank excel file would store each value as follows:
.......A .............. B ................ C
1 .. 100 .... 26.437 .... 20100101
2 .. 200 .... 26.585 .... 20100101
Let me help you with the CSV looping for now since this is rather hard for a beginner. I'm sure you will figure out how to test for a value in row 8. If not, you can always ask for more help!
In order to do so, you will have to use the Microsoft Scripting Runtime.
I suggest placing all of the csv file you want to open in the same directory, and only those to avoid potential problems.
Open a new workbook and go to the VBE (ALT + F11). Create a new module. Click in this new module, then go to Tools > References> Microsoft Scripting Runtime. This will let it know it will have to use that module and its objects.
Save the workbook as an macro-enabled workbook (.xls or .xslm for newer versions) in the same directory as your CSV (or somewhere else...)
Then start coding:
Sub Import_all_Csv()
' Reference Needed: Microsoft Scripting Runtime
' Dim some pointers to know what objects you will be manipulating thereafter
Dim MyWs, CSV As Worksheet
Set MyWs = ActiveSheet ' Meaning you will have to run the macro from the spreadsheet you want to export to. Feel free to replace that
Dim wbCSV As Workbook
' Those are the objects that belong to the reference Microsoft Scripting Runtime
Dim oFSO As FileSystemObject
Dim oFld As Folder
Dim oFile As File
Dim File As String
' Initialize the FileSystemObject
Set oFSO = New FileSystemObject
' That will only work on windows so I'm adding an error handler to ignore it if need be
On Error Resume Next
ChDir ThisWorkbook.Path
On Error GoTo 0 ' I'm asking VBA to throw an error now
' Dialog box to select the first csv file (this will let you choose another directory everytime)
File = Application.GetOpenFilename("Comma Separated Values File (*.csv*), *.csv*")
If File = "False" Then
Exit Sub ' Quit the macro if user canceled
Else
' Else get the path of the parent folder of that file
Set oFld = oFSO.GetFolder(oFSO.GetParentFolderName(File))
End If
' Go through each file in that folder
For Each oFile In oFld.Files
' Only open the CSV files
If oFile.Type = "Microsoft Excel Comma Separated Values File" Then
' Open it and set the first sheet (There is only one anyway)
Set wbCSV = Workbooks.Open(oFile)
Set CSV = wbCSV.Sheets(1)
' ============================
' Do what you want to do Here
' THIS IS A PLACEHOLDER
' Example to copy value of H8 in the CSV file to A2 the destination worksheet so you can see how to point to the correct cells in both files
MyWs.cells(1,2).value = wCSV.cells(8,8).value
' End of what you want to do
' ============================
' Close the CSV file without savings changes before going through the next one
wbCSV.Close False
End If
Next oFile
End Sub
I hope this helps! Good luck learning more VBA!
Best,
Julien

VBA - Copy as Path

I need help with a coding requirement that I've not previously experienced. I just browsed a similar issue raised here a couple of years ago - VBA to Copy files using complete path and file names listed in Excel Object.
My issue is similar but somewhat simpler than the OP.
I have a number of folders that each contain about 100 small .csv files; for each folder I need to copy the path for each file to an open worksheet. Each folder of .csv files has its own associated workbook.
As one example, the open workbook is F:\SM\M400AD.xlsm and the active worksheet is CSV_List. The folder containing the .csv files is F:\SM\M400AD.
Doing it manually, my sequence is then:
Open folder F:\SM\M400AD
Select all
Copy path
Paste to Range("B11") of worksheet CSV_List
When I do it manually, as described above, I get a list that looks like:
"F:\SM\M400AD\AC1.csv"
"F:\SM\M400AD\AC2.csv"
"F:\SM\M400AD\AE.csv"
"F:\SM\M400AD\AF.csv"
"F:\SM\M400AD\AG.csv"
"F:\SM\M400AD\AH1.csv"
"F:\SM\M400AD\AH2.csv"
"F:\SM\M400AD\AJ.csv"
and on down the page until I have a list of 100 paths. This single column list is then pasted into worksheet CSV_List, starting at Range("B11").
I need to automate this and would be grateful if a VBA guru could kindly code this for me.
Such of question has been asked before, for example:
Loop through files in a folder using VBA?
List files in folder and subfolder with path to .txt file
The difference is you want to "automate" it, which means you want to execute code on workbook Open event.
How to achieve that?
Open F:\SM\M400AD.xlsm file.
Go to Code pane (ALT+F11)
Insert new module and copy below code
Option Explicit
Sub EnumCsVFilesInCurrentFolder()
Dim sPath As String, sFileName As String
Dim i As Integer
sPath = ThisWorkbook.Path & "\"
i = 11
Do
If Len(sFileName) = 0 Then GoTo SkipNext
If LCase(Right(sFileName, 4)) = ".csv" Then
'replcae 1 with proper sheet name!
ThisWorkbook.Worksheets(1).Range("B" & i) = sPath & sFileName
i = i + 1
End If
SkipNext:
sFileName = Dir(sPath)
Loop While sFileName <> ""
End Sub
Now, go to ThisWorkbook module and insert below procedure:
Private Sub Workbook_Open()
EnumCsVFilesInCurrentFolder
End Sub
Save and close workbook
The workbook is ready to use. Whenever you open it, EnumCsVFilesInCurrentFolder macro will be executed.
Note: you have to change above code to restrict the number of records.