Can I use CreateObject to print a .tif document with vba? If not, what? - vba

The following vba routine works well to print a word document
If sHlink <> "" Then
Set OfficeObject = CreateObject("Word.Application")
OfficeObject.Documents.Open sHlink
OfficeObject.PrintOut Background:=False
OfficeObject.Quit
Set OfficeObject = Nothing
End If
but what I need to print are .TIF documents. They open by default with the Microsoft Photo Viewer. Is there something similar that will call the MS Photo Viewer, or failing that, Acrobat? Perhaps with Acrobat could I use some kind of command line?
Thank You

I found this shell command that prints using the Photo Editor that was formerly installed with Office:
Shell """C:\Program Files\Common Files\Microsoft Shared\PhotoEd\photoed.exe"" -p h:\misc\MyPicture.tif"
I think it can be adapted to print using the current Picture Manager - I bet it uses the same -p switch. This seems to be named "OIS.EXE" for some reason.

Related

Documents.Open Format: .mht in word

Problem: Word sometimes doesn't choose "Single File Web Page" format automatically for .mht files.
Description:
When opening files in Word application there is an option to select file conversion format:
For the .mht files to be correctly decoded/viewed i noticed that selecting format "Single File Web Page" works perfect.
Is it possible to achieve this programmatically? Lets say I would like to open the .mht file in word application, and use word's converter to treat it as a "Single File Web Page" file.
So far I have found that Documents.Open method (https://learn.microsoft.com/en-us/office/vba/api/word.documents.open) accepts parameter "Format". But it seem like it doesn't have the format I need. The closest I see is wdOpenFormatWebPages(7), but it is not the same as "Single File Web Page"
https://learn.microsoft.com/en-us/office/vba/api/word.wdopenformat
VB:
Documents.Open FileName:="C:\test.mht", format:=7
C#:
Application app = new Application();
Document document = app.Documents.Open(FileName: #"C:\test.mht", Format: 7);
Good day community,
Microsoft support have already document this .mht issue and offer a simple solution. I use it to get quick 'highly detail QA and training document in Word format' from PSR.exe recording (also provide in all Windows terminal since Win7). I have a conversion script to do it in batch for my tester/trainer at my compagny. With the right title and the command 'dir /b', it also build a table of content to help research for newly employés :
Ref : https://support.microsoft.com/en-us/topic/the-confirmconversions-property-in-macro-changes-the-confirm-conversion-at-open-option-in-word-6f16c1db-4cb8-2727-dc4f-fdf6ef112ff5
Sub MyDocumentOpenMacro()
Dim x As Integer
' Set x equal to the current setting of the Confirm conversion at Open
' option before opening your file.
x = Application.Options.ConfirmConversions
' Open your file.
Documents.Open "C:\My Documents\Address.txt", ConfirmConversions:=False
' Use a conditional statement to set the Confirm conversion at Open
' option back to its setting (value of x) before opening your file.
If x = "0" Then
Application.Options.ConfirmConversions = False
Else
Application.Options.ConfirmConversions = True
End If
End Sub

How to prevent Excel Add-In file from changing cell references to R1C1/columns to letters

I created an Excel Add-In file (.xlam) to be able to distribute my macro the my department. However, I'm faced with an issue that I can't seem to find when I search the web for answers. When I add and install the Add-In file to Excel (via vbscript, if that matters) it sets Excel to R1C1 mode, so the columns are numbered instead of lettered. Any idea what might be causing this? Could it something in the vbscript or Add-In files that trigger this change? Has anyone ever had this happen to them before when deploying an Add-In for Excel? How do I prevent it?
Try to look if you changing Application.ReferenceStyle = xlR1C1
If not, i would try to insert
Dim previousRefStyle
previousRefStyle = Application.ReferenceStyle
Application.ReferenceStyle = xlA1
and on the begining
Application.ReferenceStyle = previousRefStyle
So user will have restored original settings
Okay, it appears I found the actual solution from this link, where Post #10 on that forum thread points to Tip 2 in this link.
The steps are as follows:
Exit Excel (After adding the Add-In)
Click Start > Run > Enter Excel.exe /UnregServer
Wait for Excel to finish opening again
Exit Excel (again),
Click Start > Run > Enter Excel.exe /RegServer
What this does is cleans (un-registers & re-registers) the registry. I hope this will save even one person the hours of googling and forum surfing that it took me to finally stumble upon the actual solution, instead of just a workaround.
Update to include VBScript Implementation Example:
To accomplish the above steps using VBScript (below script adapted from here), you can use code similar to this (changing your path to your Excel.exe of course)
Dim objFSO, objShell
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject ("WScript.shell")
objShell.Run "cmd /c ""C:\Program Files (x86)\Microsoft Office\Office14\excel.exe"" /unregserver && timeout /t 3 && tskill excel && ""C:\Program Files (x86)\Microsoft Office\Office14\excel.exe"" /regserver",1,True
Set objFSO = Nothing
Set objShell = Nothing
x=msgbox("Excel registry refreshed." ,0, "Registry Update")
wscript.quit
Disclaimer:
As #Rory points out below (see link in his comment), according to Microsoft's documentation those switches don't work from versions dated 2010 on. Though there are many instances of people citing that they have used this method with 2010 or later versions with success (see links in my comments) I figured I would just make whoever is reading this aware that it is a now-unsupported method by Microsoft. However, if it works for you and your situation (as many unsupported features of Microsoft often do) feel free to still use it.

Run Unix Command via VBA and return the results in Excel Spreadsheet

I have this Unix command
cat /dataops/profits/name.csv
Can I save it to a .txt file and run via VBA Macro, and then automatically return the results into my Excel spreadsheet?
The hardest part is getting the file from the Unix box over to your Windows computer. You have a few different approaches to this:
1) Here's a purely VBA approach. You'll need to download the free command-line version of Putty called plink from here. In this approach you're streaming the data on your command-line and transferring the data into a local text file.
cmd = "C:\MyUtilities\plink.exe -pw PASSWORD USERNAME#IPADDRESS cat /dataops/profits/name.csv"
Set datafile = CreateObject("Scripting.FileSystemObject").CreateTextFile("c:\name.csv", True)
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec(cmd)
Do While oExec.Status = 0
If Not oExec.StdOut.AtEndOfStream Then
datafile.WriteLine oExec.StdOut.Readall
End If
Loop
datafile.Close
2) Another approach would be to run a Windows scheduled task batch file that uses FTP to transfer the file over, as explained here. Personally I think this will be your best bet, because the VBA code can be focused on just loading up the file, rather than connecting to Unix and getting the file too.
3) If you have a Samba share set up on your Unix server for a directory, then you'll be able to see that directory from Windows and can just go there to get the file.
Once you have the file on your Windows computer, you can open Excel and load the file like this:
Set xl = CreateObject("Excel.Application")
xl.Visible = True
Set wb = xl.Workbooks.Open("c:\name.csv")

Access Word 'Save As' dialog box with PowerShell script

I've got a PowerShell script (running on Windows Server 2008 R2 Enterprise) that opens a Word doc in Word 2010, performs a SaveAs, and saves the doc as a PDF. In brief my code looks similar to the below:
$word = new-object -ComObject "word.application"
$word.Visible = $true
$doc = $word.documents.open("path\file.doc")
$doc.SaveAs("path\file.pdf", [ref] 17)
$doc.Close()
ps winword | kill
The above works fine, no problems at all and is converting the documents as expected.
My question is:
If I physically open Word myself and navigate to 'File > Save As' I get various options in the dialog when saving as PDF (eg. page range, optimisation etc)
How can I, if at all, access these options from within the PowerShell script when performing the same action?
Any advice would be appreciated. Maybe it's just not possible.
Thanks in advance
After much investigation I've found that option I needed was ExportAsFixedFormat().
The documentation can be found here:
http://msdn.microsoft.com/en-us/library/bb256835%28v=office.12%29.aspx
And you can see it in action within a PowerShell script here:
http://blog.coolorange.com/2012/04/20/export-word-to-pdf-using-powershell/

Adobe Reader Command Line Reference

Is there any official command line (switches) reference for the different versions of
Adobe (formerly Acrobat) Reader?
I didn't find anything on Adobe Developer Connection.
Especially I want to:
Start Reader and open a file
Open a file at a specific position (page)
Close Reader (or single file)
You can find something about this in the Adobe Developer FAQ. (It's a PDF document rather than a web page, which I guess is unsurprising in this particular case.)
The FAQ notes that the use of the command line switches is unsupported.
To open a file it's:
AcroRd32.exe <filename>
The following switches are available:
/n - Launch a new instance of Reader even if one is already open
/s - Don't show the splash screen
/o - Don't show the open file dialog
/h - Open as a minimized window
/p <filename> - Open and go straight to the print dialog
/t <filename> <printername> <drivername> <portname> - Print the file the specified printer.
I found this:
http://www.robvanderwoude.com/commandlineswitches.php#Acrobat
Open a PDF file with navigation pane active, zoom out to 50%, and search for and highlight the word "batch":
AcroRd32.exe /A "zoom=50&navpanes=1=OpenActions&search=batch" PdfFile
To open a PDF at page 100 the follow works
<path to Adobe Reader> /A "page=100" "<Path To PDF file>"
If you require more than one argument separate them with &
I use the following in a batch file to open the book I'm reading to the page I was up to.
C:\Program Files\Adobe\Reader 10.0\Reader\AcroRd32.exe /A "page=149&pagemode=none" "D:\books\MCTS(70-562) ASP.Net 3.5 Development.pdf"
The best list of command line args for Adobe Reader I have found is here.
http://partners.adobe.com/public/developer/en/acrobat/PDFOpenParameters.pdf
It's for version 7 but all the arguments I tried worked.
As for closing the file, I think you will need to use the SDK, or if you are opening the file from code you could close the file from code once you have finished with it.
Call this after the print job has returned:
oShell.AppActivate "Adobe Reader"
oShell.SendKeys "%FX"
Having /A without additional parameters other than the filename didn't work for me, but the following code worked fine with /n
string sfile = #".\help\delta-pqca-400-100-300-fc4-user-manual.pdf";
Process myProcess = new Process();
myProcess.StartInfo.FileName = "AcroRd32.exe";
myProcess.StartInfo.Arguments = " /n " + "\"" + sfile + "\"";
myProcess.Start();