Adobe "Save as" dialog boxes - MapPoint Excel VBA - vba

I am trying to interact with an external Acrobat dialog window through VBA.
I am using MapPoint to generate maps and then save them as PDF files. Everything is controlled through VBA. I am using the "PrintOut" method of the maps to save them.
objApp.ActiveMap.PrintOut _
Title:=PDFTitle, _
PrintArea:=geoPrintFullPage, _
PrintQuality:=geoPrintQualityNormal, _
PrintOrientation:=geoPrintLandscape
Using this command in this way launches a "Save PDF file as" dialog box. At some point in the past we used to deal with this issue by using the SendKeys function to send {Enter} to the dialog and close it, but this no longer works.
I think the problem is that running this command causes VBA execution to stop until the dialog box is closed. Is there any way I could schedule a Sendkeys function to execute after the dialog opens? Or is there a way to prevent VBA execution from pausing?
Ideally, I would like to avoid having the dialog box in the first place, but that does not appear to be possible with my current setup. Specifying the OutputFileName when running the command does prevent the dialog from appearing, but it causes some kind of problem with the saved file (it can't be opened and appears to be corrupt).
Any suggestions are appreciated!

Can you download primopdf and try the following. PrimoPdf is a free print driver that allows you to save as PDF. http://download.cnet.com/PrimoPDF/3000-18497_4-10264577.html
Option Explicit
Sub PrintToPrimoPDF()
Dim strCurrentPrinter As String
strCurrentPrinter = Application.ActivePrinter ' save the currently active printer
On Error Resume Next ' ignore errors
Application.ActivePrinter = "PrimoPDF on Ne04:" ' change to PrimoPdf
Sheet1.PrintOut ' print the sheet1
Application.ActivePrinter = strCurrentPrinter ' change back to the original printer
On Error Goto 0 ' resume normal error handling
End Sub

Related

Disable Save As but not Save in Word 2010

I am looking to disable Save As in a Word 2010 file but still allow save. In other words I want users to be able to update the existing file but not create copies. I realize that this is impossible to truly do for people who know workarounds but for the general user I have successfully done this in Excel but am pretty new to word VBA.
When I add the following to a brand new document everything works as intended:
Sub FileSaveAs()
MsgBox "Copies of this file cannot be created. Please save changes in the original document." & _
, , "Copy Cannot be Created"
End Sub
My document has other macros for various command buttons but none of them involve saving the document (under original name or save as). There is also a macro running on open but that is 1 line going to a bookmark. When I try to "save as" in this document I get the message box as intended. When I try to "save" though things get strange: I get the save as dialogue (problem 1). Whether I try to save either under same name or other name the dialogue behaves as it normally would except it doesn't save and the dialogue box opens again automatically essentially creating an endless loop until I hit cancel (problem 2). I also intermittently get a "disk is full" warning pop-up after trying to save which I can dismiss but appears a few minutes later as long as he file is open (perhaps related to autosave?)
Since the macro works in the test file I assumed this strange behavior must be something elsewhere in my code but my document with the other macros saves normally as long as I don't include the save as code above so now I'm totally confused. Before I put up the rest of my code which is lengthy and for the reasons stated above I would not think impact things, I figured I'd ask this:
1. Is there any place other than my other command button macros that could be causing this behavior?
2. Is there a better method people recommend to achieve my ultimate goal of disabling save as but not save?
Thanks in advance for any advice you can provide.
The Word application has a DocumentBeforeSave event. To enable application events I suggest to create a class module by the name of ThisApplication and paste the following code into it.
Option Explicit
Private WithEvents App As Application
Private Sub Class_Initialize()
Set App = Word.Application
End Sub
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, _
SaveAsUI As Boolean, _
Cancel As Boolean)
If SaveAsUI Then
MsgBox "Please always use the ""Save"" command" & vbCr & _
"to save this file.", _
vbExclamation, "SaveAs is not allowed"
Cancel = True
End If
End Sub
Add the following code to your ThisDocument module.
Dim WdApp As ThisApplication
Private Sub Document_Open()
Set WdApp = New ThisApplication
End Sub
You may add the Set App = ... line to your existing Document_Open procedure. After the WdApp variable has been initialised all application events will be received by the ThisApplication class where the DocumentBeforeSave event procedure is programmed not to allow SaveAs.
Of course, this is a blanket refusal for all documents. Therefore you may wish to add code to the procedure to limit the restriction to certain documents only. The proc receives the entire document object with all its properties, including Name, Path, FullName and built-in as well as custom properties. You can identify the files you wish to be affected by any of these.
Note that the WdApp variable will be erased in case of a program crash. If this happens the application events will no longer fire. It may be useful to know that application events occur before document events. This is if you wish to use the application's DocumentOpen event as well as or instead of the document's Document_Open event.

vba Excel - How to Check if a SharePoint Website File is Open

I've searched the internet for the answer to this and keep running into issues getting it to work.
I need to check if a file at the below location is open/locked, and then have the code wait 15 seconds and then try again. Right now if someone else has the file open a dialog box opens asking if I want to open read only, which I don't want. I tried using Application.DisplayAlerts = False to get the message not to appear but that didn't seem to work. So I have two main issues:
Checking if the file is open and waiting to try again.
Stop the dialog box from opening asking to open as read only.
Workbooks.Open filename:= _
"https://somecorporatewebsite/sites/TNKYWest/Engr/ASE%20Updates/Shared%20Documents/ASENW Updater.xlsx"
Try something like this:
From MSDN site:
https://msdn.microsoft.com/en-us/library/office/ff193284.aspx
Sub UseCanCheckOut(docCheckOut As String)
Dim docCheckout
set docCheckout="File name to open"
' Determine if workbook can be checked out.
If Workbooks.CanCheckOut(Filename:=docCheckOut) = True Then
Workbooks.CheckOut (Filename:=docCheckOut)
Else
Found at the MSDN Website:
https://msdn.microsoft.com/en-us/library/office/ff822851.aspx
Application.Wait(Now + TimeValue("0:00:10"))
Workbooks.CheckOut (Filename:=docCheckOut) 'Try to check the file out again.
End If
End Sub
This part is covered in the
IF workbooks.cancheckout(filename:=docCheckout)=true then workbooks.checkout ' method in part 1.

