I have huge number of Word files I need to merge (join) into one file, and will be time consuming to use the Word merger (one by one). Have you experienced any tool that can handle this job?
Sub MergeAllDocuments(AllDocumentsPath as String, MasterDocumentPath as String)
Dim MasterDocument As Document
Set MasterDocument = Documents.Open(FileName:=MasterDocumentPath)
TheDocumentPath = Dir(AllDocumentsPath , vbNormal)
While TheDocumentPath <> ""
' Append the next doc to the end of the master doc. (The
' special "\EndOfDoc" bookmark is always available!)
MasterDocument.Bookmarks("\EndOfDoc").Range.InsertFile TheDocumentPath
TheDocumentPath = Dir
Wend
MasterDocument.Save
End Sub
MergeAllDocuments "C:\MySeparateDocuments\*.doc", "C:\MasterDocument.doc"
I have one question - why do you want do do such a thing (with a "huge number" of documents, at least)?
I came across a post by Graham Skan a while back. It might get you started:
Sub InsertFiles()
Dim strFileName As String
Dim rng As Range
Dim Doc As Document
Const strPath = "C:\Documents and Settings\Graham Skan\My Documents\Allwork\" 'adjust as necessary '"
Set Doc = Documents.Add
strFileName = Dir$(strPath & "\*.doc")
Do
Set rng = Doc.Bookmarks("\EndOfDoc").Range
If rng.End > 0 Then 'section break not necessary before first document.'
rng.InsertBreak wdSectionBreakNextPage
rng.Collapse wdCollapseEnd
End If
rng.InsertFile strPath & "\" & strFileName
strFileName = Dir$()
Loop Until strFileName = ""
End Sub
Have you tried using the Word COM api? You can automate lots of things - maybe you can automate a merge.
Do you really need to do an actual merge, or do you want to join the files together. The two things are quite different.
Merging is used when you have two versions of an original file with (potentially conflicting) changes. I can't really see how you would have a "huge number" of files that you needed to merge all together. This would be an absolute nightmare of conflicts. Do you mean to merge sets of them into individual files?
Joining would be when you want to concatenate them one after the other. This would be a lot easier to do. This is quite possible using the COM api.
Related
Is it possible to run a single macro for all xls/xlsx files and if so how. The macro shown below scales the excel file to fit to single page which is necessary as the number of columns is 19 and is needed to convert it to pdf using lo cli.
Libre office version: 6.0.6
Macro has been recorded with libreoffice and can be seen below:
REM ***** BASIC *****
sub Main
rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
vrem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:PageFormatDialog", "", 0, Array())
end sub
Please let me know if any info is needed regarding the tests.
Got the answer from one of the developers at Libreoffice and it works like a charm, so sharing it here. The link to the answer can be found here
Mike's Solution
First: your recorded macro wouldn't work: it doesn't apply changes, it just opens a dialog. Please always test recorded macros :-)
You may use this macro instead:
Sub FitToPage
Dim document As Object, pageStyles As Object
document = ThisComponent
pageStyles = document.StyleFamilies.getByName("PageStyles")
For i = 0 To document.Sheets.Count - 1
Dim sheet As Object, style As Object
sheet = document.Sheets(i)
style = pageStyles.getByName(sheet.PageStyle)
style.ScaleToPagesX = 1
Next
On Error Resume Next
document.storeSelf(Array())
document.close(true)
End Sub
It operates on the current document, and after setting the scale, it saves (overwrites!) and closes the document.
To use this macro from a command line, you need to save it to some of libraries, e.g. Standard. In my example below, I use Module1 to store it.
You may use this macro on a single document like this:
'path/to/LibreOffice/program/soffice' path/to/excelfile.ext macro:///Standard.Module1.FitToPage
To use it on multiple documents, you need to make this in a loop (mentioning multiple filenames as arguments to a single soffice invocation, like with shell globbing on Linux using *, will not work - actually, it will only run the macro for the last document, keeping the others open and unmodified). A loop for Windows could be like this:
for %f in (*.xls) do start /wait "" "C:\Program Files\LibreOffice\program\soffice.exe" "%f" macro:///Standard.Module1.FitToPage
I am trying to generate a pdf file, based on the contents of an Access Form or a Report (either is fine for me but understand it should be done via a report).
I can generate/populate the form itself, and the report itself, but when I create the pdf with this code, the fields in the pdf are blank. The controls are all there, like seen in my form, but the fields that should have data don't get copied over.
This works as-is for me, and makes a pdf that looks just like my report, except the data from the report does not carry over to the saved pdf
Private Sub pdfBtn_Click()
Dim fileName As String, fldrPath As String, filePath As String
Dim answer As Integer
fileName = "File_Test1" 'filename for PDF file*
fldrPath = "C:\Users\myname\Desktop" 'folder path where pdf file will be saved *
filePath = fldrPath & "\" & fileName & ".pdf"
'check if file already exists
If FileExist(filePath) Then
answer = MsgBox(prompt:="PDF file already exists: " & vbNewLine & filePath & vbNewLine & vbNewLine & _
"Would you like to replace existing file?", buttons:=vbYesNo, Title:="Existing PDF File")
If answer = vbNo Then Exit Sub
End If
On Error GoTo invalidFolderPath
DoCmd.OutputTo acOutputReport, "Report2", acFormatPDF, filePath
MsgBox prompt:="PDF File exported to: " & vbNewLine & filePath, buttons:=vbInformation, Title:="Report Exported as PDF"
Exit Sub
invalidFolderPath:
MsgBox prompt:="Error: Invalid folder path. Please update code.", buttons:=vbCritical
End Sub
Function FileExist(FileFullPath As String) As Boolean
Dim value As Boolean
value = False
If Dir(FileFullPath) <> "" Then
value = True
End If
FileExist = value
End Function
I've spent a few hours searching, so I'd be upset if it is something overly simple and available. But is anyone able to point me in the right direction with this?
It looks like I had some logic wrong. Yes the code provided does work, thanks again #June7. I was over complicating things by trying to generate the pdf off the form itself, versus off a report. I ended up making the button pop up the report instead of another form, and the data does now flow as expected.
Silly, like I said :(
I worked on some VBA code to change all the sourcelocation of all the images. However it is detecting only 3 InlineShapes. There are many more. I inserted all images as Insert and LInk
Sub ChangesSource()
Dim i As Long
Documents("Document.docx").Activate
Debug.Print ActiveDocument.Name
With ActiveDocument
For i = 1 To .InlineShapes.Count
With .InlineShapes(i)
'.shp.LinkFormat.SourceFullName =
.LinkFormat.SourceFullName = Replace(.LinkFormat.SourceFullName, "C:\oldLink", "C:\newLink")
Debug.Print .LinkFormat.SourceFullName
'Debug.Print InlineShapes(i).SourceFullName
End With
Next i
End With
End Sub
Try:
Sub ChangeSource()
Dim i As Long
With ActiveDocument
For i = .InlineShapes.Count To 1 Step -1
With .InlineShapes(i)
If Not .LinkFormat Is Nothing Then
With .LinkFormat
.SourceFullName = Replace(.SourceFullName, .SourcePath, "C:\NewPath\")
End With
End If
End With
Next i
End With
End Sub
If you save the document in the older .doc format, pressing Alt-F9 will expose the INCLUDEPICTURE fields containing the links. You can then use Find/Replace to update the paths (note the use of \\ for the path separators). Pressing Alt-F9, then Ctrl-Shift-F9 will refresh the linked images. You can then re-save the document in the docx format, if you wish. The other option is to use a macro.
I have a form which has a ComboBox on it that pulls all DISTINCT colleague names from a huge table that includes all of our sales (50k+ records). It works perfectly fine, but it takes 3-4 minutes to open the form because it takes so long for Access to find all unique colleague names in the table.
I've been trying to research this and found something that looks useful, but can't seem to get it right.
The code I have at the moment:
Private Sub CollName_Change()
Dim strText As String
strText = Nz(Me.CollName.Text, "")
If Len(strText) > 2 Then
Me.CollName.RowSource = "SELECT CollPerf.Colleague FROM CollPerf WHERE CollPerf.Colleague LIKE ""*"" & strText & ""*""; "
Me.CollName.Dropdown
End If
End Sub
I found this code on two forums, this is supposed to do the following: "the key is to not have a Row Source defined for the Combo Box. The row source will be defined as the user starts typing letters. Once they get to 3 letters then the row source of the combo box will be defined and the combo box will be told to dropdown."
When I get to 3 letters, a dropdown appears, but it's blank, it doesn't display any results.
I'm relatively new to Access, although already built two databases, but they all have relatively basic SQL queries, so I have no idea what I'm not doing right here.
Any advice? Or alternatively a different solution as to how take my combo box faster and still keep values unique?
You only have some double-quote mixup there. It is much easier to use single quotes instead of double double-quotes.
Me.CollName.RowSource = _
"SELECT CollPerf.Colleague FROM CollPerf WHERE CollPerf.Colleague LIKE '*" & strText & "*';"
But your query would be way faster if you would only use the starting letters, i.e. remove the leading *
Me.CollName.RowSource = _
"SELECT CollPerf.Colleague FROM CollPerf WHERE CollPerf.Colleague LIKE '" & strText & "*';"
But that depends on your requirements.
EDIT to debug:
Dim strText As String
Dim strSelect As String
strText = Nz(Me.CollName.Text, "")
If Len(strText) > 2 Then
strSelect = "SELECT CollPerf.Colleague FROM CollPerf WHERE CollPerf.Colleague LIKE '*" & strText & "*';"
Debug.Print strSelect
Me.CollName.RowSource = strSelect
Me.CollName.Dropdown
End If
Copy strSelect from the Immediate Window into a query, and try it out. It should help resolve the problem.
See How to debug dynamic SQL in VBA
I have a lot of Visio files, and want to programatically convert them all to png format.
I have found a couple of solutions, but can't get any of them to work.
How can .vsd files be batch-converted to .png easily through commandline or a similar means?
Use the built in Visual Basic
something like this: fix up the paths parts yourself.
Sub a()
Dim docs As New Collection
' add the paths of your documents here, use more script if you want wildcard etc
docs.Add ("C:\Users\[username]\Desktop\New folder (4)\drawing2.vsd")
docs.Add ("C:\Users\[username]\Desktop\New folder (4)\drawing3.vsd")
For Each d In docs
Dim doc As Document
Set doc = Documents.Add(d)
Dim p As Page
For Each p In doc.Pages
Dim n As String
' change this for the output path and format of your choice
n = "C:\Users\[username]\Desktop\New folder (4)\" & doc.Name & " " & p.Index & ".png"
p.Export (n)
Next
Next
End sub