manipulate a string for opening word document - c#

Based on Opening Word document within C# that has spaces in the path. I'd like to ask how to do the same but when the path is acquired from list box. I know that if I use #"path" it will work, but how do I do the same to listBox.SelectedItem.ToString() when opening
var _p = new Process();
_p.StartInfo.FileName = "Word.exe"
_p.StartInfo.Arguments = lbFiles.SelectedItem.ToString();
Let's say I want to open "C:\new word document.docx". Word gives an error can't find path "C:\new.doc" any ideas how to do it.

Try surrounding the file name in quotes as well:
_p.StartInfo.Arguments = string.Format("\"{0}\"", lbFiles.SelectedItem);
Also, Word.exe doesn't work on my system. You may need to change that to:
_p.StartInfo.FileName = "WinWord.exe"

Related

Replace StartInfo.Filename user

I've been googleing for hours without any results.
I'm trying to open up a program that is located in "C:\Users\myUsername\" file, but I cannot find any way to replace MY username to other application-users.
firstProc.StartInfo.FileName = #"C:\Users\Username?\AppData\Roaming\GameWool\Launcher.exe";
firstProc.StartInfo.UseShellExecute = false;
firstProc.StartInfo.RedirectStandardInput = true;
So, I'm basically trying to replace MY \Users\USERNAME. So, if someone is using the application, he's supposed to open the same "game" using their OWN \Users\Username.
I hope you get my point.
You can use System.Environment.UserName, this will retrieve the current username
For some reasons, the path of the user's personnal folders may differs. You can instead use
var UserFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
But since you want to use AppData\Roaming\, this will directly lead you to that folder :
// C:\Users\username\AppData\Roaming
var UserRoamingFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
In your case, for the last example, the path will looks like this :
firstProc.StartInfo.FileName = $#"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\GameWool\Launcher.exe"

Opening pdf files using Process.Start

I am trying to open PDF files in Adobe reader using C#'s Process.Start().
When I provide a path without white spaces it works fine, but paths and pdf files containing white spaces don't open.
This is my code:
Button btn = (Button)sender;
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "AcroRd32";
string s = btn.Tag.ToString();
//btn.Tag Contains the full file path
info.Arguments = s;
Process.Start(info);
If it is C:\\Users\\Manish\\Documents\\ms_Essential_.NET_4.5.pdf it works fine but if it is F:\\Tutorials\\C#\\Foundational\\Microsoft Visual C# 2012 Step By Step V413HAV.pdf Adobe Reader gives an error saying there was an error in opening the document file can't be found.
I have read through many questions related to this topic in SO but it won't work. As I can't figure out how to apply # prefix in my string s.
Any ideas how to fix this?
Just a little trick there is a default PDF reader set on the client: just use the file name as FileName if the process. Usually you don't care which program to use, so then this solution just works:
Process.Start(pdfFileName);
This doesn't need special quoting too, so it instantly fixes your problem.
Try to wrap the arguments around quotes:
info.Arguments = "\"" + s + "\"";
Using the character # before the string value should work:
var path = #"F:\Tutorials\C#\Foundational\Microsoft Visual C# 2012 Step By Step V413HAV.pdf";
You should enquote the path provided in the argument list. This will cause it to view the path as a single argument instead of multiple space separated arguments:
info.Arguments = "\"" + s + "\"";

How to change the default program to open any Word documents, using C#?

In my WPF application, I want to open Word documents in Word 2007 or above, whether or not the default program for opening Word documents is Word 2007. Even if the default program to open Word documents is Open Office, I want to open them in Word 2007+.
How can I do this?
This doesn't really have anything to do with WPF.
You'll need to now where Word is installed or add the the folder in which it is located to the Path environment variable.
Assuming your file name variable is called fileName and the full path of winword.exe is stored in wordPath (or winword.exe is in the Path), you would need to do something like this -
ProcessStartInfo startInfo = new ProcessStartInfo
{
CreateNoWindow = false,
Arguments = fileName,
FileName = wordPath
};
Process wordProcess = Process.Start(startInfo);
Note 1 - your fileName is passed directly to Word. If the path contains white spaces you'll have to wrap it in "". Something like
fileName = String.Format("{0}{1}{2}",
fileName.StartsWith("\"") ? "" : "\"",
fileName,
fileName.EndsWith("\"") ? "" : "\"");
Note 2 - Word has other command line arguments for different purposes, for other uses see here http://support.microsoft.com/kb/210565.

