I am using this code to get and minimize all open Word documents.
Word.Application wordApp = new Word.Application();
Word.Documents docs = wordApp.Documents;
wordApp.ScreenUpdating = true;
wordApp.WindowState = Word.WdWindowState.wdWindowStateMinimize;
But documents list is empty despite the fact that I have open Word 2010 documents in Windows 7. Also minimization is not working.
How can I get and minimize all open Word documents?
new Word.Application() will always create a new instance of Word.
to connect to an existing instance, you can use
Word.Application wordApp = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
(this is similar to the VB/VBA "GetObject" function).
Then you should be able to access your documents.
Related
I am new to the VBA coding. I am currently making a ms access db for printing official letters using mail merge. I have already make that happen. Now I want to make a button that saves the current record to db and simultaneously print to word file using the mail merge option.
It will be really helpful for me if you solve this problem.
Thanks in Advance.
I have a word document that uses mail merge feature and gets its information from the access db. When I use this code it does not open the word document with the current information. It opens the word document with the last saved information.
If I open the word document on its own, from the task bar, it asks if I want to run the SQL and I click yes and everything operates normally. I want to click a button from within access to accomplish this same task to open the contract.
here is the code if it can help:-
Private Sub Command205_Click()
Dim LWordDoc As String
Dim oApp As Object
'Path to the word document
LWordDoc = "C:\Users\.....k Up\01- Proposal\contract.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:=LWordDoc
End If
End Sub
Microsoft, in their infinite wisdom, decided that the default file format for Office 2010 applications should be the format that was 13 years old (Office 97-2002) at the time of release.
The newer formats (2007 and newer) save the data in compressed XML files which are much smaller, and also allow for many additional features. Our corporate IT department hasn't or can't set a group policy to force users to default to saving in the new format, so I'm writing a macro to adjust the settings for everyone in our department.
I can do this in Excel and Word very simply by executing the following VBA code (I'm running it from an Excel workbook):
Public Sub SetExcelSave()
Dim myExcel As Excel.Application
Set myExcel = New Excel.Application
Excel.DefaultSaveFormat = xlOpenXMLWorkbook
Excel.Quit
Set Excel = Nothing
End Sub
Public Sub SetWordSave()
Dim myWord As Word.Application
Set myWord = New Word.Application
Word.DefaultSaveFormat = wdFormatDocumentDefault
Word.Quit
Set Word = Nothing
End Sub
However, I haven't been able to find the appropriate setting to adjust in PowerPoint. Does anyone know where that property is or what it's called?
This code will not compile cleanly, giving an error on the PPT.DefaultSaveFormat line:
Public Sub SetPowerPointSave()
Dim PPT As PowerPoint.Application
Set PPT = New PowerPoint.Application
PPT.DefaultSaveFormat = ppSaveAsOpenXMLPresentation
PPT.Quit
Set PPT = Nothing
End Sub
I've rummaged around in the Office Application Object documentation for PowerPoint, but I'm just not finding what I'm after. It's highly likely that I just don't know what I'm looking for and have simply overlooked it.
Does anyone know what property I'm supposed to set to be able to programmatically change this?
The default save format for PPT 2007 and later is the new XML format (PPTX rather than PPT and so on). If the user (or IT staff via policies) have overridden this in the File | Save | Save files in this format: then the default becomes whatever they've selected, for whatever reason.
App-wide defaults like this typically aren't exposed via the object model; they're stored in the registry. In this case, in
HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\PowerPoint\Options
DefaultFormat DWORD=27 for PPTX
Substitute the correct version for 14.0 above; 12.0 for PPT 2007, 14.0 for 2010, and so on (no 13.0).
If you can write the value you want to the registry when PPT isn't running, you can reset the defaults. If you write to the reg while PPT's running, it won't affect the current instance of PPT, and your changes will be overwritten when PPT quits.
I have a program that requires the use of a file with the extension .dotx. Right now I have it as a resource in the solution. With the following code, instead directly using "C:\letterhead.dotx" I want to use the file I have as an 'Embedded Resource'. I tried the following.
Dim oWord As Word.Application
Dim oDoc As Word.Document
oWord = CreateObject("Word.Application")
oWord.Visible = False
'Works:
oDoc = oWord.Documents.Add("C:\Letterhead.dotx")
'Does not work:
oDoc = oWord.Documents.Add(My.Resources.Letterhead)
'Does not work:
oDoc = oWord.Documents.Add(My.Resources.ResourceManager.BaseName & "\Letterhead.dotx")
I am aware that all of those are returning the wrong values and not what I want. I just don't want to force anyone, using this, to put the Letterhead file in a specific place on their computer before running it. I'm sure there's a simple solution, I just don't know it.
Solution:
For all of those (just me) that are also having this problem, all you have to do is the following to write the embedded file to C:\ -
Try
File.WriteAllBytes("C:\Letterhead.dotx", My.Resources.Letterhead)
Catch ex As Exception
MessageBox.Show("Failed to write to C:\", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
You can absolutely store a Word document in a .NET assembly in an embedded resource, and it's fairly trivial to extract it. However, if you want to open a Word document, the steps would be:
Extract the embedded resource to a bytestream
Write the bytestream to a disk (temp folder, perhaps?) as a "doc" or "docx" file
Open that file from disk with Word
...which is quite a bit more work than just including a Word document with the software. Unless you have a very compelling reason to do this with an embedded resource, I would just include the Word document with the application.
If you want some more details, check out Aseem Gautam's answer to this question.
Hi iam creating a Word document like:
Dim oApp As Word.Application
Dim oDoc As Word.Document
oApp = CreateObject("Word.Application")
oDoc = oApp.Documents.Add
Dim rng As Word.Range = oDoc.Range(0, 0)
rng.Font.Name = "Verdana"
rng.Font.Size = 16
.......
...............
oApp.Visible = True
oDoc = Nothing
oApp = Nothing
This works perfect on my local machine. But if i put my Code on Webserver it of course does not work.
My Question is: What i have to do, that the Word document gets created on the server and offer the created document to the client as Download.
Can someone help me with that? Thanks!!
I think they way you are currently doing this requires Microsoft Word (Or at least the Interop components) to be present on the server to allow this.
Perhaps a better approach would be to create a PDF on the fly and offer that to the client
alternatively perhaps this approach would work or this?
EDIT:
Also please see how-to-generate-word-documentdoc-docx-in-asp-net
EDIT:
This code snippet will allow you to stream the file down to the client
using (MemoryStream mem = new MemoryStream())
{
// Create Document
using (<declare document>))
{
//create your document here
// Stream it down to the browser
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
Response.AppendHeader("Content-Disposition", "attachment;filename=HelloWorld.docx");
mem.Position = 0;
mem.CopyTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
Just saw this...
I have managed to get Word running on a server (Apache). You must run Apache under a user account that can run Word. Also if you use Office 32-bit you must run Apache 32-bit. Only one instance of Word runs at one time. However, Word can have multiple documents open at the same time. I developed an addin that I put in Word's startup directory so it is available for each document. In the AutoExec macro I trigger the code that reads a job queue and generates the document. All this is run from the web using AJAX and PHP.
the following two lines
Dim curTasks As Tasks
Set curTasks = Application.Tasks
get the list of all current tasks and work like charm in vba-word but not in vba-excel.
is there a way to port it into vba-excel?
As I said in comments, the Excel object in VBA doesn't have the concept of tasks. You can do the below though in an Excel Module (although I'm still not sure why you would do it):
Dim curTasks As Tasks
Dim wrd As Word.Application
Set wrd = CreateObject("Word.Application")
Set curTasks = wrd.Tasks
NOTE: you have to add a reference to Microsoft Word Object Library to get this to work