CheckIn Word document with VBA, without document closing and reopening

Our company has recently migrated over to Sharepoint and we have been having untold amounts of fun forgetting to Check In and losing all our work.
So I've developed the following code which will be accessed by a button in the Quick access toolbar, cleverly disguised as a Save button.
I developed this by recording the action and tweaked with some help from the web.
Sub ChaveIn()
'
' CheckIn/Save = ChaveIn Macro
' Andrew Nathanson
' 2014/04/07
' Ver 1.0
'
'
If ActiveDocument.CanCheckin Then
TheCurrentfilename = ActiveDocument.FullName
ActiveDocument.CheckInWithVersion SaveChanges:=True, MakePublic:=False, _
Comments:="", VersionType:=wdCheckInMinorVersion
Documents.CheckOut FileName:=TheCurrentfilename
Documents.Open FileName:=TheCurrentfilename
Else
ActiveDocument.Save
End If
End Sub
My question is mostly aesthetic; If you Check In via Word's dialog box the document will pause, upload to the server, and let you carry on where you left off.
When my code does it the document shuts and opens again, losing your location in the document.
Is there a way to do it as Word does it?
Thank you.

Macro to save e-mail as text file, to be used in a rule

My problem is very similar to this thread and this one. I think my issue is to combine these two questions.
I am running:
OS: Windows 7 Enterprise Professional
Outlook 2010
VBA version 7.0
By reading these two questions as well as some other pages from Microsoft and elsewhere, I was able to open the VB editor and paste into it, this simple code:
Sub SaveEmail(msg As Outlook.MailItem)
' save as text
msg.SaveAs "C:\Users\mel\mailsave\email.txt" & Format(Now, "YYYYMMDDHHMMSS"), _
olTXT
End Sub
Is the "format" portion of my msg.SaveAs line, going to save a unique text file for each email matching my rule?
How do I run this macro to test and if successful, how do I run it repeatedly?
I tried going to the run menu and selecting run "sub/user form" item but the next dialog box is asking what to run and does not populate a list of macros available for running. Clicked on "save" icon but nothing changed.
Specifying a method with that signature (Sub method (var As Outlook.MailItem)) allows you to use the method when creating a mailbox rule. As far as I understand your question, you're beyond that point.
Question 1
The format portion of your code is only going to save a unique file at most once per second. You're appending the current date and time to the file. Your main problem, however, is not the timestamp, but the file format. You should apply the timestamp before the file extension, e.g.
msg.SaveAs "C:\Users\mel\mailsave\email" & Format(Now, "YYYYMMDDHHMMSS") & ".txt", olTXT
Question 2
If you add the macro to a rule, it will be run when the rule is matched. The macro can be tested by creating a method that grabs the currently selected mail, e.g.
Sub TestSaveEmail()
Call SaveEmail(ActiveExplorer.Selection(1))
End Sub
This macro can then be run by setting the cursor within the method and pressing F5.
The macro can also be added to the Outlook user interface by customizing the ribbon and adding a macro button. For help on customizing the ribbon, refer to the following article:
Customize the ribbon

Send keypress to error messages on opening Word document

I am having some problems on sending key strokes to an error message while opening a document.
The document I am trying to open in a Word for Dos 5.5 document that is missing the style sheet, you can manually click ignore and the file opens, but I have 1000+ more file to do this with so i want to automate this.
I am opening this document up in either Office XP, 2003 or 2010 but i cant get the keystrokes to affect the messages.
The code I have been attempting is as follows:
Dim objWord As New Word.Application
Dim objDoc As New Word.Document
objDoc = objWord.Documents.Open(TempDir + ("\\" + fileInf.Name))
System.Threading.Thread.Sleep(5000)
SendKeys.Send("{Enter}")
System.Threading.Thread.Sleep(5000)
SendKeys.Send("{&I}")
System.Threading.Thread.Sleep(5000)
objWord.WindowState = Word.WdWindowState.wdWindowStateNormal
objWord.Visible = True
I have used the visible = true just to see what i going on, but the messages just sit there.
I think sending keyPress is not the best solution for your problem, and this is something you should avoid if you can.
You could instead try to hide Word messages. Possible ways:
Try to disable messages using Application.DisplayAlerts property before you open the file. So it would be:
objWord.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone
Important note from MSDN :
If you set this property to wdAlertsNone or wdAlertsMessageBox,
Microsoft Word doesn't set it back to wdAlertsAll when execution
stops. You should write your code in such a way that it always sets
the DisplayAlerts property back to wdAlertsAll when it stops running.
Try to disable messages by setting other parameters of the Documents.Open method. I am thinking to the Format paramater that you could try to set to wdOpenFormatAllWord to see if it disables messages.
wdOpenFormatAllWord: A Microsoft Word format that is backward compatible with earlier versions of Microsoft Word.
When you write SendKeys.Send("{Enter}"), I guess you have System.Windows.Forms namespace imported so it is equal to System.Windows.Forms.SendKeys.Send("{Enter}").
In my first answer I have suggested, If you really need to use the SendKeys method, to use the one from Excel.Application. (because I was needing coffee..., I was totally focused on Excel although the question was about Word).
Unfortunately, the method is present In Excel but there is NO SendKeys Method in Word.Application Object.