VBA code to open two files using an application - vba

I'm relatively new at VBA, and I am developing this excel based tool that creates two .xls files, whose paths are saved in two variables. The next step is the user has to select these two files, right click and select an application called Compare.
I was wondering if this process could be automated using Shell commands in VBA. Please advise.

Yes, it is possible. Assuming the compare takes two files as arguments, just do something like this:
Public Function OpenCompare(strFileOne As String, strFileTwo As String)
Dim x As Variant
x = Shell("Compare.exe " & strFileOne & " " & strFileTwo, vbNormalFocus)
End Function

Related

Merge pdf files with VBA and Foxit

I use Foxit Phantompdf, the complete version and ACCESS.
In our program, we have to save multiple pdf files, some of them should be merged in single files when saved.
Here is the code I use;
Dim phApp As PhantomPDF.Application
Dim n1 As String
Dim n2 As String
n1 = "c:\Temp\F3769-190136-GROUPE OCÉAN.pdf"
n2 = "c:\Temp\f3769-190136-GROUPE OCÉAN- facture.pdf"
Set phApp = CreateObject("PhantomPDF.Application")
Dim phCreator As PhantomPDF.Creator
Set phCreator = phApp.Creator
***'Call phCreator.CombineFiles("c:\Temp\F3769-190136-GROUPE OCÉAN.pdf|c:\Temp\f3769-190136-GROUPE OCÉAN- facture.pdf", "c:\Temp\F3769-190136-GROUPE OCÉAN.pdf", COMBINE_ADD_CONTENTS)***
Call phCreator.CombineFiles("""c:\Temp\" & n1 & "|'" & n2 & """" & ", " & """c:\Temp\F3769-190136-GROUPE OCÉAN.pdf"""" &", COMBINE_ADD_CONTENTS)
phApp.Exit
When I try it with the complete files names (in bold) the code works perfectly.
However, when I try to use variables, I get a
"Argument not optional"
error.
Can somebody help me ?
Thanks
Your string definitions in call line is incorrect.
You have defined n1 and n2 with the c:\temp already, and in your string conversion you add this again. I do not know if this is the route cause to your issue.
Furthermore I do not know the actual needed syntax for this phcreator.combine()
But is it not possible using only:
call pHcreator.combine(n1 & "|" & n2, …
The 'Argument not option' might imply you should add another input to your pHcreator, I would guess it could have something to do with FOXIT's combine function page settings. Try adding a input variable integer at the end of the function maybe?
But the fact that it works when writing strings in clear text would maybe suggest that the string manipulations is not correct?
I'm not a vba professional, but interested in the outcome, working myself with Foxit and also want to combine with vba. I'm currently not using version 9 som I seem to be out of luck, and only upgrading it I know what I want to do is possible.
I tried it, but got the same error message. So I took advantage of the fact that the function works when the file names are in plain text. I copied the files to be merged in a temporary folder and rename them. The renamed files are the then used in the merge function. It works perfectly, but Foxit adds a table of content page, and I don't know, yet, how to remove it. Here is my solution:
Private Sub Command4_Click()
Dim addi As String 'file to be merged to main file
Dim princi As String 'main file
Dim phApp As PhantomPDF.Application
'A temporary folder, in this case c:\t2, should be present
'In this example c:\Temp is the working folder
addi = "c:\Temp\filetomerge.pdf" 'full path of file to be merged
princi = "c:\Temp\mainfile.pdf" 'full path of main file
'fadd,pdf and fmain.pdf are the temporay files used in Foxit's function
FileCopy addi, "c:\t2\fadd.pdf" 'temporary file to be merged in temporary folder
FileCopy princi, "c:\t2\fmain.pdf" 'temporary main file in temporary folder
'Merge action
Set phApp = CreateObject("PhantomPDF.Application")
Dim phCreator As PhantomPDF.Creator
Set phCreator = phApp.Creator
Call phCreator.CombineFiles("c:\t2\fmain.pdf|c:\t2\fadd.pdf", "c:\t2\fmain.pdf", COMBINE_ADD_CONTENTS)
phApp.Exit
'Save merged file in working folder under main file name
Kill princi
FileCopy "c:\t2\fmain.pdf", princi
'delete temporary files
Kill "c:\t2\fadd.pdf"
Kill "c:\t2\fmain.pdf"
End Sub

Excel: Is that possible to erase names from other files using vba macro?

Note : I am a total newbie in VBA and Excel, but I know my stuff in programming. This is why I am posting this for a friend of mine.
He tried to remove all names he had in his workbook (1 workbook = 1 file, right ?), so he applied this macro he found on the web, without exactly knowing what it does :
Sub Del_Name()
Dim Loop As Integer
For Loop = ActiveWorkbook.Names.Count To 1 Step -1
If MsgBox("Erase: " & ActiveWorkbook.Names(Loop).Name & " - " & ActiveWorkbook.Names(Loop).Value & "?", vbQuestion + vbYesNo, "Confirm...") = vbYes Then
ActiveWorkbook.Names(Loop).Delete
End If
Next
End Sub
Then, in his file's name manager there are names referencing to some other files, such as :
http://randomServer:port/user/randomFolder/[file.xls]randomSheet'!$AD$6:$AF$6
\\random\folder[anotherFile.xls]anotherSheet'!#REF!
He doesn't know where this names come from.
So, his concern, and my question here : is that possible that the vba macro erased names within distant files ? Is that even possible ?
I have looked hours for information about that, some posts I have read suggest this could be possible, but I am still quite confused, any help would be much appreciated.
Thanks in advance
no. it is not possible that the vba macro erased names within remote files.
all it did was to delete a list of links that were stored in the excel file.
same as if you delete a favourite link in your web browser, you are not actually changing anything on the website that link pointed to.
it is possible to use a macro to modify remote files. you must have access to the file system that contains the file and you must have "write" permission to that file. then you could open the file and update it. or run an update SQL query. plus some other ways that i cannot think of right now.

VBA Excel variables value after closing the excel file

I have an user form which selects a directory path. That path is stored in a variable. The problem is that every time I start the macro, I have to rechoose the path for the directory. How should be the path variable declared in order to keep it's value once the macro is closed, or the excel file is closed ?
Persisting data between calls to a macro is simpler; use a variable at module scope in the .bas module containing the macro.
Persisting data on saving is more difficult. You could write to the registry but that's tricky and you'll need to use various Windows API functions.
The simplest thing to do would be to write the data to somewhere on the workbook.
The registry is simple in VBA. It's is very limited and uses ini file concepts.
There's a few of them such as (from Object Browser [F2] in VBA editor)
Function GetAllSettings(AppName As String, Section As String)
Member of VBA.Interaction
Sub SaveSetting(AppName As String, Section As String, Key As String, Setting As String)
Member of VBA.Interaction
Sub DeleteSetting(AppName As String, [Section], [Key])
Member of VBA.Interaction
Function GetSetting(AppName As String, Section As String, Key As String, [Default]) As String
Member of VBA.Interaction
Also the newer Windows Scripting Host Shell object also makes registry access easy.
object.RegDelete(strName)
object.RegRead(strName)
object.RegWrite(strName, anyValue [,strType])
Also WMI can access registry. Unlike both above methods it can ennumerate, so you can see what is there without have to know in advance.
Dim proglist()
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
ret = oReg.EnumKey(&H80000002, "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", proglist)
If err.num =0 then
For each prog in proglist
msgbox prog
Next
Else
Msgbox err.num & " " & err.description & " " & err.source
err.clear
End If
It can also check security and monitor changes to keys.
I would use a Name, which can be saved with the workbook.
ThisWorkbook.Names.Add Name:="SavedPath", RefersTo:= someString
Another simple alternative is to put that value in a cell, eventually in a hidden sheet, or a hidden row/column of any suitable sheet.
A third solution is to add it to the file properties.
Registry tricks are complex, don't work if you change machine or if user has not enough rights.
I have solved the problem storing the data on a cell and then on excel file initialization giving the data to corresponding variables. Thank you very much for those who thought to a solution !

Creating a directory named with user's text in Visual Basic

I'm fairly new to programming in general. I'm working on a simple app that could combine a couple of functions to automate or simplify some of the things I do at work. One of the functions I'm trying to build is to be able to create a folder. Now I have found an article on that on Microsoft's msdn resource and it's child's play. But the instructions there only show how to create a folder with a predefined name in the code. What I'd rather want is to have a textbox where I input the folder's name and the directory is named with that input. The msdn code looks like this:
My.Computer.FileSystem.CreateDirectory _
("C:\vb\")
I understand I should now add:
Dim txt As String
txt = TextBox1.Text
But what next? How do I tell VB to use as directory name the input "txt"?
Try this:
Dim txt As String
txt = TextBox1.Text
My.Computer.FileSystem.CreateDirectory("C:\" & txt & "\")
Using & is simple and often fine for most purposes, but for your two (including what you've added as a comment) concatenation examples there are other methods:
For pathname manipulation look at System.IO.Path:
My.Computer.FileSystem.CreateDirectory(Path.Combine("C:\", txt))
For (complex) string "formatting", consider String.Format:
Dim menu As String = String.Format("Today's main dish is {0}.", TextBox2.Text)

How to create a dynamic excel link in VB.NET? (not .dll)

VB.NET level: Beginner
I made a .exe using VB.NET. In this program there are many links to excel files (There are specific folders for excel files).
My problem:
Consider an excel file named as abc.xlsx, which is on my home pc. Link to this file is as follows,
D:\work\data\abc.xlsx
now for obvious reasons, this link will not be valid when I run the .exe on my work pc.
(Later I want to run this .exe on multiple pc's)
How to solve this issue?
My thinking is to create a dynamic link which will update itself based on pc in use or to create a constant link which is independent of pc in use.
Help will be really appreciated.
Thanks in advance
so make the path relative to the .exe
C:\myapp\myapp.exe
c:\myapp\data\abc.xslx
...
So no matter where you app is, you can get to your data like this
Dim dataFolder As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly(‌​).Location)
dataFolder = System.IO.Path.Combine(dataFolder,"data")
Dim theFileIwant as String = System.IO.Path.Combine(datafolder,"abc.xslx")
Try something like this:
Public Function GetDynamicFilename(p_filename As String) As String
Dim tempPath As String
Select Case My.Computer.Name.ToUpper
Case "COMPUTER1"
tempPath = "c:\work\data"
Case "COMPUTER2"
tempPath = "d:\work\files"
End Select
Return String.Format("{0}\{1}", tempPath, p_filename)
End Function