Opening MS Word File

I am using document.Active() method which is a part of Microsoft.office.interop.word namespace. I want to open the file i.e. I want to see the file opened in the Word application. I have set thetrackRevisions property true and rest of all the things.
I just want the file to open NOT IN SAVE-AS MODE. Just open so that when I open up a document from DB or from my PC drives I just want it to open.
Here is the code that I am executing:
Word.Document tempDoc = app.Documents.Open("E:\\xyz.docx");
// Activate the document so it shows up in front
tempDoc.Activate();
tempDoc.TrackRevisions = true;
foreach (Revision rev in tempDoc.Revisions)
{
string editedBy = rev.Author;
//string what = rev.Cells;
}
tempDoc.Close(ref Nothing, ref format, ref Nothing);
Any suggestions that come to your mind?
You just want word to open?
Have you tried
Process.Start('path to word plus filename');

Passing a C# string to VBscript

I am using a windows form and am trying to pass a string to a vbscript. The program is asking the user to select a folder, I am trying to take the folder selection and pass it through to the vbscript.
C# Code:
String SelectedFolder = #"C:\Users";
folderBrowserDialog1.SelectedPath = SelectedFolder;
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
//Set selectedFolder equal to the folder that was choosen
SelectedFolder = folderBrowserDialog1.SelectedPath;
//Call VBScript
System.Diagnostics.Process.Start(".vbsPath");
VBScript:
TargetFolder = Request.QueryString("SelectedFolder")
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(TargetFolder)
Set colItems = objFolder.Items
For Each objItem in colItems
objItem.InvokeVerbEx("Print")
Next
Any help would be greatly appreciated. Thank you
C# side
Use the Process.Start overload that accepts command-line parameters:
System.Diagnostics.Process.Start("C:\path\to\my.vbs", selectedFolder);
If the selectedFolder can contain spaces (which is likely to happen), you should enclose the argument in quotes.
System.Diagnostics.Process.Start("C:\path\to\my.vbs",
"\"" + selectedFolder + "\"");
In fact, if the path can contain quotes and/or trailing backslashes, escaping gets a lot more complicated, see these questions (and others) for details: Escape command line arguments in c#, Passing command-line arguments in C#.
VBScript side
In your VBScript, read the first command line parameter:
targetFolder = WScript.Arguments.Item(0)
You could write the string to a file which both sets of code can access, or a database. That way it won't matter which programming language you are using at all e.g. could be C# to PHP.
Write file:
string content = "folder=" + folder;
System.IO.StreamWriter file = new System.IO.StreamWriter(#"c:\config.txt");
file.WriteLine(content);
file.Close();
Read file:
System.IO.StreamReader file = new System.IO.StreamReader(#"c:\config.txt");
string content = file.ReadToEnd();
// extract value of folder setting here
file.Close();
(Of course the reading would need to be in VB, but would be very similar. Note: code based on: MSDN Example)
Going back and forth from VBScript to C# is adding a lot of complexity. If possible, it would really be easier to choose one or the other.
You can do anything in C# that you can in VBScript. (How you do it is probably different, but you can do all the same tasks - file access, database access, etc.) If it's feasible, you may be better off just working in C#.
I'm doing a bit of a guess based on the context of the question here, but I'm trying to answer in my own head why you'd want to do this, and the only thing I can think of is that you don't know how to display a folder dialog box in VBScript, so you're resorting to trying to do it in C#. Is that correct?
If so, you can show a folder dialog in VBScript as shown here: http://www.robvanderwoude.com/vbstech_ui_browsefolder.php

Categories

Resources