Word Macro for Mail Merge export to PDF - vba

I'm running a macro in Word which worked fine before my last clean format.
The mail-merge word reads records from an Excel file.
It gives me a run-time error 5941 on the following line:
ActiveDocument.SaveAs FileName:=strDirectory & "\" & ActiveDocument.MailMerge.DataSource.DataFields("company_name").Value & ".pdf", FileFormat:=wdFormatPDF
I don't know if a reference needs to be added to the Macro or what? Pls help.
Z

Related

Converting IQy files to XLSX using VBA

I currently wrote a Pyhon script that extract metadata from SharePoint online using the "Export to Excel" button. When the data saves it saves as a IQy files (internet query file) and I need to save it as an XLSX. Theres about 30 files that I want to loop through and save.
These IQy files can be opened with excel and then I can use saveas to save it as an XLSX. Is there a way to automate this? I found some code in another forum but I don't completely understand it.
ThisWorkbook.Sheets.Copy
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & Format(Date, "mm.dd.yyyy") & " " & "Position Report Ver.2.xlsx", FileFormat:=51
ActiveWorkbook.Close
the line I don't understand is ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & Format(Date, "mm.dd.yyyy") & " " & "Position Report Ver.2.xlsx", FileFormat:=51
Would it be possible to get an explanation of this line? or is there another way to use the saveas to convert these files into XLSX? I'm open to using any other language as well.
This seems pretty clear. It's calling SaveAs to save the workbook to a particular path, using the FileFormat argument to save it as an XLSX file, which is type 51.

Visual Basic: Save in same folder as Excel Sheet

I'm very new to VB, so this is probably a very easy one. I'm creating a Word document from an Excel spreadsheet, and would like the Word doc to save in the same folder location as the spreadsheet.
I'm using the code:
.SaveAs Filename:=ThisWorkbook.Path & Range("C8").Text & ".docx"
Which I though would work, but it saves it in teh directory up from the location.
I.e. the spreadsheet is in C:/User/Documents/MySpreadsheet. But the Word doc would be saved in C:/User/Documents.
I also made a message popup to display ThisWorkbook.Path which comes up with the Spreadsheet path, so I know that's right!
I also don't think I've done the naming right, as I would like it to be named the text in cell C8. But it's actually the 'Documents' folder name with the text in C8 added on.
Thanks in advance.
Activate Immediate Window (Ctrl+G) and and add this line to your code:
debug.print ThisWorkbook.Path & Range("C8").Text & ".docx"
You will see if your path is correct. In particular, if you have "\" between folder path and filename.

How do I use VBA to create multiple directories and place files within them? Word 2013

