Hide commandbutton when coverting Word Doc to PDF - vba

When converting to pdf using a commandbutton that creates an Outlook email, coverts docfile to pdf, and attached file - the pdf file still displays the commandbuttons on the form.
Is there a way to hide the commandbuttons or make them invisible before converting from Word Doc to pdf format.
If VName.Value = "" Then
Doc.SaveAs ("Quotation_Blank 2016") Else
FileName = "QFORM" & "_" & JNumber.Value & "_" & VName.Value
Doc.ExportAsFixedFormat OutputFileName:=THE_PATH & FileName, _
ExportFormat:=wdExportFormatPDF
End If

Depending on the button type there might be a Print Object property for the button object that might show/hide the button when being exported. Otherwise you might have to set the Visible property on the button to 'False' before exporting, then turn Visible back to 'True' after. Might depend on if you're using 'ActiveX' controls or 'Form' controls, so you might have to play around with those settings a bit depending on your setup.

Related

MS Word filename from bookmarks with VBA

Assuming I have:
a word template including macro: custom_template.dotm;
two bookmarks in this template: 'first_name" and "last_name".
I would like that on "Save" event, in the dialog box, the application proposes to user, instead of "document1", and only if relative bookmarks exist, the filename "Document of first_name second_name.docx".
Can anybody explain me how to achieve this with VBA?
Thanks.
=== UPDATE ===
Now I've this code, working well when I execute it.
I would like it runs automatically when user clicks on "save document".
Sub Demo()
Dim sFlNm As String
With ActiveDocument
sFlNm = "Document of " & .Bookmarks("first_name").Range.Text & " " & .Bookmarks("last_name").Range.Text
End With
With Dialogs(wdDialogFileSaveAs)
.Name = sFlNm
.Show
End With
End Sub
For a macro to run at the client's end, you would have to send a macro-enabled template or document. Many people who are running anti-virus software will get a warning of a possible Word virus. Then the user will have to manually enable the macros. Are they going to bother?
Making it run automatically with a Save command may have unintended consequences. You'll have to check whether the bookmarks have actually been filled, and using the Save command while you're revising the document can save it with a new file name. But you asked, so here's how: rename the macro as FileSave. Then when you choose Ctrl + S or File>Save in Word, the dialog will automatically pop up:
Sub FileSave()
Dim sFlNm As String
With ActiveDocument
sFlNm = "Document of " & .Bookmarks("first_name").Range.Text & " " & .Bookmarks("last_name").Range.Text
End With
With Dialogs(wdDialogFileSaveAs)
.Name = sFlNm
.Show
End With
End Sub
In Word, to create a macro that runs automatically when you choose a Word command, follow these steps:
Choose Developer>Macros.
Change the Macros in dropdown to Word commands.
Choose the command name you want to re-purpose.
Change the Macros in dropdown back to the macro-enabled document or template that you're developing.
Click on the Create button. A new macro is created in the VBE with the correct command name. Fill in the macro with whatever you want the macro to do.

How can I set focus through Word vba range.find

I am writing a macro in Word, for replacing some text with a mapped definition.
I want to give the user the option to give confirmation for each replace, just like the default Find & Replace.
How can I set focus on the text that is found?
How can the particular section be scrolled into view?
I need to use custom VBA code(rather than default Find & Replace) as I have to process the document after reading in the mappings.
I am already able to replace the text, and also show the alerts to the user.
However, I want to put the focus on the text while showing the alert.
Current code:
Do While myRange.Find.Execute( _
FindText:=dict.Items()(i) & " (" & Word & ")", _
MatchCase:=False, _
MatchWholeWord:=True _
)
myRange.Select
If MsgBox("Replace '" & myRange.Find.Text & "' with '" & Word & "'?", vbYesNo) = vbYes Then
myRange.Text = Word
End If
myRange.Start = myRange.Start + Len(myRange.Find.Text)
myRange.End = cached
Loop
p.s. I have a custom form/dialog open, from which the macro is being run;
so the text is behind the dialog.
EDIT: Based on Jay's response, I again checked the behavior of the Find & Replace dialog. The dialog gets moved based on the location of the text. Can I achieve the same when I have a form and a confirmation dialog over the text?
The statement myRange.Select that's already in your code puts the Selection on the found text, and scrolls it into view if it's offscreen. That won't help, though, if the Selection is hidden behind a custom form (userform?) or the message box. You may be able to move the form out of the way, if you can figure out where the Selection is with respect to the screen coordinates.

