I have a button on a form in my database that i would like to open a user guide on click. The user guide I have put to gether is in visio but i can't seem to find a way to open it using the macro builder. Is this something i would need to do using VBA? If so any suggestions on how the code should look?
I think something like the following may work, I have manipulated this to fit visio though, so hopefully it works.
Dim FName As String
Dim VisioApp As Object
On Error Resume Next
Set VisioApp = GetObject(, "Visio.Application")
If VisioApp Is Nothing Then
Set VisioApp = CreateObject("Visio.Application")
If VisioApp Is Nothing Then
MsgBox "Can't connect to Visio"
Exit Sub
End If
End If
On Error GoTo 0
FName = "C:\Path\FileName.vsd"
VisioApp.documents.Open FName '
VisioApp.Visible = True
You may need to go in to the VB editor, click Tools > References and then mark Microsoft Visio library as checked.
Related
Windows 10
Microsoft 365
Outlook v.2205
I am writing a macro that prints the selected email AND attachments to PDF. I need this to be fully automatic, without any user intervention once the macro starts. The problem I have, is that when the mailItem.PrintOut command is used, a print dialogue window appears and freezes the macro until the user advances the window manually.
I am able to cobble together the body of the email using the Word object library where I can easily print, but for email attachments (such as PDFs) I am at a loss.
I have scoured the web looking for a solution to this. I have found nothing so far that can be done in pure native VBA.
Image of the printer dialogue that I would like to bypass:
I previously asked here whether it is possible to manipulate the printer dialogue window using API/VBA code, but later realized that once the printer dialogue appears, VBA stops working so I must find a way to bypass the dialogue window altogether.
It seems unlikely to me that there does not exist a way to do this.
Code posted below (simplified for reference):
Private Sub printEmail()
Dim mySelection As Outlook.Selection
Dim myEmail As MailItem
Set mySelection = Application.ActiveExplorer.Selection
' If item = mailitem, print
If mySelection.Item(1).Class = 43 Then
Set myEmail = mySelection.Item(1)
myEmail.PrintOut ' <===###this command causes a dialogue to appear###
' Else, exit sub
Else
MsgBox "Select a mail item"
Exit Sub
End If
End Sub
You need to save attached files (for example, PDF) on the disk and then use a shell command to print the saved file. Or if you have got Acrobat Reader installed on the system you could use something like that:
Dim oShell As Object, oExec As Object
Set oShell = CreateObject("Wscript.Shell")
Set oExec = oShell.exec("""" & "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" _
& """/p /h """ & SomePath & "\" & "invoice.pdf" & """")
DoEvents
oExec.Terminate
Set oExec = Nothing
Set oShell = Nothing
I have made a form in access and I'm trying to code a "merge to word" button but my code has a problem and I dont know how to fix it.
Here's the code:
Private Sub Command102_Click()
Dim LWordDoc As String
Dim oApp As Object
'Path to the word document
LWordDoc = "C:\school\information tech\document.docx"
If Dir(LWordDoc) = "" Then
MsgBox "Document not found."
Else
'Create an instance of MS Word
Set oApp = CreateObject(Class:="Word.Application")
oApp.Visible = True
'Open the Document
oApp.Documents.Open FileName:=Document.docx
End If
End Sub
If someone could help that would be great. Ill attach screenshots of the error I get when I click the button and the code as the debugger is pointing to the problem. (pointing to the start of the "oApp.Documents.Open FileName:=Document.docx" line)
Thanks heaps
Since you create a variable to hold the filename, try using that:
oApp.Documents.Open FileName:=LWordDoc
Try to open embedded object in excel file with caption,able to open file with name: can any one help how to open with caption or dynamically
Worksheets(SheetName).Activate
Set o = Worksheets(SheetName).OLEObjects("object 3")
o.Verb xlVerbOpen
MsgBox "Attachmene open"
Note: object will add continuously in excel file, how to find object dynamically to open with caption?
The embedded object in Excel is a Shape. Add two embeded workbooks in your ActiveSheet and try this code:
Public Sub TestMe()
Dim obj As Object
For Each obj In ActiveSheet.Shapes
Debug.Print obj.Application.Caption
Next obj
End Sub
Then try to change the code, with a simple condition, opening the obj, if the caption is the expected one:
If obj.Application.Caption = "someCaption" Then OpenTheWorkbook(obj)
At the end write some check to make sure that it skips some possible errors.
I open an embedded WORD document through Excel using the following verb command.
Set o = .OLEObjects("Object 1")
o.Verb xlVerbOpen
"Object 1" is the default name of the embedded object so that would be changed as needed.
I am trying to copy the values of one column in a sheet to a text file. The code I currently have causes runtime error 434.
Sheets("Output to fcf.1").Columns("A").SaveToText "P:\4_Calcs\02. Flag Mapping\test_.txt"
If I try and save the whole sheet
Sheets("Output to fcf.2").SaveToText "P:\Clear Project Drive\CLE10276 AWS SMP Model Assessmnts\4_Calcs\02. Flag Mapping\test2_.txt"
I get the entire sheet converted into text rather than just the text in the sheet. Is there a simple way to do this?
Thanks in advance!
Not sure which Excel version you have but I don't see a method for SaveToText.
But this procedure should work, or at least get you started...
Sub SaveColumn(sheetName As String, columnName As String, fileName As String)
Dim cell
Dim fso
Dim file
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.CreateTextFile(fileName, True)
For Each cell In Sheets(sheetName).Columns(columnName).Cells
If cell.Value <> "" Then
file.WriteLine cell.Value
End If
Next
file.Close
Set file = Nothing
Set fso = Nothing
End Sub
To call it...
SaveColumn "Output to fcf.1", "A", "P:\4_Calcs\02. Flag Mapping\test_.txt"
This is designed to be used as a macro.
Step by step guide:
1) From excel, hit Alt+F11 on your keyboard.
2) From the menu bar, click Insert, then Module
3) Copy and paste the code provided below into the new module that opens.
NOTE: DocPath = "C:\docs\data.txt" should be wherever you want the output file saved, including the file's actual name. Remember, the folder you want the output file to be located in should ALREADY exist. This does not create the folder if it can't be found.
4) From the menu bar, click Tools, then References. Make sure both "Microsoft Office 14.0 Object Library" as well as "Microsoft Word 14.0 Object Library" are checked, and hit okay (See screenshot for details)
5) Save the document as an .xlsm file (This file type supports Macros)
6) Close the VBA editor. Back in Excel, on the ribbon click View and then Macros. Your new macro should be in the list as ExportToTXT
7) Select it and hit run.
Sub ExportToTXT()
Dim DocPath As String
Dim MsgBoxCompleted
Columns("A").Select
Dim AppWord As Word.Application
Set AppWord = CreateObject("Word.Application")
AppWord.Visible = False
Selection.Copy
DocPath = "C:\docs\data.txt"
'Create and save txt file
AppWord.Documents.Add
AppWord.Selection.Paste
AppWord.ActiveDocument.SaveAs2 Filename:=DocPath, FileFormat:=wdFormatText
Application.CutCopyMode = False
AppWord.Quit (wdDoNotSaveChanges)
Set AppWord = Nothing
MsgBoxCompleted = MsgBox("Process complete.", vbOKOnly, "Process complete")
End Sub
Good luck, and if you have any questions, don't hesitate to ask.
NOTE: These directions might seem overly simplified for your skill level, but I wrote the answer like this to potentially help others in the future.
EDIT
Change
DocPath = "C:\docs\data.txt"
to
DocPath = "C:\docs\data.fcf"
And change
AppWord.ActiveDocument.SaveAs2 Filename:=DocPath, FileFormat:=wdFormatText
to
AppWord.ActiveDocument.SaveAs2 Filename:=DocPath
The output file will be .fcf format. Whether or not it will open properly is something I'm not sure of. You'd have to test in the program you're using.
I can't find the syntax anywhere for setting a PowerPoint theme as the default theme using Excel VBA or PowerPoint (2010) VBA.
To clarify what I'm trying to do, the steps to do this mannually are: go to the design tab in PowerPoint, right click on the desired theme and select the "Set as Default" option. This process sets the default theme and slide master default.
The syntax below will set the theme (using Excel VBA) once but will not set the theme as the default. How do I set the theme as the new default?
Set PPApp = CreateObject("Powerpoint.Application")
PPApp.Visible=True
Set PPPres = PPApp.Presentations.Add
PPPres.ApplyTemplate ("Location")
Much thanks to David for the ideas below but, unfortunately they do not work for my intended application. I am trying to create a macro that will set the default theme on open so that I can distribute that file to my org and control the slide template we are all working off of. This macro found HERE is very close but I cannot find syntax that will lock in the applied template. Thoughts?
Sub AutoOpen()
Dim ThemePath As String
Dim ThemeFile As String
Dim FullPath As String
ActivePresentation.PageSetup.SlideSize = ppSlideSizeOnScreen16x9
ThemeFile = "TestTemplate.potx"
ThemePath = "File Path"
FullPath = ThemePath + ThemeFile
On Error Resume Next
ActivePresentation.ApplyTemplate (FullPath)
If Err <> 0 Then
MsgBox (ThemeFile & " was not found in " & ThemePath)
End If
End Sub
I know this is 4 years too late, but I don't think that this question was ever adequately answered. In your code above you should set the following string variables to:
ThemeFile = "blank.potx" '(it has to be "blank" or else it won't work)
ThemePath = "C:\Users\" vba.environ("username") & "\AppData\Roaming\Microsoft\Templates"
http://youpresent.co.uk/set-the-default-template-when-powerpoint-starts/