Please bear with me, I'm rather new to macros within Microsoft Office programs. Here's some background info: I'm working in a corporate environment as part of an IT team, using Microsoft Word 2013, and I'm on Windows 7. I have full read/write access to the C:\ drive, which is where I'm working within. Any assistance is greatly appreciated.
What I am trying to do is this: I want to create a script that first creates a file folder within a directory (that does not use \ in the filepath), with the folder title being the document number +1 (or DocNum + 1), generates three files (.htm with respective HTML files in their own auto-generated folder, .txt., and .rtf) within that directory (each file has the exact same name), then moves back to the parent folder and repeats this process. The files are an email signature created from mail merging into a directory, then the directory file has the below script to create the files so we can push them down and lock them down for each user remotely.
Here's where I'm getting stuck: Trying to create the subfolder with the name being the "DocNum + 1" scheme, then creating the files within it. I am getting an Error 75. I can create each subfolder manually, but of course want the script to work. Even after 2 hours of Google searches and playing around with the VBA script I have yet to figure this out.
I do NOT need anything fancy (as defined by all sorts of error-correcting and double-checking code lines), and would prefer as bare-bones as possible (just enough to make it work as desired). I do not and cannot not install any extensions. Here's what I have so far (the entire script). Those more experienced with VBA can probably see what I'm trying to do here:
Sub BreakOnPage()
' Used to set criteria for moving through the document by page.
Application.Browser.Target = wdBrowsePage
For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")
' Select and copy the text to the clipboard
ActiveDocument.Bookmarks("\page").Range.Copy
' Open new document to paste the content of the clipboard into.
Documents.Add
Selection.Paste
' Removes the break that is copied at the end of the page, if any.
Selection.TypeBackspace
Selection.TypeBackspace
ChangeFileOpenDirectory "C:\Users\User\Desktop\mm_files"
DocNum = DocNum + 1
MkDir ("C:\Users\User\Desktop\mm_files" & DocNum)
ChangeFileOpenDirectory "C:\Users\User\Desktop\mm_files" & DocNum + 1
ActiveDocument.SaveAs FileName:="Signature" & ".rtf", FileFormat:=wdFormatRTF, AddToRecentFiles:=False
ActiveDocument.SaveAs FileName:="Signature" & ".txt", FileFormat:=wdFormatEncodedText, Encoding:=msoEncodingUSASCII, AddToRecentFiles:=False
ActiveDocument.SaveAs FileName:="Signature" & ".htm", FileFormat:=wdFormatHTML, AddToRecentFiles:=False
ActiveDocument.Close
' Move the selection to the next page in the document
Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub
You add 1 to DocNum twice: once just before creating the folder, and then again when calling ChangeFileOpenDirectory.
It would be better to pass the full path to SaveAs, instead of setting a default location.
Dim DocNum As Long, fPath As String
'...
DocNum = DocNum + 1
fPath = "C:\Users\User\Desktop\mm_files" & DocNum
MkDir fPath
ActiveDocument.SaveAs FileName:=fPath & "\Signature" & ".rtf", _
FileFormat:=wdFormatRTF, AddToRecentFiles:=False
ActiveDocument.SaveAs FileName:=fPath & "\Signature" & ".txt", _
FileFormat:=wdFormatEncodedText, Encoding:=msoEncodingUSASCII, _
AddToRecentFiles:=False
ActiveDocument.SaveAs FileName:=fPath & "\Signature" & ".htm", _
FileFormat:=wdFormatHTML, AddToRecentFiles:=False
ActiveDocument.Close
'...

UFT-API Testing-How can I can store values of response in an excel file?

I am sending a request two times using LOOP, and in output I am getting a unique value. I want to store these unique values in an excel sheet column so that further I can parameterize all the values.
I have successfully got the values to a TEXT file. But what I need is to store them in separate rows (in a column) in an EXCEL file, which I am not able to do.
Directly, I don't know. But you can always cheat. For example, EXCEL has a secondary format called .CSV, which is plain test...with ";" as separators.
So write your "output.csv" file with the following kind of structure :
Name;Income;Status
Martin;25000;OK
Johnson;32500;KO
And it should be plain openable in EXCEL. As a CSV. Once in EXCEL, save it back as a XLS or XLSX, and you're done, you've got a EXCEL file perfectly usable.
If you need some kind of automation for making this .csv into a .xlsx, here is a sample macro I made quickly & dirty, but works with EXCEL 2013 :
Sub ConvertFile()
On Error Resume Next
Kill "C:\MyPath\output.xlsx"
On Error GoTo 0
Workbooks.Open Filename:= _
"C:\MyPath\output.csv"
ActiveWorkbook.SaveAs Filename:= _
"C:\MyPath\output.xlsx", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
End Sub
Beware : this is a EXCEL macro, not vbscript.

Excel VBA code is causing my worksheets to appear as jargon

I currently am using a VBA macro that I found here on stack over flow. The problem is when I run it it saves all the data into a separate excel sheet but when i open it it appears as "jargon" in other words unreadable type. This is the "Save code"
'Save the new workbook, and close it
wb.SaveAs ThisWorkbook.Path & "\test" & WorkbookCounter
wb.Close
The way I am currently running the code is that it separates my excel sheets into different spread sheets by rows of 250. Everything works but when I open the saved documents it says that this file format is unacceptable to Excel. Then I try importing it and I get an error. Here is a snap shot of the way it appears in my screen. Also here is the file name: built.list\test.xls39
Your workbook counter always ends in a number, Windows and Excel use the file extension to determine file-type, so an '.xls39' file is unrecognisable. Try:
wb.SaveAs _
Filename:=ThisWorkbook.Path & "\test" & WorkbookCounter & ".xls" _
FileFormat:=XlFileFormat.xlExcel8
'Use xlOpenXMLWorkbook for the .xlsx format
(Use space followed by underscore to separate lines in VBA)
Or make sure WorkbookCounter ends in .xls and not a number.
(Edit: For other formats, please look in the References dialog in Excel VBA Editor)