Powerpoint 2013 Compare

How can one find a only content (i.e. text) differences between two powerpoint files?
I use PPT 2013. It has a compare tool, but that also finds all text box movements, animation changes, formatting differences etc., which makes it difficult to see whether there are any text changes.
I need to compare "text only" and display any change in text
Context
My client gives me a PPT file. I will format it (color, font, animation etc.), but I'm not supposed to change any text content. If I delete or insert any content by mistake, I'd like to be able to detect that, so I can revert it.
In PowerPoint 2013 you can export the PowerPoint file content to word or a pdf file which you can then use to compare only the text changes:
choose File> Export, Create Handouts, then click
the Create Handouts button.
In the dialog box that opens, choose the Outline Only option and
click OK. Word opens with your text.
Update:
You can run this VBS script to extract the text to a text file then you can compare the two files.
It comes from the tool Beyond Compare 4 which can be downloaded as a trial edition. Download the Additional File Formats for PowerPoint files as well if you just want to compare the text changes from the tool.
' PPT_to_TXT.vbs
'
' Extracts plain text from a PowerPoint document. Requires Microsoft PowerPoint.
' Usage:
' WScript PPT_to_TXT.vbs <input file> <output file>
Option Explicit
' MsoAutomationSecurity
Const msoAutomationSecurityForceDisable = 3
' OpenTextFile iomode
Const ForAppending = 8
Dim App, AutoSec, Doc, FileSys
Set FileSys = CreateObject("Scripting.FileSystemObject")
If FileSys.FileExists(WScript.Arguments(1)) Then
FileSys.DeleteFile WScript.Arguments(1)
End If
Set App = CreateObject("Powerpoint.Application")
On Error Resume Next
App.DisplayAlerts = False
AutoSec = App.AutomationSecurity
App.AutomationSecurity = msoAutomationSecurityForceDisable
Err.Clear
Dim Comment, Shape, Slide, TgtFile
Set Doc = App.Presentations.Open(WScript.Arguments(0), True, , False)
If Err = 0 Then
Set TgtFile = FileSys.OpenTextFile(WScript.Arguments(1), ForAppending, True)
For Each Slide In Doc.Slides
For Each Shape In Slide.Shapes
If Shape.HasTextFrame Then
If Shape.TextFrame.HasText Then
TgtFile.WriteLine Shape.TextFrame.TextRange.Text
End If
End If
Next
For Each Shape In Slide.NotesPage.Shapes
If Shape.HasTextFrame Then
If Shape.TextFrame.HasText Then
TgtFile.WriteLine Shape.TextFrame.TextRange.Text
End If
End If
Next
For Each Comment In Slide.Comments
TgtFile.WriteLine Comment.Author & vbTAB & Comment.DateTime & vbTAB & Comment.Text
Next
Next
TgtFile.Close
Doc.Close
End If
App.AutomationSecurity = AutoSec
App.Quit

Delete Selected Text in a Text Box

Hey fellow Stackoverflow users,
i'm trying to create a text editor with HTML tag functions (for export) and can't get a solution working. Therefor i created a textbox where the user should be able to insert the text. For this insert function i need the in the title described function to delete the selected text (only the selected text, the text around it stays) inside the textbox. Even SendKeys won't function right.
Please let me know if anyone got an idea, thanks upfront!
EDIT: Here's the corrected code for the bold button with maintextbox as textbox for the user's text:
Private Sub BoldButton_Click()
If maintextbox.SelLength = 0 Then
MsgBox ("Please highlight the text you want to edit!")
Else
SelectionText = maintextbox.SelText MsgBox ("SelText: " & SelectionText)
maintextbox.SelText = "<b>" & _
SelectionText & _
"</b>"
End If
End Sub
Simply:
TextBox.SelText = ""
The .Sel* methods all relate to the text currently selected within the control. SelText = "" replaces the current selection with nothing, thereby deleting it whilst preserving the surrounding unselected text.

How can I use VBA to lock/unlock all fields in a Microsoft Word 2010 document?

The problem I have got is that my corporate template set uses a SaveDate field in the footer of every word document - which is used to detail when the document was saved, which ties in with our custom document management system.
Subsequently, when users want to make a PDF of an old document, using the Save As PDF function of Office 2010, the Save Date is updated - creating a PDF of the old document, but with today's date. This is wrong. We are just trying to create a true PDF version of whatever the original document has in it.
To get around this, I am writing a macro solution which locks the fields, exports the document as a PDF and then unlocks the fields again.
I have come up against an issue where I can identify and lock all fields in the headers/footers (which is actually what I'm trying to do) but to make it more robust, need to find out a way to lock ALL FIELDS in ALL SECTIONS.
Showing you my code below, how can I identify all fields in all sections? Will this have to be done using the Index facility?
Sub CPE_CustomPDFExport()
'20-02-2013
'The function of this script is to export a PDF of the active document WITHOUT updating the fields.
'This is to create a PDF of the document as it appears - to get around Microsoft Word 2010's native behaviour.
'Route errors to the correct label
'On Error GoTo errHandler
'This sub does the following:
' -1- Locks all fields in the specified ranges of the document.
' -2- Exports the document as a PDF with various arguments.
' -3- Unlocks all fields in the specified ranges again.
' -4- Opens up the PDF file to show the user that the PDF has been generated.
'Lock document fields
Call CPE_LockFields
'Export as PDF and open afterwards
Call CPE_ExportAsPDF
'Unlock document fields
Call CPE_UnlockFields
'errHandler:
' MsgBox "Error" & Str(Err) & ": " &
End Sub
Sub CPE_LockFields()
'Update MS Word status bar
Application.StatusBar = "Saving document as PDF. Please wait..."
'Update MS Word status bar
Application.StatusBar = "Locking fields in all section of the active document..."
'Declare a variable we can use to iterate through sections of the active document
Dim docSec As section
'Loop through all document sections and lock fields in the specified ranges
For Each docSec In ActiveDocument.Sections
docSec.Footers(wdHeaderFooterFirstPage).Range.fields.Locked = True
docSec.Footers(wdHeaderFooterPrimary).Range.fields.Locked = True
docSec.Footers(wdHeaderFooterEvenPages).Range.fields.Locked = True
Next
End Sub
Sub CPE_UnlockFields()
'Update MS Word status bar
Application.StatusBar = "PDF saved to DocMan Temp. Now unlocking fields in active document. Please wait..."
'Declare a variable we can use to iterate through sections of the active document
Dim docSec As section
'Loop through all document sections and unlock fields in the specified ranges
For Each docSec In ActiveDocument.Sections
docSec.Footers(wdHeaderFooterFirstPage).Range.fields.Locked = False
docSec.Footers(wdHeaderFooterPrimary).Range.fields.Locked = False
docSec.Footers(wdHeaderFooterEvenPages).Range.fields.Locked = False
Next
End Sub
Sub CPE_ExportAsPDF()
'Update MS Word status bar
Application.StatusBar = "Saving document as PDF. Please wait..."
'Chop up the filename so that we can remove the file extension (identified by everything right of the first dot)
Dim adFilename As String
adFilename = Left(ActiveDocument.FullName, (InStrRev(ActiveDocument.FullName, ".", -1, vbTextCompare) - 1)) & ".pdf"
'Export to PDF with various arguments (here we specify file name, opening after export and exporting with bookmarks)
With ActiveDocument
.ExportAsFixedFormat outPutFileName:=adFilename, _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, _
OptimizeFor:=wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateWordBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
End With
'Update MS Word status bar
Application.StatusBar = "PDF saved to DocMan Temp."
End Sub
Try something like the following to get to all fields in the document, header, footer, background and main text:
Sub LockAllFieldsInDocument(poDoc As Document, Optional pbLock As Boolean = True)
Dim oRange As Range
If Not poDoc Is Nothing Then
For Each oRange In poDoc.StoryRanges
oRange.Fields.Locked = pbLock
Next
End If
Set oRange = Nothing
End Sub
Here is another way to do it. It'll select the entire document and then lock all fields, before deselecting everything.
Sub SelectUnlink()
ActiveDocument.Range(0, 0).Select
Selection.WholeStory
Selection.Range.Fields.Unlink
Selection.End = Selection.Start
End Sub